2014-01-17 10:12:11 +00:00
|
|
|
(ns testreagent
|
2013-12-16 22:19:36 +00:00
|
|
|
(:require-macros [cemerick.cljs.test
|
|
|
|
:refer (is deftest with-test run-tests testing)]
|
2014-01-17 10:12:11 +00:00
|
|
|
[reagent.ratom :refer [reaction]]
|
|
|
|
[reagent.debug :refer [dbg println log]])
|
2013-12-16 22:19:36 +00:00
|
|
|
(:require [cemerick.cljs.test :as t]
|
2014-01-17 10:12:11 +00:00
|
|
|
[reagent.core :as reagent :refer [atom]]
|
|
|
|
[reagent.ratom :as rv]))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
|
|
|
(defn running [] (rv/running))
|
|
|
|
|
|
|
|
(def isClient (not (nil? (try (.-document js/window)
|
2013-12-18 11:14:57 +00:00
|
|
|
(catch js/Object e nil)))))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
2014-01-27 15:17:37 +00:00
|
|
|
(def rflush reagent/flush)
|
|
|
|
|
2013-12-16 22:19:36 +00:00
|
|
|
(defn add-test-div [name]
|
|
|
|
(let [doc js/document
|
|
|
|
body (.-body js/document)
|
|
|
|
div (.createElement doc "div")]
|
|
|
|
(.appendChild body div)
|
|
|
|
div))
|
|
|
|
|
|
|
|
(defn with-mounted-component [comp f]
|
|
|
|
(when isClient
|
2014-01-17 10:12:11 +00:00
|
|
|
(let [div (add-test-div "_testreagent")]
|
|
|
|
(let [comp (reagent/render-component comp div #(f comp div))]
|
2014-01-28 16:23:39 +00:00
|
|
|
(reagent/unmount-component-at-node div)
|
|
|
|
(reagent/flush)))))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
|
|
|
(defn found-in [re div]
|
2013-12-18 08:13:16 +00:00
|
|
|
(let [res (.-innerHTML div)]
|
|
|
|
(if (re-find re res)
|
|
|
|
true
|
|
|
|
(do (println "Not found: " res)
|
|
|
|
false))))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
|
|
|
(deftest really-simple-test
|
2013-12-18 11:14:57 +00:00
|
|
|
(when isClient
|
|
|
|
(let [ran (atom 0)
|
|
|
|
really-simple (fn []
|
|
|
|
(swap! ran inc)
|
|
|
|
[:div "div in really-simple"])]
|
|
|
|
(with-mounted-component [really-simple nil nil]
|
|
|
|
(fn [c div]
|
|
|
|
(swap! ran inc)
|
|
|
|
(is (found-in #"div in really-simple" div))))
|
|
|
|
(is (= 2 @ran)))))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
|
|
|
(deftest test-simple-callback
|
2013-12-18 11:14:57 +00:00
|
|
|
(when isClient
|
|
|
|
(let [ran (atom 0)
|
2014-01-17 10:12:11 +00:00
|
|
|
comp (reagent/create-class
|
2014-01-07 18:45:41 +00:00
|
|
|
{:component-did-mount #(swap! ran inc)
|
2014-01-08 15:51:10 +00:00
|
|
|
:render (fn [props children this]
|
|
|
|
(assert (map? props))
|
2014-01-07 18:45:41 +00:00
|
|
|
(swap! ran inc)
|
2014-01-08 15:51:10 +00:00
|
|
|
[:div (str "hi " (:foo props) ".")])})]
|
2013-12-18 11:14:57 +00:00
|
|
|
(with-mounted-component (comp {:foo "you"})
|
|
|
|
(fn [C div]
|
|
|
|
(swap! ran inc)
|
|
|
|
(is (found-in #"hi you" div))
|
|
|
|
|
2014-01-17 10:12:11 +00:00
|
|
|
(reagent/set-props C {:foo "there"})
|
2013-12-16 22:19:36 +00:00
|
|
|
(is (found-in #"hi there" div))
|
|
|
|
|
2013-12-18 11:14:57 +00:00
|
|
|
(let [runs @ran]
|
2014-01-17 10:12:11 +00:00
|
|
|
(reagent/set-props C {:foo "there"})
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (found-in #"hi there" div))
|
|
|
|
(is (= runs @ran)))
|
|
|
|
|
2014-01-17 10:12:11 +00:00
|
|
|
(reagent/replace-props C {:foobar "not used"})
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (found-in #"hi ." div))))
|
|
|
|
(is (= 5 @ran)))))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
|
|
|
(deftest test-state-change
|
2013-12-18 11:14:57 +00:00
|
|
|
(when isClient
|
|
|
|
(let [ran (atom 0)
|
2014-01-17 10:12:11 +00:00
|
|
|
comp (reagent/create-class
|
2014-01-07 18:45:41 +00:00
|
|
|
{:get-initial-state (fn [])
|
2014-01-08 15:51:10 +00:00
|
|
|
:render (fn [props children this]
|
2014-01-07 18:45:41 +00:00
|
|
|
(swap! ran inc)
|
2014-01-17 10:12:11 +00:00
|
|
|
[:div (str "hi " (:foo (reagent/state this)))])})]
|
2013-12-18 11:14:57 +00:00
|
|
|
(with-mounted-component (comp)
|
|
|
|
(fn [C div]
|
|
|
|
(swap! ran inc)
|
|
|
|
(is (found-in #"hi " div))
|
|
|
|
|
2014-01-17 10:12:11 +00:00
|
|
|
(reagent/set-state C {:foo "there"})
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (found-in #"hi there" div))
|
|
|
|
|
2014-01-17 10:12:11 +00:00
|
|
|
(reagent/set-state C {:foo "you"})
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (found-in #"hi you" div))))
|
|
|
|
(is (= 4 @ran)))))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
|
|
|
(deftest test-ratom-change
|
2013-12-18 11:14:57 +00:00
|
|
|
(when isClient
|
|
|
|
(let [ran (atom 0)
|
|
|
|
runs (running)
|
|
|
|
val (atom 0)
|
2014-01-27 12:37:59 +00:00
|
|
|
secval (atom 0)
|
2013-12-18 11:14:57 +00:00
|
|
|
v1 (reaction @val)
|
|
|
|
comp (fn []
|
|
|
|
(swap! ran inc)
|
2014-01-27 12:37:59 +00:00
|
|
|
[:div (str "val " @v1 @val @secval)])]
|
2013-12-18 11:14:57 +00:00
|
|
|
(with-mounted-component [comp]
|
|
|
|
(fn [C div]
|
2014-01-27 12:37:59 +00:00
|
|
|
(reagent/flush)
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (not= runs (running)))
|
|
|
|
(is (found-in #"val 0" div))
|
2014-01-27 12:37:59 +00:00
|
|
|
(is (= 1 @ran))
|
2013-12-18 11:14:57 +00:00
|
|
|
|
2014-01-27 12:37:59 +00:00
|
|
|
(reset! secval 1)
|
|
|
|
(reset! secval 0)
|
|
|
|
(reset! val 1)
|
|
|
|
(reset! val 2)
|
2013-12-18 11:14:57 +00:00
|
|
|
(reset! val 1)
|
2014-01-27 12:37:59 +00:00
|
|
|
(reagent/flush)
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (found-in #"val 1" div))
|
2014-01-27 12:37:59 +00:00
|
|
|
(is (= 2 @ran))
|
2013-12-18 11:14:57 +00:00
|
|
|
|
|
|
|
;; should not be rendered
|
|
|
|
(reset! val 1)
|
2014-01-27 12:37:59 +00:00
|
|
|
(reagent/flush)
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (found-in #"val 1" div))
|
2014-01-27 12:37:59 +00:00
|
|
|
(is (= 2 @ran))))
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (= runs (running)))
|
2014-01-27 12:37:59 +00:00
|
|
|
(is (= 2 @ran)))))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
2014-01-27 15:17:37 +00:00
|
|
|
(deftest batched-update-test []
|
|
|
|
(when isClient
|
|
|
|
(let [ran (atom 0)
|
|
|
|
v1 (atom 0)
|
|
|
|
v2 (atom 0)
|
|
|
|
c2 (fn [{val :val}]
|
|
|
|
(swap! ran inc)
|
|
|
|
(assert (= @v1 val))
|
|
|
|
[:div @v2])
|
|
|
|
c1 (fn []
|
|
|
|
(swap! ran inc)
|
|
|
|
[:div @v1
|
|
|
|
[c2 {:val @v1}]])]
|
|
|
|
(with-mounted-component [c1]
|
|
|
|
(fn [c div]
|
|
|
|
(rflush)
|
|
|
|
(is (= @ran 2))
|
|
|
|
(swap! v2 inc)
|
|
|
|
(is (= @ran 2))
|
|
|
|
(rflush)
|
|
|
|
(is (= @ran 3))
|
|
|
|
(swap! v1 inc)
|
|
|
|
(rflush)
|
|
|
|
(is (= @ran 5))
|
|
|
|
(swap! v2 inc)
|
|
|
|
(swap! v1 inc)
|
|
|
|
(rflush)
|
|
|
|
(is (= @ran 7))
|
|
|
|
(swap! v1 inc)
|
|
|
|
(swap! v1 inc)
|
|
|
|
(swap! v2 inc)
|
|
|
|
(rflush)
|
|
|
|
(is (= @ran 9)))))))
|
|
|
|
|
2013-12-18 08:13:16 +00:00
|
|
|
(deftest init-state-test
|
2013-12-18 11:14:57 +00:00
|
|
|
(when isClient
|
|
|
|
(let [ran (atom 0)
|
2014-01-08 15:51:10 +00:00
|
|
|
really-simple (fn [props children this]
|
2013-12-18 11:14:57 +00:00
|
|
|
(swap! ran inc)
|
2014-01-17 10:12:11 +00:00
|
|
|
(reagent/set-state this {:foo "foobar"})
|
2013-12-18 11:14:57 +00:00
|
|
|
(fn []
|
2014-01-08 15:51:10 +00:00
|
|
|
[:div (str "this is "
|
2014-01-17 10:12:11 +00:00
|
|
|
(:foo (reagent/state this)))]))]
|
2013-12-18 11:14:57 +00:00
|
|
|
(with-mounted-component [really-simple nil nil]
|
|
|
|
(fn [c div]
|
|
|
|
(swap! ran inc)
|
|
|
|
(is (found-in #"this is foobar" div))))
|
|
|
|
(is (= 2 @ran)))))
|
2013-12-18 08:13:16 +00:00
|
|
|
|
2014-01-25 12:21:14 +00:00
|
|
|
(defn as-string [comp]
|
|
|
|
(reagent/render-component-to-string comp))
|
|
|
|
|
2013-12-18 08:13:16 +00:00
|
|
|
(deftest to-string-test []
|
|
|
|
(let [comp (fn [props]
|
|
|
|
[:div (str "i am " (:foo props))])]
|
|
|
|
(is (re-find #"i am foobar"
|
2014-01-25 12:21:14 +00:00
|
|
|
(as-string [comp {:foo "foobar"}])))))
|
|
|
|
|
|
|
|
(deftest data-aria-test []
|
|
|
|
(is (re-find #"data-foo"
|
|
|
|
(as-string [:div {:data-foo "x"}])))
|
|
|
|
(is (re-find #"aria-foo"
|
|
|
|
(as-string [:div {:aria-foo "x"}])))
|
|
|
|
(is (not (re-find #"enctype"
|
|
|
|
(as-string [:div {"enc-type" "x"}])))
|
|
|
|
"Strings are passed through to React.")
|
|
|
|
(is (re-find #"enctype"
|
|
|
|
(as-string [:div {"encType" "x"}]))
|
|
|
|
"Strings are passed through to React, and have to be camelcase.")
|
|
|
|
(is (re-find #"enctype"
|
|
|
|
(as-string [:div {:enc-type "x"}]))
|
|
|
|
"Strings are passed through to React, and have to be camelcase."))
|