reagent/test/testcloact.cljs

140 lines
4.1 KiB
Plaintext
Raw Normal View History

2013-12-16 22:19:36 +00:00
(ns testcloact
(:require-macros [cemerick.cljs.test
:refer (is deftest with-test run-tests testing)]
[cloact.ratom :refer [reaction]]
2013-12-18 08:13:16 +00:00
[cloact.debug :refer [dbg println log]])
2013-12-16 22:19:36 +00:00
(:require [cemerick.cljs.test :as t]
[cloact.core :as r :refer [atom]]
[cloact.ratom :as rv]))
(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
(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
(let [div (add-test-div "_testcloact")]
(let [comp (r/render-component comp div #(f comp div))]
(r/unmount-component-at-node div)))))
(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)
comp (r/create-class
{:component-did-mount #(swap! ran inc)
:render (fn [P C]
(assert (map? P))
(swap! ran inc)
[:div (str "hi " (:foo P) ".")])})]
(with-mounted-component (comp {:foo "you"})
(fn [C div]
(swap! ran inc)
(is (found-in #"hi you" div))
2013-12-16 22:19:36 +00:00
(r/set-props C {:foo "there"})
(is (found-in #"hi there" div))
2013-12-18 11:14:57 +00:00
(let [runs @ran]
(r/set-props C {:foo "there"})
(is (found-in #"hi there" div))
(is (= runs @ran)))
(r/replace-props C {:foobar "not used"})
(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)
comp (r/create-class
{:get-initial-state (fn [])
:render (fn [P C]
(swap! ran inc)
[:div (str "hi " (:foo @C))])})]
(with-mounted-component (comp)
(fn [C div]
(swap! ran inc)
(is (found-in #"hi " div))
(swap! C assoc :foo "there")
(is (found-in #"hi there" div))
(swap! C assoc :foo "you")
(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)
v1 (reaction @val)
comp (fn []
(swap! ran inc)
[:div (str "val " @v1)])]
(with-mounted-component [comp]
(fn [C div]
(swap! ran inc)
(is (not= runs (running)))
(is (found-in #"val 0" div))
(is (= 2 @ran))
(reset! val 1)
(is (found-in #"val 1" div))
(is (= 3 @ran))
;; should not be rendered
(reset! val 1)
(is (found-in #"val 1" div))
(is (= 3 @ran))))
(is (= runs (running)))
(is (= 3 @ran)))))
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
(let [ran (atom 0)
really-simple (fn [props this]
(swap! ran inc)
(swap! this assoc :foo "foobar")
(fn []
[:div (str "this is " (:foo @this))]))]
(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
(deftest to-string-test []
(let [comp (fn [props]
[:div (str "i am " (:foo props))])]
(is (re-find #"i am foobar"
(r/render-component-to-string
[comp {:foo "foobar"}])))))