atom -> r/atom everywhere else, for consistency

This commit is contained in:
Dan Holmsand 2015-07-31 15:13:27 +02:00
parent 7c2455f858
commit b2210c2bf3
7 changed files with 133 additions and 133 deletions

View File

@ -1,9 +1,9 @@
(ns reagentdemo.common (ns reagentdemo.common
(:require [reagent.core :as reagent :refer [atom]] (:require [reagent.core :as r]
[reagent.debug :refer-macros [dbg println]])) [reagent.debug :refer-macros [dbg println]]))
(defn demo-component [] (defn demo-component []
(let [showing (atom true)] (let [showing (r/atom true)]
(fn [{:keys [comp src complete no-heading]}] (fn [{:keys [comp src complete no-heading]}]
[:div [:div
(when comp (when comp

View File

@ -1,5 +1,5 @@
(ns reagentdemo.core (ns reagentdemo.core
(:require [reagent.core :as reagent :refer [atom]] (:require [reagent.core :as r]
[reagent.interop :as i :refer-macros [.' .!]] [reagent.interop :as i :refer-macros [.' .!]]
[clojure.string :as string] [clojure.string :as string]
[sitetools.core :as tools :refer [link]] [sitetools.core :as tools :refer [link]]
@ -8,7 +8,7 @@
[reagentdemo.news :as news] [reagentdemo.news :as news]
[reagent.debug :refer-macros [dbg println]])) [reagent.debug :refer-macros [dbg println]]))
(def test-results (atom nil)) (def test-results (r/atom nil))
(def github {:href "https://github.com/reagent-project/reagent"}) (def github {:href "https://github.com/reagent-project/reagent"})

View File

@ -2,7 +2,7 @@
(:require [clojure.string :as string] (:require [clojure.string :as string]
[goog.events :as evt] [goog.events :as evt]
[goog.history.EventType :as hevt] [goog.history.EventType :as hevt]
[reagent.core :as reagent :refer [atom partial]] [reagent.core :as r]
[reagent.debug :refer-macros [dbg log dev?]] [reagent.debug :refer-macros [dbg log dev?]]
[reagent.interop :as i :refer-macros [.' .!]]) [reagent.interop :as i :refer-macros [.' .!]])
(:import [goog History] (:import [goog History]
@ -17,7 +17,7 @@
;;; Configuration ;;; Configuration
(defonce config (atom {:page-map {"index.html" [:div "Empty"]} (defonce config (r/atom {:page-map {"index.html" [:div "Empty"]}
:page-titles {} :page-titles {}
:body [page-content] :body [page-content]
:site-dir "outsite/public" :site-dir "outsite/public"
@ -28,8 +28,8 @@
:main-div "main-content" :main-div "main-content"
:allow-html5-history false})) :allow-html5-history false}))
(defonce page (atom "index.html")) (defonce page (r/atom "index.html"))
(defonce page-state (atom {:has-history false})) (defonce page-state (r/atom {:has-history false}))
(defn register-page (defn register-page
([pageurl comp] ([pageurl comp]
@ -60,7 +60,7 @@
(fn [e] (fn [e]
(.preventDefault e) (.preventDefault e)
(reset! page p) (reset! page p)
(reagent/next-tick (r/next-tick
#(set! (.-scrollTop (.-body js/document)) #(set! (.-scrollTop (.-body js/document))
0))) 0)))
identity)) identity))
@ -84,7 +84,7 @@
(add-watch page ::title-watch (add-watch page ::title-watch
(fn [_ _ _ p] (fn [_ _ _ p]
(when reagent/is-client (when r/is-client
(set! (.-title js/document) (get-title))) (set! (.-title js/document) (get-title)))
;; First title on a page wins ;; First title on a page wins
#_(reset! page-title ""))) #_(reset! page-title "")))
@ -92,7 +92,7 @@
;;; History ;;; History
(defn use-html5-history [] (defn use-html5-history []
(when reagent/is-client (when r/is-client
(let [proto (.' js/window :location.protocol)] (let [proto (.' js/window :location.protocol)]
(and (:allow-html5-history @config) (and (:allow-html5-history @config)
(.isSupported Html5History) (.isSupported Html5History)
@ -125,7 +125,7 @@
(reset! page (if (and tb (== 0 (.indexOf t tb))) (reset! page (if (and tb (== 0 (.indexOf t tb)))
(subs t (count tb)) (subs t (count tb))
t))) t)))
(reagent/flush))) (r/flush)))
(add-watch page ::history (add-watch page ::history
(fn [_ _ oldp newp] (fn [_ _ oldp newp]
(when-not (= oldp newp) (when-not (= oldp newp)
@ -167,7 +167,7 @@
main (str (:js-file c) timestamp) main (str (:js-file c) timestamp)
css-file (:css-file c) css-file (:css-file c)
opt-none (:opt-none c)] opt-none (:opt-none c)]
(reagent/render-to-static-markup (r/render-to-static-markup
[:html [:html
[:head [:head
[:meta {:charset "utf-8"}] [:meta {:charset "utf-8"}]
@ -186,7 +186,7 @@
(defn gen-page [page-name timestamp] (defn gen-page [page-name timestamp]
(reset! page page-name) (reset! page page-name)
(let [b (reagent/render-component-to-string (body))] (let [b (r/render-component-to-string (body))]
(str "<!doctype html>" (str "<!doctype html>"
(html-template {:title (get-title) (html-template {:title (get-title)
:body b :body b
@ -234,7 +234,7 @@
(log "Generating site") (log "Generating site")
(swap! config merge (js->clj opts :keywordize-keys true)) (swap! config merge (js->clj opts :keywordize-keys true))
(let [dir (:site-dir @config) (let [dir (:site-dir @config)
written (atom #{}) written (r/atom #{})
timestamp (str "?" (.' js/Date now)) timestamp (str "?" (.' js/Date now))
one-page (fn [] (first (filter one-page (fn [] (first (filter
(fn [x] (nil? (@written x))) (fn [x] (nil? (@written x)))
@ -250,7 +250,7 @@
(defn start! [site-config] (defn start! [site-config]
(swap! config merge site-config) (swap! config merge site-config)
(when reagent/is-client (when r/is-client
(let [conf (when (exists? js/pageConfig) (let [conf (when (exists? js/pageConfig)
(js->clj js/pageConfig :keywordize-keys true)) (js->clj js/pageConfig :keywordize-keys true))
page-name (:page-name conf)] page-name (:page-name conf)]
@ -260,6 +260,6 @@
(set-start-page page-name)) (set-start-page page-name))
(setup-history page-name) (setup-history page-name)
(set! (.-title js/document) (get-title))) (set! (.-title js/document) (get-title)))
(reagent/render-component (body) (r/render-component (body)
(.' js/document getElementById (.' js/document getElementById
(:main-div @config)))))) (:main-div @config))))))

View File

@ -5,14 +5,14 @@
[reagenttest.testratom] [reagenttest.testratom]
[reagenttest.testwrap] [reagenttest.testwrap]
[cljs.test :as test :include-macros true] [cljs.test :as test :include-macros true]
[reagent.core :as reagent :refer [atom]] [reagent.core :as r]
[reagent.interop :refer-macros [.' .!]] [reagent.interop :refer-macros [.' .!]]
[reagent.debug :refer-macros [dbg log]] [reagent.debug :refer-macros [dbg log]]
[reagentdemo.core :as demo])) [reagentdemo.core :as demo]))
(enable-console-print!) (enable-console-print!)
(def test-results (atom nil)) (def test-results (r/atom nil))
(def test-box-style {:position 'absolute (def test-box-style {:position 'absolute
:margin-left -35 :margin-left -35
@ -40,7 +40,7 @@
(defn run-tests [] (defn run-tests []
(reset! test-results nil) (reset! test-results nil)
(if reagent/is-client (if r/is-client
(js/setTimeout all-tests 100) (js/setTimeout all-tests 100)
(all-tests))) (all-tests)))

View File

@ -2,7 +2,7 @@
(:require [cljs.test :as t :refer-macros [is deftest testing]] (:require [cljs.test :as t :refer-macros [is deftest testing]]
[reagent.ratom :as rv :refer-macros [run! reaction]] [reagent.ratom :as rv :refer-macros [run! reaction]]
[reagent.debug :refer-macros [dbg]] [reagent.debug :refer-macros [dbg]]
[reagent.core :as r :refer [atom]])) [reagent.core :as r]))
;; this repeats all the atom tests but using cursors instead ;; this repeats all the atom tests but using cursors instead
@ -244,8 +244,8 @@
(deftest test-equality (deftest test-equality
(let [a (atom {:foo "bar"}) (let [a (r/atom {:foo "bar"})
a1 (atom {:foo "bar"}) a1 (r/atom {:foo "bar"})
c (r/cursor a [:foo]) c (r/cursor a [:foo])
foo (fn foo (fn
([path] (get-in @a path)) ([path] (get-in @a path))
@ -285,7 +285,7 @@
(is (= @a {:foo "bar" :foobar "foo"})))) (is (= @a {:foo "bar" :foobar "foo"}))))
(deftest test-wrap (deftest test-wrap
(let [a (atom {:foo "bar"}) (let [a (r/atom {:foo "bar"})
w (r/wrap (:foo @a) swap! a assoc :foo)] w (r/wrap (:foo @a) swap! a assoc :foo)]
(is (= @w "bar")) (is (= @w "bar"))
(is (= w (r/wrap "bar" swap! a assoc :foo))) (is (= w (r/wrap "bar" swap! a assoc :foo)))
@ -301,7 +301,7 @@
(deftest cursor-values (deftest cursor-values
(let [test-atom (atom {:a {:b {:c {:d 1}}}}) (let [test-atom (r/atom {:a {:b {:c {:d 1}}}})
test-cursor (r/cursor test-atom [:a :b :c :d]) test-cursor (r/cursor test-atom [:a :b :c :d])
test-cursor2 (r/cursor test-atom []) test-cursor2 (r/cursor test-atom [])
runs (running)] ;; nasty edge case runs (running)] ;; nasty edge case
@ -340,9 +340,9 @@
(deftest cursor-atom-behaviors (deftest cursor-atom-behaviors
(let [test-atom (atom {:a {:b {:c {:d 1}}}}) (let [test-atom (r/atom {:a {:b {:c {:d 1}}}})
test-cursor (r/cursor test-atom [:a :b :c :d]) test-cursor (r/cursor test-atom [:a :b :c :d])
witness (atom nil) witness (r/atom nil)
runs (running)] runs (running)]
;; per the description, reset! should return the new values ;; per the description, reset! should return the new values
(is (= {} (is (= {}
@ -384,9 +384,9 @@
)) ))
(deftest wrap-atom-behaviors (deftest wrap-atom-behaviors
(let [test-atom (atom "foo") (let [test-atom (r/atom "foo")
test-wrap (r/wrap @test-atom reset! test-atom) test-wrap (r/wrap @test-atom reset! test-atom)
witness (atom nil)] witness (r/atom nil)]
;; per the description, reset! should return the new values ;; per the description, reset! should return the new values
(is (= {} (is (= {}
(reset! test-wrap {}))) (reset! test-wrap {})))
@ -418,7 +418,7 @@
)) ))
(deftest test-cursor-swap (deftest test-cursor-swap
(let [a (atom {:b 1}) (let [a (r/atom {:b 1})
b (r/cursor a [:b])] b (r/cursor a [:b])]
(is (= 1 @b)) (is (= 1 @b))
(is (= 2 (swap! b inc))) (is (= 2 (swap! b inc)))
@ -433,7 +433,7 @@
f (fn [] f (fn []
(swap! c assoc :not-pristine true) (swap! c assoc :not-pristine true)
(swap! a update-in [:foo :active?] not)) (swap! a update-in [:foo :active?] not))
spy (atom nil) spy (r/atom nil)
r (run! r (run!
(reset! spy (:active? @c)))] (reset! spy (:active? @c)))]
(is (= @spy false)) (is (= @spy false))

View File

@ -3,13 +3,13 @@
[reagent.ratom :as rv :refer-macros [reaction]] [reagent.ratom :as rv :refer-macros [reaction]]
[reagent.debug :refer-macros [dbg println log]] [reagent.debug :refer-macros [dbg println log]]
[reagent.interop :refer-macros [.' .!]] [reagent.interop :refer-macros [.' .!]]
[reagent.core :as reagent :refer [atom]])) [reagent.core :as r]))
(defn running [] (rv/running)) (defn running [] (rv/running))
(def isClient reagent/is-client) (def isClient r/is-client)
(def rflush reagent/flush) (def rflush r/flush)
(defn add-test-div [name] (defn add-test-div [name]
(let [doc js/document (let [doc js/document
@ -21,9 +21,9 @@
(defn with-mounted-component [comp f] (defn with-mounted-component [comp f]
(when isClient (when isClient
(let [div (add-test-div "_testreagent")] (let [div (add-test-div "_testreagent")]
(let [comp (reagent/render-component comp div #(f comp div))] (let [comp (r/render-component comp div #(f comp div))]
(reagent/unmount-component-at-node div) (r/unmount-component-at-node div)
(reagent/flush) (r/flush)
(.removeChild (.-body js/document) div))))) (.removeChild (.-body js/document) div)))))
(defn found-in [re div] (defn found-in [re div]
@ -35,7 +35,7 @@
(deftest really-simple-test (deftest really-simple-test
(when isClient (when isClient
(let [ran (atom 0) (let [ran (r/atom 0)
really-simple (fn [] really-simple (fn []
(swap! ran inc) (swap! ran inc)
[:div "div in really-simple"])] [:div "div in really-simple"])]
@ -43,24 +43,24 @@
(fn [c div] (fn [c div]
(swap! ran inc) (swap! ran inc)
(is (found-in #"div in really-simple" div)) (is (found-in #"div in really-simple" div))
(reagent/flush) (r/flush)
(is (= 2 @ran)) (is (= 2 @ran))
(reagent/force-update-all) (r/force-update-all)
(is (= 3 @ran)))) (is (= 3 @ran))))
(is (= 3 @ran))))) (is (= 3 @ran)))))
(deftest test-simple-callback (deftest test-simple-callback
(when isClient (when isClient
(let [ran (atom 0) (let [ran (r/atom 0)
comp (reagent/create-class comp (r/create-class
{:component-did-mount #(swap! ran inc) {:component-did-mount #(swap! ran inc)
:render :render
(fn [this] (fn [this]
(let [props (reagent/props this)] (let [props (r/props this)]
(is (map? props)) (is (map? props))
(is (= props ((reagent/argv this) 1))) (is (= props ((r/argv this) 1)))
(is (= 1 (first (reagent/children this)))) (is (= 1 (first (r/children this))))
(is (= 1 (count (reagent/children this)))) (is (= 1 (count (r/children this))))
(swap! ran inc) (swap! ran inc)
[:div (str "hi " (:foo props) ".")]))})] [:div (str "hi " (:foo props) ".")]))})]
(with-mounted-component (comp {:foo "you"} 1) (with-mounted-component (comp {:foo "you"} 1)
@ -71,45 +71,45 @@
(deftest test-state-change (deftest test-state-change
(when isClient (when isClient
(let [ran (atom 0) (let [ran (r/atom 0)
self (atom nil) self (r/atom nil)
comp (reagent/create-class comp (r/create-class
{:get-initial-state (fn [] {:foo "initial"}) {:get-initial-state (fn [] {:foo "initial"})
:reagent-render :reagent-render
(fn [] (fn []
(let [this (reagent/current-component)] (let [this (r/current-component)]
(reset! self this) (reset! self this)
(swap! ran inc) (swap! ran inc)
[:div (str "hi " (:foo (reagent/state this)))]))})] [:div (str "hi " (:foo (r/state this)))]))})]
(with-mounted-component (comp) (with-mounted-component (comp)
(fn [C div] (fn [C div]
(swap! ran inc) (swap! ran inc)
(is (found-in #"hi initial" div)) (is (found-in #"hi initial" div))
(reagent/replace-state @self {:foo "there"}) (r/replace-state @self {:foo "there"})
(reagent/state @self) (r/state @self)
(rflush) (rflush)
(is (found-in #"hi there" div)) (is (found-in #"hi there" div))
(reagent/set-state @self {:foo "you"}) (r/set-state @self {:foo "you"})
(rflush) (rflush)
(is (found-in #"hi you" div)))) (is (found-in #"hi you" div))))
(is (= 4 @ran))))) (is (= 4 @ran)))))
(deftest test-ratom-change (deftest test-ratom-change
(when isClient (when isClient
(let [ran (atom 0) (let [ran (r/atom 0)
runs (running) runs (running)
val (atom 0) val (r/atom 0)
secval (atom 0) secval (r/atom 0)
v1 (reaction @val) v1 (reaction @val)
comp (fn [] comp (fn []
(swap! ran inc) (swap! ran inc)
[:div (str "val " @v1 @val @secval)])] [:div (str "val " @v1 @val @secval)])]
(with-mounted-component [comp] (with-mounted-component [comp]
(fn [C div] (fn [C div]
(reagent/flush) (r/flush)
(is (not= runs (running))) (is (not= runs (running)))
(is (found-in #"val 0" div)) (is (found-in #"val 0" div))
(is (= 1 @ran)) (is (= 1 @ran))
@ -119,13 +119,13 @@
(reset! val 1) (reset! val 1)
(reset! val 2) (reset! val 2)
(reset! val 1) (reset! val 1)
(reagent/flush) (r/flush)
(is (found-in #"val 1" div)) (is (found-in #"val 1" div))
(is (= 2 @ran)) (is (= 2 @ran))
;; should not be rendered ;; should not be rendered
(reset! val 1) (reset! val 1)
(reagent/flush) (r/flush)
(is (found-in #"val 1" div)) (is (found-in #"val 1" div))
(is (= 2 @ran)))) (is (= 2 @ran))))
(is (= runs (running))) (is (= runs (running)))
@ -133,9 +133,9 @@
(deftest batched-update-test [] (deftest batched-update-test []
(when isClient (when isClient
(let [ran (atom 0) (let [ran (r/atom 0)
v1 (atom 0) v1 (r/atom 0)
v2 (atom 0) v2 (r/atom 0)
c2 (fn [{val :val}] c2 (fn [{val :val}]
(swap! ran inc) (swap! ran inc)
(is (= @v1 val)) (is (= @v1 val))
@ -167,14 +167,14 @@
(deftest init-state-test (deftest init-state-test
(when isClient (when isClient
(let [ran (atom 0) (let [ran (r/atom 0)
really-simple (fn [] really-simple (fn []
(let [this (reagent/current-component)] (let [this (r/current-component)]
(swap! ran inc) (swap! ran inc)
(reagent/set-state this {:foo "foobar"}) (r/set-state this {:foo "foobar"})
(fn [] (fn []
[:div (str "this is " [:div (str "this is "
(:foo (reagent/state this)))])))] (:foo (r/state this)))])))]
(with-mounted-component [really-simple nil nil] (with-mounted-component [really-simple nil nil]
(fn [c div] (fn [c div]
(swap! ran inc) (swap! ran inc)
@ -183,9 +183,9 @@
(deftest shoud-update-test (deftest shoud-update-test
(when isClient (when isClient
(let [parent-ran (atom 0) (let [parent-ran (r/atom 0)
child-ran (atom 0) child-ran (r/atom 0)
child-props (atom nil) child-props (r/atom nil)
f (fn []) f (fn [])
f1 (fn []) f1 (fn [])
child (fn [p] child (fn [p]
@ -215,23 +215,23 @@
(is (= @child-ran 4) "symbols are equal") (is (= @child-ran 4) "symbols are equal")
(do (reset! child-props {:style {:color 'red}}) (rflush)) (do (reset! child-props {:style {:color 'red}}) (rflush))
(is (= @child-ran 5)) (is (= @child-ran 5))
(do (reset! child-props {:on-change (reagent/partial f)}) (do (reset! child-props {:on-change (r/partial f)})
(rflush)) (rflush))
(is (= @child-ran 6)) (is (= @child-ran 6))
(do (reset! child-props {:on-change (reagent/partial f)}) (do (reset! child-props {:on-change (r/partial f)})
(rflush)) (rflush))
(is (= @child-ran 6)) (is (= @child-ran 6))
(do (reset! child-props {:on-change (reagent/partial f1)}) (do (reset! child-props {:on-change (r/partial f1)})
(rflush)) (rflush))
(is (= @child-ran 7)) (is (= @child-ran 7))
(reagent/force-update-all) (r/force-update-all)
(is (= @child-ran 8))))))) (is (= @child-ran 8)))))))
(deftest dirty-test (deftest dirty-test
(when isClient (when isClient
(let [ran (atom 0) (let [ran (r/atom 0)
state (atom 0) state (r/atom 0)
really-simple (fn [] really-simple (fn []
(swap! ran inc) (swap! ran inc)
(if (= @state 1) (if (= @state 1)
@ -248,7 +248,7 @@
(is (= 2 @ran))))) (is (= 2 @ran)))))
(defn as-string [comp] (defn as-string [comp]
(reagent/render-component-to-string comp)) (r/render-component-to-string comp))
(deftest to-string-test [] (deftest to-string-test []
(let [comp (fn [props] (let [comp (fn [props]
@ -321,12 +321,12 @@
(as-string ['div#foo.foo.bar])))) (as-string ['div#foo.foo.bar]))))
(deftest partial-test [] (deftest partial-test []
(let [p1 (reagent/partial vector 1 2)] (let [p1 (r/partial vector 1 2)]
(is (= (p1 3) [1 2 3])) (is (= (p1 3) [1 2 3]))
(is (= p1 (reagent/partial vector 1 2))) (is (= p1 (r/partial vector 1 2)))
(is (ifn? p1)) (is (ifn? p1))
(is (= (reagent/partial vector 1 2) p1)) (is (= (r/partial vector 1 2) p1))
(is (not= p1 (reagent/partial vector 1 3))))) (is (not= p1 (r/partial vector 1 3)))))
(deftest test-null-component (deftest test-null-component
(let [null-comp (fn [do-show] (let [null-comp (fn [do-show]
@ -339,34 +339,34 @@
(deftest test-static-markup (deftest test-static-markup
(is (= "<div>foo</div>" (is (= "<div>foo</div>"
(reagent/render-to-static-markup (r/render-to-static-markup
[:div "foo"]))) [:div "foo"])))
(is (= "<div class=\"bar\"><p>foo</p></div>" (is (= "<div class=\"bar\"><p>foo</p></div>"
(reagent/render-to-static-markup (r/render-to-static-markup
[:div.bar [:p "foo"]]))) [:div.bar [:p "foo"]])))
(is (= "<div class=\"bar\"><p>foobar</p></div>" (is (= "<div class=\"bar\"><p>foobar</p></div>"
(reagent/render-to-static-markup (r/render-to-static-markup
[:div.bar {:dangerously-set-inner-HTML [:div.bar {:dangerously-set-inner-HTML
{:__html "<p>foobar</p>"}} ])))) {:__html "<p>foobar</p>"}} ]))))
(deftest test-return-class (deftest test-return-class
(when isClient (when isClient
(let [ran (atom 0) (let [ran (r/atom 0)
top-ran (atom 0) top-ran (r/atom 0)
comp (fn [] comp (fn []
(swap! top-ran inc) (swap! top-ran inc)
(reagent/create-class (r/create-class
{:component-did-mount #(swap! ran inc) {:component-did-mount #(swap! ran inc)
:render :render
(fn [this] (fn [this]
(let [props (reagent/props this)] (let [props (r/props this)]
(is (map? props)) (is (map? props))
(is (= props ((reagent/argv this) 1))) (is (= props ((r/argv this) 1)))
(is (= 1 (first (reagent/children this)))) (is (= 1 (first (r/children this))))
(is (= 1 (count (reagent/children this)))) (is (= 1 (count (r/children this))))
(swap! ran inc) (swap! ran inc)
[:div (str "hi " (:foo props) ".")]))})) [:div (str "hi " (:foo props) ".")]))}))
prop (atom {:foo "you"}) prop (r/atom {:foo "you"})
parent (fn [] [comp @prop 1])] parent (fn [] [comp @prop 1])]
(with-mounted-component [parent] (with-mounted-component [parent]
(fn [C div] (fn [C div]
@ -376,25 +376,25 @@
(is (= 3 @ran)) (is (= 3 @ran))
(swap! prop assoc :foo "me") (swap! prop assoc :foo "me")
(reagent/flush) (r/flush)
(is (found-in #"hi me" div)) (is (found-in #"hi me" div))
(is (= 1 @top-ran)) (is (= 1 @top-ran))
(is (= 4 @ran))))))) (is (= 4 @ran)))))))
(deftest test-return-class-fn (deftest test-return-class-fn
(when isClient (when isClient
(let [ran (atom 0) (let [ran (r/atom 0)
top-ran (atom 0) top-ran (r/atom 0)
comp (fn [] comp (fn []
(swap! top-ran inc) (swap! top-ran inc)
(reagent/create-class (r/create-class
{:component-did-mount #(swap! ran inc) {:component-did-mount #(swap! ran inc)
:component-function :component-function
(fn [p a] (fn [p a]
(is (= 1 a)) (is (= 1 a))
(swap! ran inc) (swap! ran inc)
[:div (str "hi " (:foo p) ".")])})) [:div (str "hi " (:foo p) ".")])}))
prop (atom {:foo "you"}) prop (r/atom {:foo "you"})
parent (fn [] [comp @prop 1])] parent (fn [] [comp @prop 1])]
(with-mounted-component [parent] (with-mounted-component [parent]
(fn [C div] (fn [C div]
@ -404,17 +404,17 @@
(is (= 3 @ran)) (is (= 3 @ran))
(swap! prop assoc :foo "me") (swap! prop assoc :foo "me")
(reagent/flush) (r/flush)
(is (found-in #"hi me" div)) (is (found-in #"hi me" div))
(is (= 1 @top-ran)) (is (= 1 @top-ran))
(is (= 4 @ran))))))) (is (= 4 @ran)))))))
(defn rstr [react-elem] (defn rstr [react-elem]
(reagent/render-to-static-markup react-elem)) (r/render-to-static-markup react-elem))
(deftest test-create-element (deftest test-create-element
(let [ae reagent/as-element (let [ae r/as-element
ce reagent/create-element] ce r/create-element]
(is (= (rstr (ae [:div])) (is (= (rstr (ae [:div]))
(rstr (ce "div")))) (rstr (ce "div"))))
(is (= (rstr (ae [:div])) (is (= (rstr (ae [:div]))
@ -442,13 +442,13 @@
(fn [] (fn []
(this-as (this-as
this this
(reagent/create-element (r/create-element
"div" #js{:className (.' this :props.className)} "div" #js{:className (.' this :props.className)}
(.' this :props.children))))})) (.' this :props.children))))}))
(deftest test-adapt-class (deftest test-adapt-class
(let [d1 (reagent/adapt-react-class ndiv) (let [d1 (r/adapt-react-class ndiv)
d2 (reagent/adapt-react-class "div")] d2 (r/adapt-react-class "div")]
(is (= (rstr [:div]) (is (= (rstr [:div])
(rstr [d1]))) (rstr [d1])))
(is (= (rstr [:div "a"]) (is (= (rstr [:div "a"])
@ -472,11 +472,11 @@
(rstr [d2 "a" "b" [:div "c"]]))))) (rstr [d2 "a" "b" [:div "c"]])))))
(deftest test-reactize-component (deftest test-reactize-component
(let [ae reagent/as-element (let [ae r/as-element
ce reagent/create-element ce r/create-element
c1r (fn [p] c1r (fn [p]
[:p "p:" (:a p) (:children p)]) [:p "p:" (:a p) (:children p)])
c1 (reagent/reactify-component c1r)] c1 (r/reactify-component c1r)]
(is (= (rstr [:p "p:a"]) (is (= (rstr [:p "p:a"])
(rstr (ce c1 #js{:a "a"})))) (rstr (ce c1 #js{:a "a"}))))
(is (= (rstr [:p "p:"]) (is (= (rstr [:p "p:"])
@ -493,7 +493,7 @@
(ae [:i "i"]))))))) (ae [:i "i"])))))))
(deftest test-keys (deftest test-keys
(let [a nil ;; (atom "a") (let [a nil ;; (r/atom "a")
c (fn key-tester [] c (fn key-tester []
[:div [:div
(for [i (range 3)] (for [i (range 3)]

View File

@ -1,7 +1,7 @@
(ns reagenttest.testwrap (ns reagenttest.testwrap
(:require [cljs.test :as t :refer-macros [is deftest]] (:require [cljs.test :as t :refer-macros [is deftest]]
[reagent.debug :refer-macros [dbg println log]] [reagent.debug :refer-macros [dbg println log]]
[reagent.core :as r :refer [atom wrap]])) [reagent.core :as r]))
(defn add-test-div [name] (defn add-test-div [name]
(let [doc js/document (let [doc js/document
@ -26,7 +26,7 @@
false)))) false))))
(deftest test-wrap-basic (deftest test-wrap-basic
(let [state (atom {:foo 1}) (let [state (r/atom {:foo 1})
ws (fn [] (r/wrap (:foo @state) ws (fn [] (r/wrap (:foo @state)
swap! state assoc :foo))] swap! state assoc :foo))]
(let [w1 (ws) w2 (ws)] (let [w1 (ws) w2 (ws)]
@ -72,8 +72,8 @@
(reset! w1 1)))) (reset! w1 1))))
(deftest test-wrap-equality (deftest test-wrap-equality
(let [a (atom 1) (let [a (r/atom 1)
b (atom 2)] b (r/atom 2)]
(is (= (r/wrap @a swap! a assoc :foo) (is (= (r/wrap @a swap! a assoc :foo)
(r/wrap @a swap! a assoc :foo))) (r/wrap @a swap! a assoc :foo)))
(is (not= (r/wrap @a swap! a assoc :foo) (is (not= (r/wrap @a swap! a assoc :foo)
@ -98,8 +98,8 @@
(deftest test-wrap-returns (deftest test-wrap-returns
(let [n (fn [] :foobar) (let [n (fn [] :foobar)
a (atom {:k 1}) a (r/atom {:k 1})
b (wrap {:k 1} n)] b (r/wrap {:k 1} n)]
(is (not= a b)) (is (not= a b))
(is (not= b a)) (is (not= b a))
(is (= (swap! a update-in [:k] inc) (is (= (swap! a update-in [:k] inc)
@ -114,18 +114,18 @@
(deftest test-wrap (deftest test-wrap
(when r/is-client (when r/is-client
(let [state (atom {:foo {:bar {:foobar 1}}}) (let [state (r/atom {:foo {:bar {:foobar 1}}})
ran (atom 0) ran (r/atom 0)
grand-state (clojure.core/atom nil) grand-state (clojure.core/atom nil)
grand-child (fn [v] grand-child (fn [v]
(swap! ran inc) (swap! ran inc)
(reset! grand-state v) (reset! grand-state v)
[:div (str "value:" (:foobar @v) ":")]) [:div (str "value:" (:foobar @v) ":")])
child (fn [v] child (fn [v]
[grand-child (wrap (:bar @v) [grand-child (r/wrap (:bar @v)
swap! v assoc :bar)]) swap! v assoc :bar)])
parent (fn [] parent (fn []
[child (wrap (:foo @state) [child (r/wrap (:foo @state)
swap! state assoc :foo)])] swap! state assoc :foo)])]
(with-mounted-component [parent] (with-mounted-component [parent]
(fn [c div] (fn [c div]
@ -175,10 +175,10 @@
(is (= @ran 7))))))) (is (= @ran 7)))))))
(deftest test-cursor (deftest test-cursor
(let [state (atom {:a 0 (let [state (r/atom {:a 0
:b 0}) :b 0})
a-count (atom 0) a-count (r/atom 0)
b-count (atom 0) b-count (r/atom 0)
derefer (fn derefer [cur count] derefer (fn derefer [cur count]
(swap! count inc) (swap! count inc)
[:div @cur]) [:div @cur])