0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-03 04:49:03 -05:00
penpot/frontend/test/token_tests/helpers/state.cljs

50 lines
1.8 KiB
Text
Raw Normal View History

2024-07-03 16:11:39 +02:00
(ns token-tests.helpers.state
(:require
[beicon.v2.core :as rx]
[potok.v2.core :as ptk]))
(defn stop-on
"Helper function to be used with async version of run-store.
Will stop the execution after event with `event-type` has completed."
[event-type]
(fn [stream]
(->> stream
2024-07-03 17:04:47 +02:00
#_(rx/tap #(prn (ptk/type %)))
2024-07-03 16:11:39 +02:00
(rx/filter #(ptk/type? event-type %)))))
2024-07-03 17:04:47 +02:00
(def stop-on-send-update-indices
"Stops on `send-update-indices` function being called, which should be the last function of an event chain."
(stop-on :app.main.data.workspace.changes/send-update-indices))
2024-07-03 16:11:39 +02:00
;; Support for async events in tests
;; https://chat.kaleidos.net/penpot-partners/pl/tz1yoes3w3fr9qanxqpuhoz3ch
(defn run-store
"Async version of `frontend-tests.helpers.state/run-store`."
([store done events completed-cb]
(run-store store done events completed-cb nil))
([store done events completed-cb stopper]
(let [stream (ptk/input-stream store)]
(->> stream
(rx/take-until (if stopper
(stopper stream)
(rx/filter #(= :the/end %) stream)))
(rx/last)
(rx/tap (fn []
(completed-cb @store)))
(rx/subs! (fn [_] (done))
(fn [cause]
(js/console.log "[error]:" cause))
(fn [_]
2024-07-04 09:29:16 +02:00
#_(js/console.log "[complete]"))))
2024-07-03 16:11:39 +02:00
(doall (for [event events]
(ptk/emit! store event)))
(ptk/emit! store :the/end))))
2024-07-03 17:04:47 +02:00
(defn run-store-async
"Helper version of `run-store` that automatically stops on the `send-update-indices` event"
([store done events completed-cb]
(run-store store done events completed-cb stop-on-send-update-indices))
([store done events completed-cb stop-on]
(run-store store done events completed-cb stop-on)))