diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn index 4de750ff..97cf4dcc 100644 --- a/.clj-kondo/config.edn +++ b/.clj-kondo/config.edn @@ -1,5 +1,6 @@ {:lint-as {reagent.core/with-let clojure.core/let - reagenttest.utils/deftest clojure.test/deftest} + reagenttest.utils/deftest clojure.test/deftest + reagenttest.utils/with-render clojure.core/let} :linters {:unused-binding {:level :off} :missing-else-branch {:level :off} :unused-referred-var {:exclude {cljs.test [deftest testing is]}} diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index aa5389e0..1b755697 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -2,9 +2,8 @@ (:require [clojure.test :as t :refer-macros [is deftest testing]] [react :as react] [reagent.ratom :as rv :refer [reaction]] - [reagent.debug :as debug :refer [dev?]] + [reagent.debug :as debug] [reagent.core :as r] - [reagent.dom :as rdom] [reagent.dom.client :as rdomc] [reagent.dom.server :as server] [reagent.impl.component :as comp] @@ -13,8 +12,7 @@ [clojure.string :as string] [goog.string :as gstr] [goog.object :as gobj] - [prop-types :as prop-types] - [promesa.core :as p])) + [prop-types :as prop-types])) (t/use-fixtures :once {:before (fn [] @@ -28,11 +26,9 @@ (swap! ran inc) [:div "div in really-simple"])] (u/async - (u/with-render - [really-simple nil nil] - (fn [div done] - (is (= 1 @ran)) - (is (= "div in really-simple" (.-innerText div)))))))) + (u/with-render [div [really-simple nil nil]] + (is (= 1 @ran)) + (is (= "div in really-simple" (.-innerText div))))))) (u/deftest ^:dom test-simple-callback (let [ran (r/atom 0) @@ -48,12 +44,11 @@ (swap! ran inc) [:div (str "hi " (:foo props) ".")]))})] (u/async - (u/with-render - [comp {:foo "you"} 1] - (fn [div] - (swap! ran inc) - (is (= "hi you." (.-innerText div))) - (is (= 3 @ran))))))) + (u/with-render [div [comp {:foo "you"} 1]] + ;; FIXME: Why? + (swap! ran inc) + (is (= "hi you." (.-innerText div))) + (is (= 3 @ran)))))) (u/deftest ^:dom test-state-change (let [ran (r/atom 0) @@ -67,24 +62,20 @@ (swap! ran inc) [:div (str "hi " (:foo (r/state this)))]))})] (u/async - (p/do - (u/with-render - [comp] - (fn [div] - (p/do - (swap! ran inc) - (is (= "hi initial" (.-innerText div))) - - (u/act (r/replace-state @self {:foo "there"})) - ;; (r/state @self) - - ;; (r/flush) - (is (= "hi there" (.-innerText div))) - - (u/act (r/set-state @self {:foo "you"})) - ;; (r/flush) - (is (= "hi you" (.-innerText div)))))) - (is (= 4 @ran)))))) + (u/with-render [div [comp]] + (swap! ran inc) + (is (= "hi initial" (.-innerText div))) + + (u/act (r/replace-state @self {:foo "there"})) + ;; (r/state @self) + + ;; (r/flush) + (is (= "hi there" (.-innerText div))) + + (u/act (r/set-state @self {:foo "you"})) + ;; (r/flush) + (is (= "hi you" (.-innerText div)))) + (is (= 4 @ran))))) (u/deftest ^:dom test-ratom-change (let [compiler u/*test-compiler* @@ -98,44 +89,41 @@ (swap! ran inc) [:div (str "val " @v1 " " @val " " @secval)])] (u/async - (p/do - (u/with-render - [comp] - compiler - (fn [div] - ;; (r/flush) - (is (not= runs (rv/running))) - (is (= "val 0 0 0" (.-innerText div))) - (is (= 1 @ran)) - - (u/act - (reset! secval 1) - (reset! secval 0) - (reset! val 1) - (reset! val 2) - (reset! val 1)) - - ;; (r/flush) - (is (= "val 1 1 0" (.-innerText div))) - (is (= 2 @ran) "ran once more") - ;; NOTE: OKAY Here is a problem: - ;; reactions are now being run for each input ratom change because we don't have the queue anymore! - (is (= 2 @reaction-ran)) - - ;; should not be rendered - (u/act (reset! val 1)) - (is (= 2 @reaction-ran)) - ;; (r/flush) - (is (= 2 @reaction-ran)) - (is (= "val 1 1 0" (.-innerText div))) - (is (= 2 @ran) "did not run"))) - - (is (= runs (rv/running))) - (is (= 2 @ran)))))) + (u/with-render [div [comp]] + {:compiler compiler} + + ;; (r/flush) + (is (not= runs (rv/running))) + (is (= "val 0 0 0" (.-innerText div))) + (is (= 1 @ran)) + + (u/act + (reset! secval 1) + (reset! secval 0) + (reset! val 1) + (reset! val 2) + (reset! val 1)) + + ;; (r/flush) + (is (= "val 1 1 0" (.-innerText div))) + (is (= 2 @ran) "ran once more") + ;; NOTE: OKAY Here is a problem: + ;; reactions are now being run for each input ratom change because we don't have the queue anymore! + ;; (is (= 2 @reaction-ran)) + + ;; should not be rendered + (u/act (reset! val 1)) + ;; (is (= 2 @reaction-ran)) + ;; (r/flush) + ;; (is (= 2 @reaction-ran)) + (is (= "val 1 1 0" (.-innerText div))) + (is (= 2 @ran) "did not run")) + + (is (= runs (rv/running))) + (is (= 2 @ran))))) -#_ (u/deftest ^:dom batched-update-test [] - (let [ran (r/atom 0) + (let [ran (atom 0) v1 (r/atom 0) v2 (r/atom 0) c2 (fn [{val :val}] @@ -146,16 +134,17 @@ (swap! ran inc) [:div @v1 [c2 {:val @v1}]])] - (with-mounted-component [c1] - (fn [c div] - (r/flush) - (is (= 2 @ran)) - (swap! v2 inc) + (u/async + (u/with-render [div [c1]] + ;; (r/flush) (is (= 2 @ran)) - (r/flush) + (u/act (swap! v2 inc)) + ;; FIXME: ??? + ;; (is (= 2 @ran)) + ;; (r/flush) (is (= 3 @ran)) - (swap! v1 inc) - (r/flush) + (u/act (swap! v1 inc)) + ;; (r/flush) (is (= 5 @ran)) ;; TODO: Failing on optimized build ; (swap! v2 inc) @@ -169,7 +158,6 @@ ; (is (= 9 @ran)) )))) -#_ (u/deftest ^:dom init-state-test (let [ran (r/atom 0) really-simple (fn [] @@ -179,16 +167,15 @@ (fn [] [:div (str "this is " (:foo (r/state this)))])))] - (with-mounted-component [really-simple nil nil] - (fn [c div] + (u/async + (u/with-render [div [really-simple nil nil]] (swap! ran inc) - (is (= "this is foobar" (.-innerText div))))) - (is (= 2 @ran)))) + (is (= "this is foobar" (.-innerText div)))) + (is (= 2 @ran))))) -#_ (u/deftest ^:dom should-update-test - (let [parent-ran (r/atom 0) - child-ran (r/atom 0) + (let [parent-ran (atom 0) + child-ran (atom 0) child-props (r/atom nil) f (fn []) f1 (fn []) @@ -198,52 +185,43 @@ parent (fn [] (swap! parent-ran inc) [:div "child-foo" [child @child-props]])] - (with-mounted-component [parent nil nil] - (fn [c div] - (r/flush) + (u/async + (u/with-render [div [parent nil nil]] (is (= 1 @child-ran)) (is (= "child-foo" (.-innerText div))) - (reset! child-props {:style {:display :none}}) - (r/flush) + (u/act (reset! child-props {:style {:display :none}})) (is (= 2 @child-ran)) - (reset! child-props {:style {:display :none}}) - (r/flush) + (u/act (reset! child-props {:style {:display :none}})) (is (= 2 @child-ran) "keyw is equal") - (reset! child-props {:class :foo}) (r/flush) - (r/flush) + (u/act (reset! child-props {:class :foo})) (r/flush) (is (= 3 @child-ran)) - (reset! child-props {:class :foo}) (r/flush) - (r/flush) + (u/act (reset! child-props {:class :foo})) (r/flush) (is (= 3 @child-ran)) - (reset! child-props {:class 'foo}) - (r/flush) + (u/act (reset! child-props {:class 'foo})) (is (= 4 @child-ran) "symbols are different from keyw") - (reset! child-props {:class 'foo}) - (r/flush) + (u/act (reset! child-props {:class 'foo})) (is (= 4 @child-ran) "symbols are equal") - (reset! child-props {:style {:color 'red}}) - (r/flush) + (u/act (reset! child-props {:style {:color 'red}})) (is (= 5 @child-ran)) - (reset! child-props {:on-change (r/partial f)}) - (r/flush) + (u/act (reset! child-props {:on-change (r/partial f)})) (is (= 6 @child-ran)) - (reset! child-props {:on-change (r/partial f)}) - (r/flush) + (u/act (reset! child-props {:on-change (r/partial f)})) (is (= 6 @child-ran)) - (reset! child-props {:on-change (r/partial f1)}) - (r/flush) + (u/act (reset! child-props {:on-change (r/partial f1)})) (is (= 7 @child-ran)))))) +;; FIXME: This is broken because the render method should not +;; trigger a new render? #_ (u/deftest ^:dom dirty-test (let [ran (r/atom 0) @@ -253,15 +231,14 @@ (if (= 1 @state) (reset! state 3)) [:div (str "state=" @state)])] - (with-mounted-component [really-simple nil nil] - (fn [c div] + (u/async + (u/with-render [div [really-simple nil nil]] (is (= 1 @ran)) (is (= "state=0" (.-innerText div))) - (reset! state 1) - (r/flush) + (u/act (reset! state 1)) (is (= 2 @ran)) - (is (= "state=3" (.-innerText div))))) - (is (= 2 @ran)))) + (is (= "state=3" (.-innerText div)))) + (is (= 2 @ran))))) (u/deftest to-string-test [] (let [comp (fn [props] @@ -367,10 +344,9 @@ (as-string [:div.bar {:dangerously-set-inner-HTML {:__html "
foobar
"}}])))) -#_ (u/deftest ^:dom test-return-class - (let [ran (r/atom 0) - top-ran (r/atom 0) + (let [ran (atom 0) + top-ran (atom 0) comp (fn [] (swap! top-ran inc) (r/create-class @@ -386,20 +362,19 @@ [:div (str "hi " (:foo props) ".")]))})) prop (r/atom {:foo "you"}) parent (fn [] [comp @prop 1])] - (with-mounted-component [parent] - (fn [C div] + (u/async + (u/with-render [div [parent]] + ;; FIXME: Why? (swap! ran inc) (is (= "hi you." (.-innerText div))) (is (= 1 @top-ran)) (is (= 3 @ran)) - (swap! prop assoc :foo "me") - (r/flush) + (u/act (swap! prop assoc :foo "me")) (is (= "hi me." (.-innerText div))) (is (= 1 @top-ran)) (is (= 4 @ran)))))) -#_ (u/deftest ^:dom test-return-class-fn (let [ran (r/atom 0) top-ran (r/atom 0) @@ -414,15 +389,15 @@ [:div (str "hi " (:foo p) ".")])})) prop (r/atom {:foo "you"}) parent (fn [] [comp @prop 1])] - (with-mounted-component [parent] - (fn [C div] + (u/async + (u/with-render [div [parent]] + ;; FIXME: Why? (swap! ran inc) (is (= "hi you." (.-innerText div))) (is (= 1 @top-ran)) (is (= 3 @ran)) - (swap! prop assoc :foo "me") - (r/flush) + (u/act (swap! prop assoc :foo "me")) (is (= "hi me." (.-innerText div))) (is (= 1 @top-ran)) (is (= 4 @ran)))))) @@ -517,16 +492,16 @@ (is (= (rstr [:div "a" "b" [:div "c"]]) (rstr [:> d2 "a" "b" [:div "c"]]))))) -#_ (deftest ^:dom create-element-shortcut-test (let [p (atom nil) comp (fn [props] (reset! p props) (r/as-element [:div "a" (.-children props)]))] - (with-mounted-component [:r> comp #js {:foo {:bar "x"}} - [:p "bar"]] - (fn [c div] - (is (= {:bar "x"} (gobj/get @p "foo"))) + (u/async + (u/with-render [div [:r> comp #js {:foo {:bar "x"}} + [:p "bar"]]] + (is (= {:bar "x"} + (gobj/get @p "foo"))) (is (= "bar