Skip to content

Commit

Permalink
Merge pull request #28 from pitch-io/fix-setup-mocks
Browse files Browse the repository at this point in the history
Always recreate mocks inside of `setup-mocks`
  • Loading branch information
jo-sm authored May 25, 2023
2 parents a84b9df + 385c528 commit ae2a3a8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
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

0 comments on commit ae2a3a8

Please sign in to comment.