Simplify the few tests we have

This commit is contained in:
Dan Holmsand 2013-12-17 13:25:21 +01:00
parent 9e5e38fbc7
commit 59333443a7
1 changed files with 37 additions and 40 deletions

View File

@ -28,88 +28,85 @@
(defn found-in [re div] (defn found-in [re div]
(re-find re (.-innerHTML div))) (re-find re (.-innerHTML div)))
(def tests-run (clojure.core/atom 0))
(def tests-should-run (clojure.core/atom 0))
(defn really-simple []
[:div "div in really-simple"])
(deftest really-simple-test (deftest really-simple-test
(swap! tests-should-run inc) (let [ran (atom 0)
(with-mounted-component [really-simple nil nil] really-simple (fn []
(fn [c div] (swap! ran inc)
(swap! tests-run inc) [:div "div in really-simple"])]
(is (found-in #"div in really-simple" div))))) (with-mounted-component [really-simple nil nil]
(fn [c div]
(swap! ran inc)
(is (found-in #"div in really-simple" div))))
(is (= 2 @ran))))
(deftest test-simple-callback (deftest test-simple-callback
(swap! tests-should-run + 6) (let [ran (atom 0)
(let [comp (r/create-class comp (r/create-class
{:component-did-mount #(swap! tests-run inc) {:component-did-mount #(swap! ran inc)
:render (fn [P C S] :render (fn [P C]
(assert (map? P)) (assert (map? P))
(swap! tests-run inc) (swap! ran inc)
[:div (str "hi " (:foo P) ".")])})] [:div (str "hi " (:foo P) ".")])})]
(with-mounted-component (comp {:foo "you"}) (with-mounted-component (comp {:foo "you"})
(fn [C div] (fn [C div]
(swap! tests-run inc) (swap! ran inc)
(is (found-in #"hi you" div)) (is (found-in #"hi you" div))
(r/set-props C {:foo "there"}) (r/set-props C {:foo "there"})
(is (found-in #"hi there" div)) (is (found-in #"hi there" div))
(let [runs @tests-run] (let [runs @ran]
(r/set-props C {:foo "there"}) (r/set-props C {:foo "there"})
(is (found-in #"hi there" div)) (is (found-in #"hi there" div))
(is (= runs @tests-run))) (is (= runs @ran)))
(r/replace-props C {:foobar "not used"}) (r/replace-props C {:foobar "not used"})
(is (found-in #"hi ." div)))))) (is (found-in #"hi ." div))))
(is (= 5 @ran))))
(deftest test-state-change (deftest test-state-change
(swap! tests-should-run + 3) (let [ran (atom 0)
(let [comp (r/create-class comp (r/create-class
{:get-initial-state (fn []) {:get-initial-state (fn [])
:render (fn [P C S] :render (fn [P C]
(swap! tests-run inc) (swap! ran inc)
[:div (str "hi " (:foo S))])})] [:div (str "hi " (:foo @C))])})]
(with-mounted-component (comp) (with-mounted-component (comp)
(fn [C div] (fn [C div]
(swap! tests-run inc) (swap! ran inc)
(is (found-in #"hi " div)) (is (found-in #"hi " div))
(swap! C assoc :foo "there") (swap! C assoc :foo "there")
(is (found-in #"hi there" div)) (is (found-in #"hi there" div))
(swap! C assoc :foo "you") (swap! C assoc :foo "you")
(is (found-in #"hi you" div)))))) (is (found-in #"hi you" div))))
(is (= 4 @ran))))
(deftest test-ratom-change (deftest test-ratom-change
(swap! tests-should-run + 3) (let [ran (atom 0)
(let [runs (running) runs (running)
val (atom 0) val (atom 0)
v1 (reaction @val) v1 (reaction @val)
ran @tests-run
comp (fn [] comp (fn []
(swap! tests-run inc) (swap! ran inc)
[:div (str "val " @v1)])] [:div (str "val " @v1)])]
(with-mounted-component [comp] (with-mounted-component [comp]
(fn [C div] (fn [C div]
(swap! tests-run inc) (swap! ran inc)
(is (not= runs (running))) (is (not= runs (running)))
(is (found-in #"val 0" div)) (is (found-in #"val 0" div))
(is (= @tests-run (+ ran 2))) (is (= 2 @ran))
(reset! val 1) (reset! val 1)
(is (found-in #"val 1" div)) (is (found-in #"val 1" div))
(is (= @tests-run (+ ran 3))) (is (= 3 @ran))
;; should not be rendered ;; should not be rendered
(reset! val 1) (reset! val 1)
(is (found-in #"val 1" div)) (is (found-in #"val 1" div))
(is (= @tests-run (+ ran 3))))) (is (= 3 @ran))))
(is (= runs (running))))) (is (= runs (running)))
(is (= 3 @ran))))
(deftest check-that-test-ran
(if isClient
(is (= @tests-run @tests-should-run))
(is (= @tests-run 0))))