Skip to content

Commit

Permalink
Mock act on production with with setTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Nov 15, 2024
1 parent 85463e6 commit 85137e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
6 changes: 3 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
[doo "0.1.11"]
[cljsjs/prop-types "15.7.2-0"]

[cljsjs/react "18.2.0-1"]
[cljsjs/react-dom "18.2.0-1"]
[cljsjs/react-dom-server "18.2.0-1"]]
[cljsjs/react "18.3.1-1"]
[cljsjs/react-dom "18.3.1-1"]
[cljsjs/react-dom-server "18.3.1-1"]]
:source-paths ["demo" "test" "examples/todomvc/src" "examples/simple/src" "examples/geometry/src"]
:resource-paths ["site" "target/cljsbuild/client" "target/cljsbuild/client-npm"]}}

Expand Down
2 changes: 2 additions & 0 deletions test/reagenttest/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@
(done#)))
(.catch (fn [e#]
(js/clearTimeout timeout#)
(js/console.error "Promise failed in async macro" e#)
(cljs.test/is (not e#))
(done#))))
(catch js/Error e#
(js/clearTimeout timeout#)
(js/console.error "Error in async macro" e#)
(cljs.test/is (not e#))
(done#)))))))
33 changes: 23 additions & 10 deletions test/reagenttest/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require-macros [reagenttest.utils :refer [act]])
(:require ["react" :as react]
[reagent.core :as r]
[reagent.debug :as debug]
[reagent.debug :as debug :refer [dev?]]
[reagent.dom.client :as rdomc]
[reagent.dom.server :as server]
[reagent.impl.template :as tmpl]))
Expand Down Expand Up @@ -106,18 +106,31 @@
(defn act*
"Run f to trigger Reagent updates,
will return Promise which will resolve after
Reagent and React render."
Reagent and React render.
In production builds, the React.act isn't available,
so just mock with 17ms timeout... Hopefully that usually
is enough time for React to flush the queue?"
[f]
;; async act doesn't return a real promise (with chainable then),
;; so wrap it.
(js/Promise.
(fn [resolve reject]
(try
(.then (react/act f)
resolve
reject)
(catch :default e
(reject e))))))
(if (dev?)
(js/Promise.
(fn [resolve reject]
(try
(.then (react/act f)
resolve
reject)
(catch :default e
(reject e)))))
(js/Promise.
(fn [resolve reject]
(try
(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
Expand Down

0 comments on commit 85137e2

Please sign in to comment.