Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always recreate mocks inside of setup-mocks #28

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cljest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions cljest/src/cljest/helpers/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
45 changes: 45 additions & 0 deletions cljest/src/cljest/helpers/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down