Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Nov 15, 2024
1 parent 85137e2 commit 87d6765
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 56 deletions.
28 changes: 13 additions & 15 deletions test/reagenttest/testreagent.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
[really-simple nil nil]
(fn [div done]
(is (= 1 @ran))
(is (= "div in really-simple" (.-innerText div)))
(js/Promise.resolve nil))))))
(is (= "div in really-simple" (.-innerText div))))))))

#_
(u/deftest ^:dom test-simple-callback
(let [ran (r/atom 0)
comp (r/create-class
Expand All @@ -48,11 +46,13 @@
(is (= 1 (count (r/children this))))
(swap! ran inc)
[:div (str "hi " (:foo props) ".")]))})]
(with-mounted-component [comp {:foo "you"} 1]
(fn [C div]
(swap! ran inc)
(is (= "hi you." (.-innerText div)))
(is (= 3 @ran))))))
(u/async
(u/with-render
[comp {:foo "you"} 1]
(fn [div]
(swap! ran inc)
(is (= "hi you." (.-innerText div)))
(is (= 3 @ran)))))))

#_
(u/deftest ^:dom test-state-change
Expand Down Expand Up @@ -1466,27 +1466,25 @@
[:f> c "foo"]
u/class-compiler
(fn [div]
(is (= "Hello foo" (.-innerText div)))
(js/Promise.resolve nil))))
(is (= "Hello foo" (.-innerText div))))))

(.then (fn []
(testing "compiler options"
(u/with-render
[c "foo"]
u/fn-compiler
(fn [div]
(is (= "Hello foo" (.-innerText div)))
(js/Promise.resolve nil))))))
(is (= "Hello foo" (.-innerText div))))))))

(.then (fn []
(testing "setting default compiler"
(try
(r/set-default-compiler! u/fn-compiler)
(u/with-render
[c "foo"] nil
[c "foo"]
nil
(fn [div]
(is (= "Hello foo" (.-innerText div)))
(js/Promise.resolve nil)))
(is (= "Hello foo" (.-innerText div)))))
(finally
(r/set-default-compiler! nil))))))))))

Expand Down
10 changes: 5 additions & 5 deletions test/reagenttest/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
(cljs.test/deftest
~(with-meta (symbol (str (name test-name) "--class"))
(meta test-name))
(binding [*test-compiler* class-compiler
*test-compiler-name* "class"]
(binding [reagenttest.utils/*test-compiler* class-compiler
reagenttest.utils/*test-compiler-name* "class"]
~@body))
(cljs.test/deftest
~(with-meta (symbol (str (name test-name) "--fn"))
(meta test-name))
(binding [*test-compiler* fn-compiler
*test-compiler-name* "fn"]
(binding [reagenttest.utils/*test-compiler* fn-compiler
reagenttest.utils/*test-compiler-name* "fn"]
~@body))))

(defmacro act
[& body]
`(act* (fn [] ~@body)))
`(reagenttest.utils/act* (fn [] ~@body)))

;; Inspired by
;; https://github.com/henryw374/Cljs-Async-Timeout-Tests/blob/master/src/widdindustries/timeout_test.cljc
Expand Down
50 changes: 14 additions & 36 deletions test/reagenttest/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
[reagent.dom.server :as server]
[reagent.impl.template :as tmpl]))

;; Silence ReactDOM.render warning
;; Should be only set for tests....
;; (set! (.-IS_REACT_ACT_ENVIRONMENT js/window) true)

(defonce original-console-error (.-error js/console))

(set! (.-error js/console)
(fn [& [first-arg :as args]]
(cond
(and (string? first-arg) (.startsWith first-arg "Warning: ReactDOM.render is no longer supported in React 18."))
nil

(and (string? first-arg) (.startsWith first-arg "Warning: The current testing environment is not configured to support"))
nil

Expand Down Expand Up @@ -47,27 +46,6 @@
(.unmount root)
(r/flush))))))

(defn with-mounted-component-async
[comp done compiler f]
(let [div (.createElement js/document "div")
root (rdomc/create-root div)
c (if compiler
(rdomc/render root comp compiler)
(rdomc/render root comp))]
(js/console.log "mount" (pr-str comp))
(f c div (fn []
(.unmount root)
(r/flush)
(done)))))

(defn run-fns-after-render [& fs]
((reduce (fn [cb f]
(fn []
(r/after-render (fn []
(f)
(cb)))))
(reverse fs))))

;; For testing logged errors and warnings

(defn log-error [& f]
Expand Down Expand Up @@ -125,19 +103,18 @@
(reject e)))))
(js/Promise.
(fn [resolve reject]
(try
(f)
(js/setTimeout (fn []
(resolve))
;; 16.6ms is one animation frame @ 60hz
17))))))
(f)
(js/setTimeout (fn []
(resolve))
;; 16.6ms is one animation frame @ 60hz
17)))))

(defn with-render
"Run initial render with React/act and then run
given function to check the results. The function
should also return a Promise so we can wait until
it is done before cleanup and resolving the
Promise with function returns."
given function to check the results. If the function
also returns a Promise or thenable, this function
waits until that is resolved, before unmounting the
root and resolving the Promise this function returns."
([comp f]
(with-render comp *test-compiler* f))
([comp compiler f]
Expand All @@ -147,7 +124,8 @@
(rdomc/render root comp compiler)
(rdomc/render root comp)))
(fn []
(-> (f div)
(-> (js/Promise.resolve (f div))
(.then (fn []
(.unmount root)
;; TODO: Likely not needed now?
(r/flush)))))))))

0 comments on commit 87d6765

Please sign in to comment.