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

Extract to helpers

This commit is contained in:
Florian Schroedl 2024-07-04 10:51:08 +02:00
parent 1f0f35e754
commit b43d16008f
2 changed files with 53 additions and 40 deletions

View file

@ -0,0 +1,24 @@
(ns token-tests.helpers.tokens
(:require
[app.common.test-helpers.ids-map :as thi]
[app.main.ui.workspace.tokens.token :as wtt]))
(defn add-token [state label params]
(let [id (thi/new-id! label)
token (assoc params :id id)]
(update-in state [:data :tokens] assoc id token)))
(defn get-token [file label]
(let [id (thi/id label)]
(get-in file [:data :tokens id])))
(defn apply-token-to-shape [file shape-label token-label attributes]
(let [first-page-id (get-in file [:data :pages 0])
shape-id (thi/id shape-label)
token-id (thi/id token-label)
applied-attributes (wtt/attributes-map attributes token-id)]
(update-in file [:data
:pages-index first-page-id
:objects shape-id
:applied-tokens]
merge applied-attributes)))

View file

@ -1,5 +1,6 @@
(ns token-tests.logic.token-actions-test
(:require
[token-tests.helpers.tokens :as toht]
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.compositions :as ctho]
[app.common.test-helpers.files :as cthf]
@ -14,38 +15,26 @@
(t/use-fixtures :each
{:before thp/reset-idmap!})
(defn add-token [state label params]
(let [id (thi/new-id! label)
token (assoc params :id id)]
(update-in state [:data :tokens] assoc id token)))
(defn get-token [file label]
(let [id (thi/id label)]
(get-in file [:data :tokens id])))
(defn apply-token-to-shape [file shape-label token-label attributes]
(let [first-page-id (get-in file [:data :pages 0])
shape-id (thi/id shape-label)
token-id (thi/id token-label)
applied-attributes (wtt/attributes-map attributes token-id)]
(update-in file [:data
:pages-index first-page-id
:objects shape-id
:applied-tokens]
merge applied-attributes)))
(defn- setup-file
[]
(-> (cthf/sample-file :file-1 :page-label :page-1)
(ctho/add-rect :rect-1 {})
(ctho/add-rect :rect-2 {})
(ctho/add-rect :rect-3 {})
(add-token :token-1 {:value "12"
:name "borderRadius.sm"
:type :border-radius})
(add-token :token-2 {:value "{borderRadius.sm} * 2"
:name "borderRadius.md"
:type :border-radius})))
(toht/add-token :token-1 {:value "12"
:name "borderRadius.sm"
:type :border-radius})
(toht/add-token :token-2 {:value "{borderRadius.sm} * 2"
:name "borderRadius.md"
:type :border-radius})))
(comment
(floscr/make-printable
(-> (setup-file)
(toht/apply-token-to-shape :rect-1 :token-1 #{:rx :ry})))
(-> (setup-file))
nil)
(t/deftest test-apply-token
(t/testing "applying a token twice with the same attributes will override")
@ -56,17 +45,17 @@
rect-1 (cths/get-shape file :rect-1)
events [(wtc/apply-token {:shape-ids [(:id rect-1)]
:attributes #{:rx :ry}
:token (get-token file :token-1)
:token (toht/get-token file :token-1)
:on-update-shape wtc/update-shape-radius})
(wtc/apply-token {:shape-ids [(:id rect-1)]
:attributes #{:rx :ry}
:token (get-token file :token-2)
:token (toht/get-token file :token-2)
:on-update-shape wtc/update-shape-radius})]]
(tohs/run-store-async
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)
token-2' (get-token file' :token-2)
token-2' (toht/get-token file' :token-2)
rect-1' (cths/get-shape file' :rect-1)]
(t/is (some? (:applied-tokens rect-1')))
(t/is (= (:rx (:applied-tokens rect-1')) (:id token-2')))
@ -83,13 +72,13 @@
rect-1 (cths/get-shape file :rect-1)
events [(wtc/apply-token {:shape-ids [(:id rect-1)]
:attributes #{:rx :ry}
:token (get-token file :token-2)
:token (toht/get-token file :token-2)
:on-update-shape wtc/update-shape-radius})]]
(tohs/run-store-async
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)
token-2' (get-token file' :token-2)
token-2' (toht/get-token file' :token-2)
rect-1' (cths/get-shape file' :rect-1)]
(t/is (some? (:applied-tokens rect-1')))
(t/is (= (:rx (:applied-tokens rect-1')) (:id token-2')))
@ -102,20 +91,20 @@
(t/async
done
(let [file (-> (setup-file)
(add-token :token-target {:value "100"
:name "dimensions.sm"
:type :dimensions}))
(toht/add-token :token-target {:value "100"
:name "dimensions.sm"
:type :dimensions}))
store (ths/setup-store file)
rect-1 (cths/get-shape file :rect-1)
events [(wtc/apply-token {:shape-ids [(:id rect-1)]
:attributes #{:width :height}
:token (get-token file :token-target)
:token (toht/get-token file :token-target)
:on-update-shape wtc/update-shape-dimensions})]]
(tohs/run-store-async
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)
token-target' (get-token file' :token-target)
token-target' (toht/get-token file' :token-target)
rect-1' (cths/get-shape file' :rect-1)]
(t/is (some? (:applied-tokens rect-1')))
(t/is (= (:width (:applied-tokens rect-1')) (:id token-target')))
@ -134,12 +123,12 @@
events [(wtc/toggle-token {:shapes [rect-1 rect-2]
:token-type-props {:attributes #{:rx :ry}
:on-update-shape wtc/update-shape-radius}
:token (get-token file :token-2)})]]
:token (toht/get-token file :token-2)})]]
(tohs/run-store-async
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)
token-2' (get-token file' :token-2)
token-2' (toht/get-token file' :token-2)
rect-1' (cths/get-shape file' :rect-1)
rect-2' (cths/get-shape file' :rect-2)]
(t/is (some? (:applied-tokens rect-1')))
@ -156,7 +145,7 @@
(t/async
done
(let [file (-> (setup-file)
(apply-token-to-shape :rect-1 :token-1 #{:rx :ry}))
(toht/apply-token-to-shape :rect-1 :token-1 #{:rx :ry}))
store (ths/setup-store file)
rect-1 (cths/get-shape file :rect-1)
rect-2 (cths/get-shape file :rect-2)
@ -166,7 +155,7 @@
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)
token-2' (get-token file' :token-2)
token-2' (toht/get-token file' :token-2)
rect-1' (cths/get-shape file' :rect-1)
rect-2' (cths/get-shape file' :rect-2)]
(t/is (nil? (:rx (:applied-tokens rect-1'))))