0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 06:02:32 -05:00

Async token event tests working

This commit is contained in:
Florian Schroedl 2024-07-03 16:11:39 +02:00
parent e203646085
commit ed7aad6c4e
4 changed files with 90 additions and 49 deletions

View file

@ -60,15 +60,19 @@
shapes)]
(and (empty? with-token) (seq without-token))))
(defn apply-token
"Applies `attributes` to `token` for `shapes-ids`.
When a `on-shape-update` function is passed, use this to update the shape attributes as well."
[{:keys [attributes token shape-ids on-update-shape] :as _props}]
(ptk/reify ::apply-token
(defn done
[]
(ptk/reify ::done
ptk/WatchEvent
(watch [_ _ _]
(->> (rx/from (sd/resolve-workspace-tokens+))
(rx/from (p/resolved true)))))
(defn apply-token
[{:keys [attributes shape-ids token on-update-shape cb] :as _props}]
(ptk/reify ::apply-token
ptk/WatchEvent
(watch [_ state _]
(->> (rx/from (sd/resolve-tokens+ (get-in state [:workspace-data :tokens])))
(rx/mapcat
(fn [sd-tokens]
(let [resolved-value (-> (get sd-tokens (:id token))
@ -77,8 +81,13 @@
(into {}))]
(rx/of
(dch/update-shapes shape-ids (fn [shape] (update shape :applied-tokens merge tokenized-attributes)))
(when cb
(cb))
(when on-update-shape
(on-update-shape resolved-value shape-ids attributes))))))))))
(on-update-shape resolved-value shape-ids attributes))))
#_(rx/of
(dch/update-shapes shape-ids #(assoc % :applied-tokens {:found 1})))))))))
(def remove-keys #(apply dissoc %1 %2))

View file

@ -53,7 +53,7 @@
"Resolves references and math expressions using StyleDictionary.
Returns a promise with the resolved dictionary."
[tokens & {:keys [debug?] :as config}]
(let [performance-start (js/window.performance.now)
(let [#_#_performance-start (js/window.performance.now)
sd (tokens->style-dictionary+ tokens config)]
(when debug?
(js/console.log "StyleDictionary" sd))
@ -61,12 +61,12 @@
(.buildAllPlatforms "json")
(.catch js/console.error)
(.then (fn [^js resp]
(let [performance-end (js/window.performance.now)
duration-ms (- performance-end performance-start)
(let [#_#_performance-end (js/window.performance.now)
#_#_duration-ms (- performance-end performance-start)
resolved-tokens (.-allTokens resp)]
(when debug?
(js/console.log "Time elapsed" duration-ms "ms")
(js/console.log "Resolved tokens" resolved-tokens))
#_(when debug?
(js/console.log "Time elapsed" duration-ms "ms")
(js/console.log "Resolved tokens" resolved-tokens))
resolved-tokens))))))
(defn humanize-errors [{:keys [errors value] :as _token}]

View file

@ -0,0 +1,38 @@
(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
(rx/tap #(prn (ptk/type %)))
(rx/filter #(ptk/type? event-type %)))))
;; 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 [_]
(js/console.log "[complete]"))))
(doall (for [event events]
(ptk/emit! store event)))
(ptk/emit! store :the/end))))

View file

@ -4,11 +4,12 @@
[app.common.test-helpers.files :as cthf]
[app.common.test-helpers.shapes :as cths]
[app.main.data.workspace.changes :as dch]
[app.main.data.workspace.selection :as dws]
[app.main.ui.workspace.tokens.core :as wtc]
[beicon.v2.core :as rx]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.pages :as thp]
[frontend-tests.helpers.state :as ths]))
[frontend-tests.helpers.state :as ths]
[potok.v2.core :as ptk]))
(t/use-fixtures :each
{:before thp/reset-idmap!})
@ -21,43 +22,36 @@
(ctho/add-rect :rect-1 {})
(ctho/add-rect :rect-2 {})
(ctho/add-rect :rect-3 {})
(assoc-in [:data :tokens] {token-id {:id token-id
:value "12"
:name "sm"
:type :border-radius}}))))
(assoc-in [:data :tokens] {#uuid "91bf7f1f-fce2-482f-a423-c6b502705ff1"
{:id #uuid "91bf7f1f-fce2-482f-a423-c6b502705ff1"
:value "12"
:name "sm"
:type :border-radius}}))))
(t/deftest test-update-shape
(t/deftest test-apply-token
(t/async
done
(let [;; ==== Setup
file (setup-file)
store (ths/setup-store file)
rect-1 (cths/get-shape file :rect-1)
;; ==== Action
events [(dws/select-shape (:id rect-1))
(dch/update-shapes [(:id rect-1)] (fn [shape] (assoc shape :applied-tokens {:rx (random-uuid)})))
#_(wtc/on-add-token {:token-type-props {:attributes {:rx :ry}
:on-update-shape #(fn [& _])}
:shape-ids [(:id rect-1)]
:token {:id (random-uuid)}})]]
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)
page' (cthf/current-page file')
rect-1' (cths/get-shape file' :rect-1)
;; ==== Get
#_#_rect-1' (get-in new-state [:workspace-data
:pages-index
(cthi/id :page-1)
:objects
(cthi/id :rect-1)])]
;; ==== Check
(t/is (some? (:applied-tokens rect-1')))))))))
done
(let [file (setup-file)
store (ths/setup-store file)
rect-1 (cths/get-shape file :rect-1)
events [(wtc/apply-token {:shape-ids [(:id rect-1)]
:attributes #{:rx :ry}
:token {:id #uuid "91bf7f1f-fce2-482f-a423-c6b502705ff1"}
:on-update-shape wtc/update-shape-radius})]]
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)
rect-1' (cths/get-shape file' :rect-1)]
(t/is (some? (:applied-tokens rect-1')))
(t/is (= (:rx (:applied-tokens rect-1')) #uuid "91bf7f1f-fce2-482f-a423-c6b502705ff1"))
(t/is (= (:rx rect-1') 12))))
(fn [stream]
(->> stream
;; (rx/tap #(prn (ptk/type %)))
(rx/filter #(ptk/type? :app.main.data.workspace.changes/send-update-indices %))))))))
(comment