2015-02-10 13:18:56 +00:00
|
|
|
(ns reagenttest.testwrap
|
2015-01-31 22:09:42 +00:00
|
|
|
(:require [cljs.test :as t :refer-macros [is deftest]]
|
2014-12-03 13:50:03 +00:00
|
|
|
[reagent.debug :refer-macros [dbg println log]]
|
2017-11-08 18:08:27 +00:00
|
|
|
[reagent.core :as r]
|
2017-11-08 19:03:50 +00:00
|
|
|
[reagenttest.utils :as u :refer [with-mounted-component found-in]]))
|
2014-12-03 13:50:03 +00:00
|
|
|
|
|
|
|
(deftest test-wrap-basic
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [state (r/atom {:foo 1})
|
2014-12-03 13:50:03 +00:00
|
|
|
ws (fn [] (r/wrap (:foo @state)
|
|
|
|
swap! state assoc :foo))]
|
|
|
|
(let [w1 (ws) w2 (ws)]
|
|
|
|
(is (= @w1 1))
|
|
|
|
(is (= w1 w2))
|
|
|
|
(reset! w1 1)
|
|
|
|
(is (= @w1 1))
|
|
|
|
(is (= @w1 @w2))
|
|
|
|
(is (not= w1 w2)))
|
|
|
|
|
|
|
|
(let [w1 (ws) w2 (ws)]
|
|
|
|
(is (= @w1 1))
|
|
|
|
(is (= w1 w2))
|
|
|
|
(reset! w2 1)
|
|
|
|
(is (= @w2 1))
|
|
|
|
(is (= @w1 @w2))
|
|
|
|
(is (not= w1 w2))
|
|
|
|
(reset! w1 1))
|
2017-10-14 10:06:29 +00:00
|
|
|
|
2014-12-03 13:50:03 +00:00
|
|
|
(let [w1 (ws) w2 (ws)]
|
|
|
|
(is (= @w1 1))
|
|
|
|
(is (= w1 w2))
|
2014-12-03 15:07:58 +00:00
|
|
|
(is (= w2 w1))
|
2014-12-03 13:50:03 +00:00
|
|
|
(reset! w1 2)
|
|
|
|
(is (= @w1 2))
|
|
|
|
(is (= (:foo @state) 2))
|
|
|
|
(is (not= @w1 @w2))
|
|
|
|
(is (not= w1 w2))
|
2014-12-03 15:07:58 +00:00
|
|
|
(is (not= w2 w1))
|
2014-12-03 13:50:03 +00:00
|
|
|
(reset! w1 1)
|
|
|
|
(is (= (:foo @state) 1)))
|
|
|
|
|
|
|
|
(let [w1 (ws) w2 (ws)]
|
|
|
|
(is (= @w1 1))
|
|
|
|
(is (= w1 w2))
|
|
|
|
(reset! w1 2)
|
|
|
|
(reset! w2 2)
|
|
|
|
(is (= @w1 2))
|
|
|
|
(is (= (:foo @state) 2))
|
|
|
|
(is (= @w2 2))
|
|
|
|
(is (= @w1 @w2))
|
|
|
|
(is (not= w1 w2))
|
|
|
|
(reset! w1 1))))
|
|
|
|
|
2014-12-03 15:07:58 +00:00
|
|
|
(deftest test-wrap-equality
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [a (r/atom 1)
|
|
|
|
b (r/atom 2)]
|
2014-12-03 15:07:58 +00:00
|
|
|
(is (= (r/wrap @a swap! a assoc :foo)
|
|
|
|
(r/wrap @a swap! a assoc :foo)))
|
|
|
|
(is (not= (r/wrap @a swap! a assoc :foo)
|
|
|
|
(r/wrap @b swap! a assoc :foo)))
|
|
|
|
(is (not= (r/wrap @a swap! a assoc :foo)
|
|
|
|
(r/wrap @a swap! b assoc :foo)))
|
|
|
|
(is (not= (r/wrap @a swap! a assoc :foo)
|
|
|
|
(r/wrap @a swap! a identity :foo)))
|
|
|
|
(is (not= (r/wrap @a swap! a assoc :foo)
|
|
|
|
(r/wrap @a swap! a assoc :bar)))
|
|
|
|
|
|
|
|
(is (= (r/wrap @a update-in [:foo :bar] inc)
|
|
|
|
(r/wrap @a update-in [:foo :bar] inc)))
|
|
|
|
|
|
|
|
(is (= (r/wrap @a identity)
|
|
|
|
(r/wrap @a identity)))
|
|
|
|
(is (not= (r/wrap @a identity)
|
|
|
|
(r/wrap @b identity)))
|
|
|
|
|
|
|
|
(is (= (r/wrap @a reset! a)
|
|
|
|
(r/wrap @a reset! a)))))
|
|
|
|
|
|
|
|
(deftest test-wrap-returns
|
|
|
|
(let [n (fn [] :foobar)
|
2015-07-31 13:13:27 +00:00
|
|
|
a (r/atom {:k 1})
|
|
|
|
b (r/wrap {:k 1} n)]
|
2014-12-03 15:07:58 +00:00
|
|
|
(is (not= a b))
|
|
|
|
(is (not= b a))
|
|
|
|
(is (= (swap! a update-in [:k] inc)
|
|
|
|
(swap! b update-in [:k] inc)))
|
|
|
|
(is (= @a @b {:k 2}))
|
2015-07-23 12:54:46 +00:00
|
|
|
(is (= (swap! a assoc :k 3 :l 4 :m 7 :n 8 :o)
|
|
|
|
(swap! b assoc :k 3 :l 4 :m 7 :n 8 :o)))
|
2014-12-03 15:07:58 +00:00
|
|
|
(is (= (reset! a 23)
|
|
|
|
(reset! b 23)))
|
|
|
|
(is (= @a @b))
|
|
|
|
(is (= (swap! a inc)
|
|
|
|
(swap! b inc)))
|
|
|
|
(is (= @a @b 24))))
|
|
|
|
|
2014-12-03 13:50:03 +00:00
|
|
|
(deftest test-wrap
|
|
|
|
(when r/is-client
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [state (r/atom {:foo {:bar {:foobar 1}}})
|
|
|
|
ran (r/atom 0)
|
2014-12-03 13:50:03 +00:00
|
|
|
grand-state (clojure.core/atom nil)
|
|
|
|
grand-child (fn [v]
|
|
|
|
(swap! ran inc)
|
|
|
|
(reset! grand-state v)
|
|
|
|
[:div (str "value:" (:foobar @v) ":")])
|
|
|
|
child (fn [v]
|
2015-07-31 13:13:27 +00:00
|
|
|
[grand-child (r/wrap (:bar @v)
|
2014-12-03 13:50:03 +00:00
|
|
|
swap! v assoc :bar)])
|
|
|
|
parent (fn []
|
2015-07-31 13:13:27 +00:00
|
|
|
[child (r/wrap (:foo @state)
|
2014-12-03 13:50:03 +00:00
|
|
|
swap! state assoc :foo)])]
|
2017-10-14 10:06:29 +00:00
|
|
|
(t/async done
|
2017-11-08 18:08:27 +00:00
|
|
|
(u/with-mounted-component-async [parent] done
|
2017-10-14 10:06:29 +00:00
|
|
|
(fn [c div done]
|
2017-11-08 18:08:27 +00:00
|
|
|
(u/run-fns-after-render
|
2017-10-14 10:06:29 +00:00
|
|
|
(fn []
|
|
|
|
(is (found-in #"value:1:" div))
|
|
|
|
(is (= @ran 1))
|
|
|
|
|
|
|
|
(reset! @grand-state {:foobar 2}))
|
|
|
|
(fn []
|
|
|
|
(is (= @state {:foo {:bar {:foobar 2}}}))
|
|
|
|
(is (= @ran 2))
|
|
|
|
(is (found-in #"value:2:" div))
|
|
|
|
|
|
|
|
(swap! state update-in [:foo :bar] assoc :foobar 3))
|
|
|
|
(fn []
|
|
|
|
(is (= @ran 3))
|
|
|
|
(is (found-in #"value:3:" div))
|
|
|
|
(reset! state {:foo {:bar {:foobar 3}}
|
|
|
|
:foo1 {}}))
|
|
|
|
(fn []
|
|
|
|
(is (= @ran 3))
|
|
|
|
(reset! @grand-state {:foobar 3}))
|
|
|
|
(fn []
|
|
|
|
(is (= @ran 3))
|
|
|
|
|
|
|
|
(reset! state {:foo {:bar {:foobar 2}}
|
|
|
|
:foo2 {}}))
|
|
|
|
(fn []
|
|
|
|
(is (found-in #"value:2:" div))
|
|
|
|
(is (= @ran 4))
|
|
|
|
|
|
|
|
(reset! @grand-state {:foobar 2}))
|
|
|
|
(fn []
|
|
|
|
(is (found-in #"value:2:" div))
|
|
|
|
(is (= @ran 5))
|
|
|
|
|
|
|
|
(reset! state {:foo {:bar {:foobar 4}}})
|
|
|
|
(reset! @grand-state {:foobar 4}))
|
|
|
|
(fn []
|
|
|
|
(is (found-in #"value:4:" div))
|
|
|
|
(is (= @ran 6))
|
|
|
|
|
|
|
|
(reset! @grand-state {:foobar 4}))
|
|
|
|
(fn []
|
|
|
|
(is (found-in #"value:4:" div))
|
|
|
|
(is (= @ran 7)))
|
|
|
|
done)))))))
|
2015-02-04 21:14:04 +00:00
|
|
|
|
|
|
|
(deftest test-cursor
|
2017-11-28 15:12:29 +00:00
|
|
|
(when r/is-client
|
|
|
|
(let [state (r/atom {:a {:v 1}
|
|
|
|
:b {:v 2}})
|
|
|
|
a-count (r/atom 0)
|
|
|
|
b-count (r/atom 0)
|
|
|
|
derefer (fn derefer [cur count]
|
|
|
|
(swap! count inc)
|
|
|
|
[:div "" @cur])
|
|
|
|
comp (fn test-cursor []
|
|
|
|
[:div
|
|
|
|
[derefer (r/cursor state [:a]) a-count]
|
|
|
|
[derefer (r/cursor state [:b]) b-count]])]
|
|
|
|
(t/async done
|
|
|
|
(u/with-mounted-component-async [comp] done
|
|
|
|
(fn [c div done]
|
|
|
|
(u/run-fns-after-render
|
|
|
|
(fn []
|
|
|
|
(is (= @a-count 1))
|
|
|
|
(is (= @b-count 1))
|
|
|
|
|
|
|
|
|
|
|
|
(swap! state update-in [:a :v] inc)
|
|
|
|
(is (= @a-count 1)))
|
|
|
|
(fn []
|
|
|
|
(is (= @a-count 2))
|
|
|
|
(is (= @b-count 1))
|
|
|
|
|
|
|
|
(reset! state {:a {:v 2} :b {:v 2}}))
|
|
|
|
(fn []
|
|
|
|
(is (= @a-count 2))
|
|
|
|
(is (= @b-count 1))
|
|
|
|
|
|
|
|
(reset! state {:a {:v 3} :b {:v 2}}))
|
|
|
|
(fn []
|
|
|
|
(is (= @a-count 3))
|
|
|
|
(is (= @b-count 1)))
|
|
|
|
done)))))))
|
2015-09-11 13:06:42 +00:00
|
|
|
|
|
|
|
(deftest test-fn-cursor
|
2015-09-11 17:36:55 +00:00
|
|
|
(let [state (r/atom {:a {:v 1}
|
|
|
|
:b {:v 2}})
|
|
|
|
statec (r/cursor state [])
|
2015-09-11 13:06:42 +00:00
|
|
|
a-count (r/atom 0)
|
|
|
|
b-count (r/atom 0)
|
|
|
|
derefer (fn derefer [cur count]
|
2016-07-14 07:34:03 +00:00
|
|
|
[:div "" @cur])
|
2015-09-11 17:36:55 +00:00
|
|
|
f (fn [[x y]] (swap! y inc) (get-in @statec x))
|
2015-09-11 13:06:42 +00:00
|
|
|
ac (r/cursor f [[:a] a-count])
|
|
|
|
bc (r/cursor f [[:b] b-count])
|
|
|
|
comp (fn test-cursor []
|
|
|
|
[:div
|
|
|
|
[derefer ac]
|
|
|
|
[derefer bc]])]
|
|
|
|
(with-mounted-component [comp]
|
|
|
|
(fn [c div]
|
|
|
|
(is (= @a-count 1))
|
|
|
|
(is (= @b-count 1))
|
|
|
|
|
2015-09-11 17:36:55 +00:00
|
|
|
(swap! state update-in [:a :v] inc)
|
2015-09-11 13:06:42 +00:00
|
|
|
(is (= @a-count 1))
|
|
|
|
(is (= @b-count 1))
|
2017-10-14 10:06:29 +00:00
|
|
|
|
2015-09-11 13:06:42 +00:00
|
|
|
(r/flush)
|
|
|
|
(is (= @a-count 2))
|
|
|
|
(is (= @b-count 2))
|
|
|
|
|
2015-09-11 17:36:55 +00:00
|
|
|
(reset! state {:a {:v 2} :b {:v 2}})
|
2015-09-11 13:06:42 +00:00
|
|
|
(r/flush)
|
|
|
|
(is (= @a-count 2))
|
2015-09-11 17:36:55 +00:00
|
|
|
(is (= @b-count 2))
|
|
|
|
|
|
|
|
(reset! state {:a {:v 3} :b {:v 2}})
|
|
|
|
(r/flush)
|
|
|
|
(is (= @a-count 3))
|
|
|
|
(is (= @b-count 3))))))
|