2024-07-03 16:11:39 +02:00
|
|
|
(ns token-tests.helpers.state
|
|
|
|
(:require
|
2024-08-07 12:38:24 +02:00
|
|
|
[app.main.ui.workspace.tokens.style-dictionary :as sd]
|
2024-07-03 16:11:39 +02:00
|
|
|
[beicon.v2.core :as rx]
|
|
|
|
[potok.v2.core :as ptk]))
|
|
|
|
|
2024-08-07 12:38:24 +02:00
|
|
|
(defn end
|
|
|
|
"Apply `attributes` that match `token` for `shape-ids`.
|
|
|
|
|
|
|
|
Optionally remove attributes from `attributes-to-remove`,
|
|
|
|
this is useful for applying a single attribute from an attributes set
|
|
|
|
while removing other applied tokens from this set."
|
|
|
|
[]
|
|
|
|
(ptk/reify ::end
|
|
|
|
ptk/WatchEvent
|
|
|
|
(watch [_ _ _]
|
|
|
|
(rx/empty))))
|
|
|
|
|
|
|
|
(defn end+
|
|
|
|
[]
|
|
|
|
(ptk/reify ::end+
|
|
|
|
ptk/WatchEvent
|
|
|
|
(watch [_ state _]
|
|
|
|
(->> (rx/from (sd/resolve-tokens+ (get-in state [:workspace-data :tokens])))
|
|
|
|
(rx/mapcat
|
|
|
|
(fn [_]
|
|
|
|
(rx/of
|
|
|
|
(end))))))))
|
|
|
|
|
2024-07-03 16:11:39 +02:00
|
|
|
(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."
|
2024-08-07 12:38:24 +02:00
|
|
|
(stop-on ::end))
|
2024-07-03 17:04:47 +02:00
|
|
|
|
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]
|
2024-08-07 17:14:05 +02:00
|
|
|
(let [stream (ptk/input-stream store)
|
|
|
|
stopper-s (if (fn? stopper)
|
|
|
|
(stopper stream)
|
|
|
|
(rx/filter #(= :the/end %) stream))]
|
2024-07-03 16:11:39 +02:00
|
|
|
(->> stream
|
2024-08-07 17:14:05 +02:00
|
|
|
(rx/take-until stopper-s)
|
2024-07-03 16:11:39 +02:00
|
|
|
(rx/last)
|
2024-08-07 17:14:05 +02:00
|
|
|
(rx/tap (fn [_]
|
2024-07-03 16:11:39 +02:00
|
|
|
(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-08-07 17:14:05 +02:00
|
|
|
(doseq [event (concat events [(end+)])]
|
|
|
|
(ptk/emit! store event))
|
2024-07-03 16:11:39 +02:00
|
|
|
(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)))
|