diff --git a/cljest/package-lock.json b/cljest/package-lock.json index 48d7af8..7719d58 100644 --- a/cljest/package-lock.json +++ b/cljest/package-lock.json @@ -23,7 +23,7 @@ } }, "../jest-preset-cljest": { - "version": "1.0.0-alpha2", + "version": "1.0.0", "dev": true, "license": "MIT", "dependencies": { diff --git a/cljest/src/cljest/helpers/core.clj b/cljest/src/cljest/helpers/core.clj index bdd87f5..19bdda7 100644 --- a/cljest/src/cljest/helpers/core.clj +++ b/cljest/src/cljest/helpers/core.clj @@ -7,13 +7,14 @@ [start finish bindings & body] (let [names (take-nth 2 bindings) vals (take-nth 2 (drop 1 bindings)) + wrapped-vals (map (fn [v] (list 'fn [] v)) vals) orig-val-syms (for [_ names] (gensym)) temp-val-syms (for [_ names] (gensym)) binds (map vector names temp-val-syms) redefs (reverse (map vector names orig-val-syms)) - bind-value (fn [[k v]] (list 'set! k v))] + bind-value (fn [[k v]] (list 'set! k (list v)))] `(let [~@(interleave orig-val-syms names) - ~@(interleave temp-val-syms vals) + ~@(interleave temp-val-syms wrapped-vals) ~start #(do ~@(map bind-value binds)) ~finish #(do ~@(map bind-value redefs))] ~@body))) diff --git a/cljest/src/cljest/helpers/core_test.cljs b/cljest/src/cljest/helpers/core_test.cljs index 148c38b..1f7d97f 100644 --- a/cljest/src/cljest/helpers/core_test.cljs +++ b/cljest/src/cljest/helpers/core_test.cljs @@ -3,6 +3,51 @@ [cljest.helpers.core :as h] [cyrik.cljs-macroexpand :refer [cljs-macroexpand-all] :rename {cljs-macroexpand-all macroexpand-all}])) +(describe "with-mocks" + (defn ^:private cool-fn + [x y] + (* x y)) + + (it "works" + (is (= 50 (cool-fn 5 10))) + + (h/with-mocks [cool-fn #(+ %1 %2)] + (is (= 15 (cool-fn 5 10)))))) + +(describe "setup-mocks" + (def ^:private something-stateful + (let [counter (atom 0)] + (fn [] + (swap! counter inc) + @counter))) + + (def ^:private something-else-stateful + (let [counter (atom 0)] + (fn [] + (swap! counter dec) + @counter))) + + (h/setup-mocks [something-stateful (let [counter (atom 0)] + (fn [] + (swap! counter (partial + 2)) + @counter)) + + something-else-stateful (let [counter (atom 0)] + (fn [] + (swap! counter #(- % 2)) + @counter))]) + + (it "works" + (is (= 2 (something-stateful))) + (is (= 4 (something-stateful))) + + (is (= -2 (something-else-stateful))) + (is (= -4 (something-else-stateful)))) + + (it "reinstantiates each mock for each test case in scope" + (is (= 2 (something-stateful))) + (is (= -2 (something-else-stateful))))) + (describe "async" (it "should macroexpand into a resolves promise when called with nothing" (is (= (macroexpand-all '(js/Promise.resolve))