2015-02-10 13:18:56 +00:00
|
|
|
(ns reagenttest.testreagent
|
2015-01-31 22:09:42 +00:00
|
|
|
(:require [cljs.test :as t :refer-macros [is deftest testing]]
|
2015-09-24 08:19:30 +00:00
|
|
|
[reagent.ratom :as rv :refer-macros [reaction]]
|
2015-01-31 22:09:42 +00:00
|
|
|
[reagent.debug :refer-macros [dbg println log]]
|
2015-02-02 14:19:43 +00:00
|
|
|
[reagent.interop :refer-macros [.' .!]]
|
2015-07-31 13:13:27 +00:00
|
|
|
[reagent.core :as r]))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
|
|
|
(defn running [] (rv/running))
|
|
|
|
|
2015-07-31 13:13:27 +00:00
|
|
|
(def isClient r/is-client)
|
2013-12-16 22:19:36 +00:00
|
|
|
|
2015-07-31 13:13:27 +00:00
|
|
|
(def rflush r/flush)
|
2014-01-27 15:17:37 +00:00
|
|
|
|
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")]
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [comp (r/render-component comp div #(f comp div))]
|
|
|
|
(r/unmount-component-at-node div)
|
|
|
|
(r/flush)
|
2014-12-03 15:14:07 +00:00
|
|
|
(.removeChild (.-body js/document) div)))))
|
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
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ran (r/atom 0)
|
2013-12-18 11:14:57 +00:00
|
|
|
really-simple (fn []
|
|
|
|
(swap! ran inc)
|
|
|
|
[:div "div in really-simple"])]
|
|
|
|
(with-mounted-component [really-simple nil nil]
|
|
|
|
(fn [c div]
|
|
|
|
(swap! ran inc)
|
2014-12-09 11:58:30 +00:00
|
|
|
(is (found-in #"div in really-simple" div))
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/flush)
|
2014-12-09 11:58:30 +00:00
|
|
|
(is (= 2 @ran))
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/force-update-all)
|
2014-12-09 11:58:30 +00:00
|
|
|
(is (= 3 @ran))))
|
|
|
|
(is (= 3 @ran)))))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
|
|
|
(deftest test-simple-callback
|
2013-12-18 11:14:57 +00:00
|
|
|
(when isClient
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ran (r/atom 0)
|
|
|
|
comp (r/create-class
|
2014-02-08 12:55:01 +00:00
|
|
|
{:component-did-mount #(swap! ran inc)
|
|
|
|
:render
|
|
|
|
(fn [this]
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [props (r/props this)]
|
2014-02-14 10:27:45 +00:00
|
|
|
(is (map? props))
|
2015-07-31 13:13:27 +00:00
|
|
|
(is (= props ((r/argv this) 1)))
|
|
|
|
(is (= 1 (first (r/children this))))
|
|
|
|
(is (= 1 (count (r/children this))))
|
2014-02-08 12:55:01 +00:00
|
|
|
(swap! ran inc)
|
|
|
|
[:div (str "hi " (:foo props) ".")]))})]
|
2015-08-20 07:35:24 +00:00
|
|
|
(with-mounted-component [comp {:foo "you"} 1]
|
2013-12-18 11:14:57 +00:00
|
|
|
(fn [C div]
|
|
|
|
(swap! ran inc)
|
2014-02-10 08:31:25 +00:00
|
|
|
(is (found-in #"hi you" div))))
|
|
|
|
(is (= 3 @ran)))))
|
2013-12-16 22:19:36 +00:00
|
|
|
|
|
|
|
(deftest test-state-change
|
2013-12-18 11:14:57 +00:00
|
|
|
(when isClient
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ran (r/atom 0)
|
|
|
|
self (r/atom nil)
|
|
|
|
comp (r/create-class
|
2014-02-15 10:48:12 +00:00
|
|
|
{:get-initial-state (fn [] {:foo "initial"})
|
2015-02-04 21:45:39 +00:00
|
|
|
:reagent-render
|
2014-02-08 12:55:01 +00:00
|
|
|
(fn []
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [this (r/current-component)]
|
2014-03-25 05:23:44 +00:00
|
|
|
(reset! self this)
|
2014-02-08 12:55:01 +00:00
|
|
|
(swap! ran inc)
|
2015-07-31 13:13:27 +00:00
|
|
|
[:div (str "hi " (:foo (r/state this)))]))})]
|
2015-08-20 07:35:24 +00:00
|
|
|
(with-mounted-component [comp]
|
2013-12-18 11:14:57 +00:00
|
|
|
(fn [C div]
|
|
|
|
(swap! ran inc)
|
2014-02-15 10:48:12 +00:00
|
|
|
(is (found-in #"hi initial" div))
|
2013-12-18 11:14:57 +00:00
|
|
|
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/replace-state @self {:foo "there"})
|
|
|
|
(r/state @self)
|
2014-03-25 05:23:44 +00:00
|
|
|
|
2014-02-15 10:48:12 +00:00
|
|
|
(rflush)
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (found-in #"hi there" div))
|
|
|
|
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/set-state @self {:foo "you"})
|
2014-02-15 10:48:12 +00:00
|
|
|
(rflush)
|
2014-12-09 11:58:30 +00:00
|
|
|
(is (found-in #"hi you" div))))
|
2013-12-18 11:14:57 +00:00
|
|
|
(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
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ran (r/atom 0)
|
2013-12-18 11:14:57 +00:00
|
|
|
runs (running)
|
2015-07-31 13:13:27 +00:00
|
|
|
val (r/atom 0)
|
|
|
|
secval (r/atom 0)
|
2015-09-11 13:06:42 +00:00
|
|
|
v1-ran (atom 0)
|
|
|
|
v1 (reaction (swap! v1-ran inc) @val)
|
2013-12-18 11:14:57 +00:00
|
|
|
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]
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/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)
|
2013-12-18 11:14:57 +00:00
|
|
|
(reset! val 1)
|
2014-01-27 12:37:59 +00:00
|
|
|
(reset! val 2)
|
2013-12-18 11:14:57 +00:00
|
|
|
(reset! val 1)
|
2015-08-31 08:59:59 +00:00
|
|
|
(is (= 1 @ran))
|
2015-09-11 13:06:42 +00:00
|
|
|
(is (= 1 @v1-ran))
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/flush)
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (found-in #"val 1" div))
|
2015-08-31 09:36:42 +00:00
|
|
|
(is (= 2 @ran) "ran once more")
|
2015-09-11 13:06:42 +00:00
|
|
|
(is (= 2 @v1-ran))
|
2013-12-18 11:14:57 +00:00
|
|
|
|
|
|
|
;; should not be rendered
|
|
|
|
(reset! val 1)
|
2015-09-11 13:06:42 +00:00
|
|
|
(is (= 2 @v1-ran))
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/flush)
|
2015-09-11 13:06:42 +00:00
|
|
|
(is (= 2 @v1-ran))
|
2013-12-18 11:14:57 +00:00
|
|
|
(is (found-in #"val 1" div))
|
2015-08-31 09:36:42 +00:00
|
|
|
(is (= 2 @ran) "did not run")))
|
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
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ran (r/atom 0)
|
|
|
|
v1 (r/atom 0)
|
|
|
|
v2 (r/atom 0)
|
2014-01-27 15:17:37 +00:00
|
|
|
c2 (fn [{val :val}]
|
|
|
|
(swap! ran inc)
|
2014-02-14 10:27:45 +00:00
|
|
|
(is (= @v1 val))
|
2014-01-27 15:17:37 +00:00
|
|
|
[: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-16 22:19:36 +00:00
|
|
|
|
2013-12-18 08:13:16 +00:00
|
|
|
(deftest init-state-test
|
2013-12-18 11:14:57 +00:00
|
|
|
(when isClient
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ran (r/atom 0)
|
2014-02-08 12:55:01 +00:00
|
|
|
really-simple (fn []
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [this (r/current-component)]
|
2014-02-08 12:55:01 +00:00
|
|
|
(swap! ran inc)
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/set-state this {:foo "foobar"})
|
2014-02-08 12:55:01 +00:00
|
|
|
(fn []
|
|
|
|
[:div (str "this is "
|
2015-07-31 13:13:27 +00:00
|
|
|
(:foo (r/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-30 09:34:41 +00:00
|
|
|
(deftest shoud-update-test
|
|
|
|
(when isClient
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [parent-ran (r/atom 0)
|
|
|
|
child-ran (r/atom 0)
|
|
|
|
child-props (r/atom nil)
|
2014-01-30 09:34:41 +00:00
|
|
|
f (fn [])
|
|
|
|
f1 (fn [])
|
|
|
|
child (fn [p]
|
|
|
|
(swap! child-ran inc)
|
|
|
|
[:div (:val p)])
|
|
|
|
parent(fn []
|
|
|
|
(swap! parent-ran inc)
|
|
|
|
[:div "child-foo" [child @child-props]])]
|
|
|
|
(with-mounted-component [parent nil nil]
|
|
|
|
(fn [c div]
|
|
|
|
(rflush)
|
|
|
|
(is (= @child-ran 1))
|
|
|
|
(is (found-in #"child-foo" div))
|
|
|
|
(do (reset! child-props {:style {:display :none}})
|
|
|
|
(rflush))
|
|
|
|
(is (= @child-ran 2))
|
|
|
|
(do (reset! child-props {:style {:display :none}})
|
|
|
|
(rflush))
|
|
|
|
(is (= @child-ran 2) "keyw is equal")
|
|
|
|
(do (reset! child-props {:class :foo}) (rflush))
|
|
|
|
(is (= @child-ran 3))
|
|
|
|
(do (reset! child-props {:class :foo}) (rflush))
|
|
|
|
(is (= @child-ran 3))
|
|
|
|
(do (reset! child-props {:class 'foo}) (rflush))
|
|
|
|
(is (= @child-ran 4) "symbols are different from keyw")
|
|
|
|
(do (reset! child-props {:class 'foo}) (rflush))
|
|
|
|
(is (= @child-ran 4) "symbols are equal")
|
|
|
|
(do (reset! child-props {:style {:color 'red}}) (rflush))
|
|
|
|
(is (= @child-ran 5))
|
2015-07-31 13:13:27 +00:00
|
|
|
(do (reset! child-props {:on-change (r/partial f)})
|
2014-01-30 09:34:41 +00:00
|
|
|
(rflush))
|
|
|
|
(is (= @child-ran 6))
|
2015-07-31 13:13:27 +00:00
|
|
|
(do (reset! child-props {:on-change (r/partial f)})
|
2014-01-30 09:34:41 +00:00
|
|
|
(rflush))
|
|
|
|
(is (= @child-ran 6))
|
2015-07-31 13:13:27 +00:00
|
|
|
(do (reset! child-props {:on-change (r/partial f1)})
|
2014-01-30 09:34:41 +00:00
|
|
|
(rflush))
|
2014-12-09 11:58:30 +00:00
|
|
|
(is (= @child-ran 7))
|
|
|
|
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/force-update-all)
|
2014-12-09 11:58:30 +00:00
|
|
|
(is (= @child-ran 8)))))))
|
2014-01-30 09:34:41 +00:00
|
|
|
|
2014-02-03 12:58:31 +00:00
|
|
|
(deftest dirty-test
|
|
|
|
(when isClient
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ran (r/atom 0)
|
|
|
|
state (r/atom 0)
|
2014-02-10 22:08:20 +00:00
|
|
|
really-simple (fn []
|
2014-02-03 12:58:31 +00:00
|
|
|
(swap! ran inc)
|
|
|
|
(if (= @state 1)
|
|
|
|
(reset! state 3))
|
|
|
|
[:div (str "state=" @state)])]
|
|
|
|
(with-mounted-component [really-simple nil nil]
|
|
|
|
(fn [c div]
|
|
|
|
(is (= 1 @ran))
|
|
|
|
(is (found-in #"state=0" div))
|
|
|
|
(reset! state 1)
|
|
|
|
(rflush)
|
|
|
|
(is (= 2 @ran))
|
|
|
|
(is (found-in #"state=3" div))))
|
|
|
|
(is (= 2 @ran)))))
|
2014-01-30 09:34:41 +00:00
|
|
|
|
2014-01-25 12:21:14 +00:00
|
|
|
(defn as-string [comp]
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/render-component-to-string comp))
|
2014-01-25 12:21:14 +00:00
|
|
|
|
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."))
|
2014-01-28 17:00:15 +00:00
|
|
|
|
|
|
|
(deftest dynamic-id-class []
|
|
|
|
(is (re-find #"id=.foo"
|
|
|
|
(as-string [:div#foo {:class "bar"}])))
|
|
|
|
(is (re-find #"class=.foo bar"
|
|
|
|
(as-string [:div.foo {:class "bar"}])))
|
|
|
|
(is (re-find #"class=.foo bar"
|
|
|
|
(as-string [:div.foo.bar])))
|
|
|
|
(is (re-find #"id=.foo"
|
|
|
|
(as-string [:div#foo.foo.bar])))
|
|
|
|
(is (re-find #"class=.xxx bar"
|
|
|
|
(as-string [:div#foo.xxx.bar])))
|
|
|
|
(is (re-find #"id=.foo"
|
|
|
|
(as-string [:div.bar {:id "foo"}])))
|
|
|
|
(is (re-find #"id=.foo"
|
|
|
|
(as-string [:div.bar.xxx {:id "foo"}])))
|
|
|
|
(is (re-find #"id=.foo"
|
|
|
|
(as-string [:div#bar {:id "foo"}]))
|
|
|
|
"Dynamic id overwrites static"))
|
2014-02-11 10:36:58 +00:00
|
|
|
|
|
|
|
(deftest ifn-component []
|
2014-03-03 15:49:41 +00:00
|
|
|
(defmulti my-div :type)
|
|
|
|
(defmethod my-div :fooish [child] [:div.foo (:content child)])
|
|
|
|
(defmethod my-div :barish [child] [:div.bar (:content child)])
|
|
|
|
|
2014-02-11 10:36:58 +00:00
|
|
|
(let [comp {:foo [:div "foodiv"]
|
|
|
|
:bar [:div "bardiv"]}]
|
|
|
|
(is (re-find #"foodiv"
|
|
|
|
(as-string [:div [comp :foo]])))
|
|
|
|
(is (re-find #"bardiv"
|
2014-03-03 15:49:41 +00:00
|
|
|
(as-string [:div [comp :bar]])))
|
|
|
|
(is (re-find #"class=.foo"
|
|
|
|
(as-string [my-div {:type :fooish :content "inner"}])))))
|
2014-02-11 11:03:44 +00:00
|
|
|
|
|
|
|
(deftest symbol-string-tag []
|
|
|
|
(is (re-find #"foobar"
|
|
|
|
(as-string ['div "foobar"])))
|
|
|
|
(is (re-find #"foobar"
|
|
|
|
(as-string ["div" "foobar"])))
|
|
|
|
(is (re-find #"id=.foo"
|
|
|
|
(as-string ['div#foo "x"])))
|
|
|
|
(is (re-find #"id=.foo"
|
|
|
|
(as-string ["div#foo" "x"])))
|
|
|
|
(is (re-find #"class=.foo bar"
|
|
|
|
(as-string ['div.foo {:class "bar"}])))
|
|
|
|
(is (re-find #"class=.foo bar"
|
|
|
|
(as-string ["div.foo.bar"])))
|
|
|
|
(is (re-find #"id=.foo"
|
|
|
|
(as-string ['div#foo.foo.bar]))))
|
2014-02-14 10:27:45 +00:00
|
|
|
|
|
|
|
(deftest partial-test []
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [p1 (r/partial vector 1 2)]
|
2014-02-14 10:27:45 +00:00
|
|
|
(is (= (p1 3) [1 2 3]))
|
2015-07-31 13:13:27 +00:00
|
|
|
(is (= p1 (r/partial vector 1 2)))
|
2014-02-14 10:27:45 +00:00
|
|
|
(is (ifn? p1))
|
2015-07-31 13:13:27 +00:00
|
|
|
(is (= (r/partial vector 1 2) p1))
|
|
|
|
(is (not= p1 (r/partial vector 1 3)))))
|
2014-09-16 14:31:29 +00:00
|
|
|
|
|
|
|
(deftest test-null-component
|
|
|
|
(let [null-comp (fn [do-show]
|
|
|
|
(when do-show
|
|
|
|
[:div "div in test-null-component"]))]
|
|
|
|
(is (not (re-find #"test-null-component"
|
|
|
|
(as-string [null-comp false]))))
|
|
|
|
(is (re-find #"test-null-component"
|
|
|
|
(as-string [null-comp true])))))
|
2014-11-06 10:34:51 +00:00
|
|
|
|
|
|
|
(deftest test-static-markup
|
|
|
|
(is (= "<div>foo</div>"
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/render-to-static-markup
|
2014-11-06 10:34:51 +00:00
|
|
|
[:div "foo"])))
|
|
|
|
(is (= "<div class=\"bar\"><p>foo</p></div>"
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/render-to-static-markup
|
2014-11-06 18:18:56 +00:00
|
|
|
[:div.bar [:p "foo"]])))
|
|
|
|
(is (= "<div class=\"bar\"><p>foobar</p></div>"
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/render-to-static-markup
|
2014-11-06 18:18:56 +00:00
|
|
|
[:div.bar {:dangerously-set-inner-HTML
|
|
|
|
{:__html "<p>foobar</p>"}} ]))))
|
2014-11-22 08:42:31 +00:00
|
|
|
|
|
|
|
(deftest test-return-class
|
|
|
|
(when isClient
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ran (r/atom 0)
|
|
|
|
top-ran (r/atom 0)
|
2014-11-22 08:42:31 +00:00
|
|
|
comp (fn []
|
|
|
|
(swap! top-ran inc)
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/create-class
|
2014-11-22 08:42:31 +00:00
|
|
|
{:component-did-mount #(swap! ran inc)
|
|
|
|
:render
|
|
|
|
(fn [this]
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [props (r/props this)]
|
2014-11-22 08:42:31 +00:00
|
|
|
(is (map? props))
|
2015-07-31 13:13:27 +00:00
|
|
|
(is (= props ((r/argv this) 1)))
|
|
|
|
(is (= 1 (first (r/children this))))
|
|
|
|
(is (= 1 (count (r/children this))))
|
2014-11-22 08:42:31 +00:00
|
|
|
(swap! ran inc)
|
|
|
|
[:div (str "hi " (:foo props) ".")]))}))
|
2015-07-31 13:13:27 +00:00
|
|
|
prop (r/atom {:foo "you"})
|
2014-11-22 08:42:31 +00:00
|
|
|
parent (fn [] [comp @prop 1])]
|
|
|
|
(with-mounted-component [parent]
|
|
|
|
(fn [C div]
|
|
|
|
(swap! ran inc)
|
|
|
|
(is (found-in #"hi you" div))
|
|
|
|
(is (= 1 @top-ran))
|
|
|
|
(is (= 3 @ran))
|
|
|
|
|
|
|
|
(swap! prop assoc :foo "me")
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/flush)
|
2014-11-22 08:42:31 +00:00
|
|
|
(is (found-in #"hi me" div))
|
|
|
|
(is (= 1 @top-ran))
|
|
|
|
(is (= 4 @ran)))))))
|
|
|
|
|
|
|
|
(deftest test-return-class-fn
|
|
|
|
(when isClient
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ran (r/atom 0)
|
|
|
|
top-ran (r/atom 0)
|
2014-11-22 08:42:31 +00:00
|
|
|
comp (fn []
|
|
|
|
(swap! top-ran inc)
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/create-class
|
2014-11-22 08:42:31 +00:00
|
|
|
{:component-did-mount #(swap! ran inc)
|
|
|
|
:component-function
|
|
|
|
(fn [p a]
|
|
|
|
(is (= 1 a))
|
|
|
|
(swap! ran inc)
|
|
|
|
[:div (str "hi " (:foo p) ".")])}))
|
2015-07-31 13:13:27 +00:00
|
|
|
prop (r/atom {:foo "you"})
|
2014-11-22 08:42:31 +00:00
|
|
|
parent (fn [] [comp @prop 1])]
|
|
|
|
(with-mounted-component [parent]
|
|
|
|
(fn [C div]
|
|
|
|
(swap! ran inc)
|
|
|
|
(is (found-in #"hi you" div))
|
|
|
|
(is (= 1 @top-ran))
|
|
|
|
(is (= 3 @ran))
|
|
|
|
|
|
|
|
(swap! prop assoc :foo "me")
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/flush)
|
2014-11-22 08:42:31 +00:00
|
|
|
(is (found-in #"hi me" div))
|
|
|
|
(is (= 1 @top-ran))
|
|
|
|
(is (= 4 @ran)))))))
|
2014-12-02 10:43:35 +00:00
|
|
|
|
|
|
|
(defn rstr [react-elem]
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/render-to-static-markup react-elem))
|
2014-12-02 10:43:35 +00:00
|
|
|
|
|
|
|
(deftest test-create-element
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ae r/as-element
|
|
|
|
ce r/create-element]
|
2014-12-07 19:26:29 +00:00
|
|
|
(is (= (rstr (ae [:div]))
|
|
|
|
(rstr (ce "div"))))
|
2014-12-02 10:43:35 +00:00
|
|
|
(is (= (rstr (ae [:div]))
|
|
|
|
(rstr (ce "div" nil))))
|
|
|
|
(is (= (rstr (ae [:div "foo"]))
|
|
|
|
(rstr (ce "div" nil "foo"))))
|
|
|
|
(is (= (rstr (ae [:div "foo" "bar"]))
|
|
|
|
(rstr (ce "div" nil "foo" "bar"))))
|
|
|
|
(is (= (rstr (ae [:div "foo" "bar" "foobar"]))
|
|
|
|
(rstr (ce "div" nil "foo" "bar" "foobar"))))
|
|
|
|
|
|
|
|
(is (= (rstr (ae [:div.foo "bar"]))
|
|
|
|
(rstr (ce "div" #js{:className "foo"} "bar"))))
|
|
|
|
|
|
|
|
(is (= (rstr (ae [:div [:div "foo"]]))
|
|
|
|
(rstr (ce "div" nil (ce "div" nil "foo")))))
|
|
|
|
(is (= (rstr (ae [:div [:div "foo"]]))
|
|
|
|
(rstr (ce "div" nil (ae [:div "foo"])))))
|
|
|
|
(is (= (rstr (ae [:div [:div "foo"]]))
|
|
|
|
(rstr (ae [:div (ce "div" nil "foo")]))))))
|
|
|
|
|
2015-02-02 14:19:43 +00:00
|
|
|
(def ndiv (.' js/React
|
|
|
|
createClass
|
|
|
|
#js{:render
|
|
|
|
(fn []
|
|
|
|
(this-as
|
|
|
|
this
|
2015-07-31 13:13:27 +00:00
|
|
|
(r/create-element
|
2015-02-02 14:19:43 +00:00
|
|
|
"div" #js{:className (.' this :props.className)}
|
|
|
|
(.' this :props.children))))}))
|
|
|
|
|
|
|
|
(deftest test-adapt-class
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [d1 (r/adapt-react-class ndiv)
|
|
|
|
d2 (r/adapt-react-class "div")]
|
2015-02-02 14:19:43 +00:00
|
|
|
(is (= (rstr [:div])
|
|
|
|
(rstr [d1])))
|
|
|
|
(is (= (rstr [:div "a"])
|
|
|
|
(rstr [d1 "a"])))
|
|
|
|
(is (= (rstr [:div "a" "b"])
|
|
|
|
(rstr [d1 "a" "b"])))
|
|
|
|
(is (= (rstr [:div.foo "a"])
|
|
|
|
(rstr [d1 {:class "foo"} "a"])))
|
|
|
|
(is (= (rstr [:div "a" "b" [:div "c"]])
|
|
|
|
(rstr [d1 "a" "b" [:div "c"]])))
|
|
|
|
|
|
|
|
(is (= (rstr [:div])
|
|
|
|
(rstr [d2])))
|
|
|
|
(is (= (rstr [:div "a"])
|
|
|
|
(rstr [d2 "a"])))
|
|
|
|
(is (= (rstr [:div "a" "b"])
|
|
|
|
(rstr [d2 "a" "b"])))
|
|
|
|
(is (= (rstr [:div.foo "a"])
|
|
|
|
(rstr [d2 {:class "foo"} "a"])))
|
|
|
|
(is (= (rstr [:div "a" "b" [:div "c"]])
|
|
|
|
(rstr [d2 "a" "b" [:div "c"]])))))
|
2015-02-07 23:01:31 +00:00
|
|
|
|
|
|
|
(deftest test-reactize-component
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [ae r/as-element
|
|
|
|
ce r/create-element
|
2015-02-07 23:01:31 +00:00
|
|
|
c1r (fn [p]
|
|
|
|
[:p "p:" (:a p) (:children p)])
|
2015-07-31 13:13:27 +00:00
|
|
|
c1 (r/reactify-component c1r)]
|
2015-02-07 23:01:31 +00:00
|
|
|
(is (= (rstr [:p "p:a"])
|
|
|
|
(rstr (ce c1 #js{:a "a"}))))
|
|
|
|
(is (= (rstr [:p "p:"])
|
|
|
|
(rstr (ce c1 #js{:a nil}))))
|
|
|
|
(is (= (rstr [:p "p:"])
|
|
|
|
(rstr (ce c1 nil))))
|
|
|
|
|
|
|
|
(is (= (rstr [:p "p:a" [:b "b"]])
|
|
|
|
(rstr (ce c1 #js{:a "a"}
|
|
|
|
(ae [:b "b"])))))
|
|
|
|
(is (= (rstr [:p "p:a" [:b "b"] [:i "i"]])
|
|
|
|
(rstr (ce c1 #js{:a "a"}
|
|
|
|
(ae [:b "b"])
|
|
|
|
(ae [:i "i"])))))))
|
2015-02-08 11:24:52 +00:00
|
|
|
|
|
|
|
(deftest test-keys
|
2015-07-31 13:13:27 +00:00
|
|
|
(let [a nil ;; (r/atom "a")
|
2015-02-08 11:24:52 +00:00
|
|
|
c (fn key-tester []
|
|
|
|
[:div
|
|
|
|
(for [i (range 3)]
|
|
|
|
^{:key i} [:p i (some-> a deref)])
|
|
|
|
(for [i (range 3)]
|
|
|
|
[:p {:key i} i (some-> a deref)])])]
|
|
|
|
(with-mounted-component [c]
|
|
|
|
(fn [c div]
|
|
|
|
;; Just make sure this doesn't print a debug message
|
|
|
|
))))
|
2015-07-30 18:26:16 +00:00
|
|
|
|
|
|
|
(deftest test-extended-syntax
|
|
|
|
(is (= (rstr [:p>b "foo"])
|
|
|
|
"<p><b>foo</b></p>"))
|
|
|
|
(is (= (rstr [:p.foo>b "x"])
|
|
|
|
(rstr [:p.foo [:b "x"]])))
|
|
|
|
(is (= (rstr [:div.foo>p.bar.foo>b.foobar "xy"])
|
|
|
|
(rstr [:div.foo [:p.bar.foo [:b.foobar "xy"]]])))
|
|
|
|
(is (= (rstr [:div.foo>p.bar.foo>b.foobar {} "xy"])
|
|
|
|
(rstr [:div.foo [:p.bar.foo [:b.foobar "xy"]]])))
|
|
|
|
(is (= (rstr [:div>p.bar.foo>a.foobar {:href "href"} "xy"])
|
|
|
|
(rstr [:div [:p.bar.foo [:a.foobar {:href "href"} "xy"]]]))))
|
2015-08-20 12:59:13 +00:00
|
|
|
|
|
|
|
(deftest test-force-update
|
|
|
|
(let [v (atom {:v1 0
|
|
|
|
:v2 0})
|
|
|
|
comps (atom {})
|
|
|
|
c1 (fn []
|
|
|
|
(swap! comps assoc :c1 (r/current-component))
|
|
|
|
[:p (swap! v update-in [:v1] inc)])
|
|
|
|
c2 (fn []
|
|
|
|
(swap! comps assoc :c2 (r/current-component))
|
|
|
|
[:p (swap! v update-in [:v2] inc)
|
|
|
|
[c1]])]
|
|
|
|
(with-mounted-component [c2]
|
|
|
|
(fn [c div]
|
|
|
|
(is (= @v {:v1 1 :v2 1}))
|
|
|
|
|
|
|
|
(r/force-update (:c2 @comps))
|
|
|
|
(is (= @v {:v1 1 :v2 2}))
|
|
|
|
|
|
|
|
(r/force-update (:c1 @comps))
|
|
|
|
(is (= @v {:v1 2 :v2 2}))
|
|
|
|
|
|
|
|
(r/force-update (:c2 @comps) true)
|
|
|
|
(is (= @v {:v1 3 :v2 3}))))))
|
2015-08-30 16:29:27 +00:00
|
|
|
|
|
|
|
(deftest test-component-path
|
|
|
|
(let [a (atom nil)
|
|
|
|
tc (r/create-class {:display-name "atestcomponent"
|
|
|
|
:render (fn []
|
|
|
|
(let [c (r/current-component)]
|
|
|
|
(reset! a (r/component-path c))
|
|
|
|
[:div]))})]
|
|
|
|
(with-mounted-component [tc]
|
|
|
|
(fn [c]
|
|
|
|
(is (seq @a))
|
|
|
|
(is (re-find #"atestcomponent" @a) "component-path should work")))))
|
2015-08-31 06:18:45 +00:00
|
|
|
|
|
|
|
(deftest test-sorted-map-key
|
|
|
|
(let [c1 (fn [map]
|
|
|
|
[:div (map 1)])
|
|
|
|
c2 (fn []
|
|
|
|
[c1 (sorted-map 1 "foo" 2 "bar")])]
|
|
|
|
(is (= (rstr [c2]) "<div>foo</div>"))))
|
2015-09-23 08:39:49 +00:00
|
|
|
|
|
|
|
(deftest basic-with-let
|
2015-09-25 09:48:11 +00:00
|
|
|
(when isClient
|
|
|
|
(let [n1 (atom 0)
|
|
|
|
n2 (atom 0)
|
|
|
|
n3 (atom 0)
|
|
|
|
val (r/atom 0)
|
|
|
|
c (fn []
|
|
|
|
(r/with-let [v (swap! n1 inc)]
|
|
|
|
(swap! n2 inc)
|
|
|
|
[:div @val]
|
|
|
|
(finally
|
|
|
|
(swap! n3 inc))))]
|
|
|
|
(with-mounted-component [c]
|
|
|
|
(fn [_ div]
|
|
|
|
(is (= [1 1 0] [@n1 @n2 @n3]))
|
|
|
|
(swap! val inc)
|
|
|
|
(is (= [1 1 0] [@n1 @n2 @n3]))
|
|
|
|
(r/flush)
|
|
|
|
(is (= [1 2 0] [@n1 @n2 @n3]))))
|
|
|
|
(is (= [1 2 1] [@n1 @n2 @n3])))))
|
2015-09-23 13:55:57 +00:00
|
|
|
|
|
|
|
(deftest with-let-destroy-only
|
2015-09-25 09:48:11 +00:00
|
|
|
(when isClient
|
|
|
|
(let [n1 (atom 0)
|
|
|
|
n2 (atom 0)
|
|
|
|
c (fn []
|
|
|
|
(r/with-let []
|
|
|
|
(swap! n1 inc)
|
|
|
|
[:div]
|
|
|
|
(finally
|
|
|
|
(swap! n2 inc))))]
|
|
|
|
(with-mounted-component [c]
|
|
|
|
(fn [_ div]
|
|
|
|
(is (= [1 0] [@n1 @n2]))))
|
|
|
|
(is (= [1 1] [@n1 @n2])))))
|
2015-09-23 13:55:57 +00:00
|
|
|
|
|
|
|
(deftest with-let-non-reactive
|
|
|
|
(let [n1 (atom 0)
|
|
|
|
n2 (atom 0)
|
|
|
|
n3 (atom 0)
|
|
|
|
c (fn []
|
2015-09-24 08:19:30 +00:00
|
|
|
(r/with-let [a (swap! n1 inc)]
|
2015-09-23 13:55:57 +00:00
|
|
|
(swap! n2 inc)
|
|
|
|
[:div a]
|
|
|
|
(finally
|
|
|
|
(swap! n3 inc))))]
|
|
|
|
(is (= (rstr [c]) (rstr [:div 1])))
|
|
|
|
(is (= [1 1 1] [@n1 @n2 @n3]))))
|
2015-09-25 11:44:10 +00:00
|
|
|
|
|
|
|
(deftest lifecycle
|
|
|
|
(let [n1 (atom 0)
|
|
|
|
t (atom 0)
|
|
|
|
res (atom {})
|
|
|
|
add-args (fn [key args]
|
|
|
|
(swap! res assoc key
|
|
|
|
{:at (swap! n1 inc)
|
|
|
|
:args (vec args)}))
|
|
|
|
render (fn [& args]
|
|
|
|
(add-args :render args)
|
|
|
|
[:div (first args)])
|
|
|
|
ls {:get-initial-state
|
|
|
|
(fn [& args]
|
|
|
|
(reset! t (first args))
|
|
|
|
(add-args :initial-state args)
|
|
|
|
{:foo "bar"})
|
|
|
|
:component-will-mount
|
|
|
|
(fn [& args] (add-args :will-mount args))
|
|
|
|
:component-did-mount
|
|
|
|
(fn [& args] (add-args :did-mount args))
|
|
|
|
:should-component-update
|
|
|
|
(fn [& args] (add-args :should-update args) true)
|
|
|
|
:component-will-update
|
|
|
|
(fn [& args] (add-args :will-update args))
|
|
|
|
:component-did-update
|
|
|
|
(fn [& args] (add-args :did-update args))
|
|
|
|
:component-will-unmount
|
|
|
|
(fn [& args] (add-args :will-unmount args))}
|
|
|
|
c1 (r/create-class
|
|
|
|
(assoc ls :reagent-render render))
|
|
|
|
defarg ["a" "b"]
|
|
|
|
arg (r/atom defarg)
|
|
|
|
comp (atom c1)
|
|
|
|
c2 (fn []
|
|
|
|
(apply vector @comp @arg))
|
|
|
|
check (fn []
|
|
|
|
(is (= (:initial-state @res)
|
|
|
|
{:at 1 :args [@t]}))
|
|
|
|
(is (= (:will-mount @res)
|
|
|
|
{:at 2 :args [@t]}))
|
|
|
|
(is (= (:render @res)
|
|
|
|
{:at 3 :args ["a" "b"]}))
|
|
|
|
(is (= (:did-mount @res)
|
|
|
|
{:at 4 :args [@t]}))
|
|
|
|
|
|
|
|
(reset! arg ["a" "c"])
|
|
|
|
(r/flush)
|
|
|
|
(is (= (:should-update @res)
|
|
|
|
{:at 5 :args [@t [@comp "a" "b"] [@comp "a" "c"]]}))
|
|
|
|
(is (= (:will-update @res)
|
|
|
|
{:at 6 :args [@t [@comp "a" "c"]]}))
|
|
|
|
(is (= (:render @res)
|
|
|
|
{:at 7 :args ["a" "c"]}))
|
|
|
|
(is (= (:did-update @res)
|
|
|
|
{:at 8 :args [@t [@comp "a" "b"]]})))]
|
|
|
|
(when isClient
|
|
|
|
(with-mounted-component [c2] check)
|
|
|
|
(is (= (:will-unmount @res)
|
|
|
|
{:at 9 :args [@t]}))
|
|
|
|
|
|
|
|
(reset! comp (with-meta render ls))
|
|
|
|
(reset! arg defarg)
|
|
|
|
(reset! n1 0)
|
|
|
|
(with-mounted-component [c2] check)
|
|
|
|
(is (= (:will-unmount @res)
|
|
|
|
{:at 9 :args [@t]})))))
|