From 888e2d936844e6f3eb859f599e1a1f7137877268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Schr=C3=B6dl?= Date: Mon, 23 Nov 2026 10:44:20 +0100 Subject: [PATCH] :wrench: Test applying dimension and spacing tokens (#13215) Covers how dimension, spacing and sizing tokens change position, corners, stroke sides, paddings, gaps, margins and size limits, the fallback to the closest font weight with its warning, and detaching tokens from attributes without knowing the token. The sizing test in token_actions_test now applies the token through the function the app uses for sizing tokens, not the dimensions one. Signed-off-by: Florian Schroedl --- frontend/test/frontend_tests/runner.cljs | 2 + .../tokens/logic/token_actions_test.cljs | 112 ++++++++- .../token_dimension_application_test.cljs | 231 ++++++++++++++++++ 3 files changed, 343 insertions(+), 2 deletions(-) create mode 100644 frontend/test/frontend_tests/tokens/logic/token_dimension_application_test.cljs diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 4c812f7007..f5070ea349 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -145,6 +145,7 @@ [frontend-tests.tokens.import-export-test] [frontend-tests.tokens.logic.token-actions-test] [frontend-tests.tokens.logic.token-data-test] + [frontend-tests.tokens.logic.token-dimension-application-test] [frontend-tests.tokens.logic.token-remapping-test] [frontend-tests.tokens.logic.tokens-source-test] [frontend-tests.tokens.logic.tokens-status-test] @@ -384,6 +385,7 @@ 'frontend-tests.tokens.import-export-test 'frontend-tests.tokens.logic.token-actions-test 'frontend-tests.tokens.logic.token-data-test + 'frontend-tests.tokens.logic.token-dimension-application-test 'frontend-tests.tokens.logic.token-remapping-test 'frontend-tests.tokens.referenced-token-value-test 'frontend-tests.tokens.shadow-form-test diff --git a/frontend/test/frontend_tests/tokens/logic/token_actions_test.cljs b/frontend/test/frontend_tests/tokens/logic/token_actions_test.cljs index e23c51b7ff..ae18599aae 100644 --- a/frontend/test/frontend_tests/tokens/logic/token_actions_test.cljs +++ b/frontend/test/frontend_tests/tokens/logic/token_actions_test.cljs @@ -15,18 +15,25 @@ [app.common.types.token :as ctt] [app.common.types.tokens-lib :as ctob] [app.common.types.tokens-status :as ctos] + [app.main.data.notifications :as ntf] [app.main.data.workspace.colors :as dc] [app.main.data.workspace.tokens.application :as dwta] [app.main.data.workspace.tokens.library-edit :as dwtl] [app.main.data.workspace.wasm-text :as dwwt] + [app.main.store :as st] + [app.util.i18n :refer [tr]] [app.util.storage :as storage] + [beicon.v2.core :as rx] [cljs.test :as t :include-macros true] [cuerdas.core :as str] + [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.mock :as mock] [frontend-tests.helpers.pages :as thp] [frontend-tests.helpers.state :as ths] [frontend-tests.helpers.wasm :as thw] [frontend-tests.tokens.helpers.state :as tohs] - [frontend-tests.tokens.helpers.tokens :as toht])) + [frontend-tests.tokens.helpers.tokens :as toht] + [potok.v2.core :as ptk])) (t/use-fixtures :each {:before (fn [] @@ -462,7 +469,7 @@ events [(dwta/apply-token {:shape-ids [(:id rect-1)] :attributes #{:width :height} :token (toht/get-token file "sizing.sm") - :on-update-shape dwta/apply-dimensions-token})]] + :on-update-shape dwta/apply-sizing-token})]] (tohs/run-store-async store done events (fn [new-state] @@ -1034,6 +1041,34 @@ :on-update-shape dwta/update-stroke-width})] step2))))) +(t/deftest test-detach-tokens-by-attribute + (t/testing "detaching attributes removes whichever token they have, also one missing from the library" + (t/async + done + (let [file (-> (setup-file-with-tokens) + (ctht/apply-token-to-shape :rect-1 "radius.md" #{:r1 :r2 :r3 :r4} #{:r1 :r2 :r3 :r4} 8) + (ctht/apply-token-to-shape :rect-2 "radius.lg" #{:r1} #{:r1} 16)) + store (ths/setup-store file) + events [(dwta/unapply-multiple-tokens {:attributes #{:r1 :r2} + :shape-ids [(cthi/id :rect-1) (cthi/id :rect-2) (cthi/id :rect-3)]})]] + (tohs/run-store-async + store done events + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + rect-1' (cths/get-shape file' :rect-1) + rect-2' (cths/get-shape file' :rect-2)] + (t/testing "removes the tokens from those attributes only" + (t/is (= {:r3 "radius.md" :r4 "radius.md"} (:applied-tokens rect-1'))) + (t/is (empty? (:applied-tokens rect-2')))) + + (t/testing "keeps the values the tokens had set" + (t/is (= 8 (:r1 rect-1'))) + (t/is (= 16 (:r1 rect-2')))) + + (t/testing "a shape without tokens stays as it is" + (t/is (= (:applied-tokens (cths/get-shape file :rect-3)) + (:applied-tokens (cths/get-shape file' :rect-3)))))))))))) + (t/deftest test-apply-shadow (t/testing "applies shadow token and updates the shapes with shadow" (t/async @@ -1337,6 +1372,79 @@ (t/is (pos? (thw/call-count :get-text-dimensions)))))) debounce-text-stop))))) +(defn- text-styles + "The styles of the first run of text in `shape`." + [shape] + (->> (txt/content->text+styles (:content shape)) + (some (fn [[style text]] + (when (seq text) + style))))) + +(defn- add-font-weight-token + [file token-name value] + (update-in file [:data :tokens-lib] + #(ctob/add-token % (cthi/id :set-a) + (ctob/make-token {:name token-name + :value value + :type :font-weight})))) + +(defn- ^:async apply-font-weight-token + "Applies the font weight token to the text the way the token pill does, + and waits until the text has been measured again." + [store file token-name] + (let [resized (async/observe + (->> (ptk/input-stream store) + (rx/filter (ptk/type? ::dwwt/resize-wasm-text-debounce-commit)) + (rx/take 1)))] + (ptk/emit! store (dwta/apply-token {:shape-ids [(cthi/id :text-1)] + :attributes #{:font-weight} + :token (toht/get-token file token-name) + :on-update-shape dwta/update-font-weight-interactive})) + (await resized))) + +(t/deftest ^:async test-apply-font-weight-without-matching-variant + (let [file (-> (setup-file-with-tokens) + (add-font-weight-token "font-weight.thin" "100")) + store (ths/setup-store file) + emitted (atom [])] + (await (mock/with-mocks* + {st/emit! (mock/stub (fn [& events] (swap! emitted into events) nil))} + (await (apply-font-weight-token store file "font-weight.thin")))) + + (let [styles (text-styles (cths/get-shape (ths/get-file-from-state @store) :text-1))] + (t/testing "keeps the token's weight and uses the closest variant of the font" + (t/is (= "100" (:font-weight styles))) + (t/is (= "200" (:font-variant-id styles))))) + + (t/testing "warns that the font has no variant for the weight" + (t/is (= {:content (tr "workspace.tokens.font-variant-not-found") + :type :toast + :level :warning} + (some #(when (ptk/type? ::ntf/show %) + (-> (ptk/update % {}) + :notification + (select-keys [:content :type :level]))) + @emitted)))) + + (rx/dispose! store))) + +(t/deftest ^:async test-apply-font-weight-with-matching-variant + (let [file (-> (setup-file-with-tokens) + (add-font-weight-token "font-weight.light-italic" "300 italic")) + store (ths/setup-store file) + emitted (atom [])] + (await (mock/with-mocks* + {st/emit! (mock/stub (fn [& events] (swap! emitted into events) nil))} + (await (apply-font-weight-token store file "font-weight.light-italic")))) + + (let [styles (text-styles (cths/get-shape (ths/get-file-from-state @store) :text-1))] + (t/is (= "300" (:font-weight styles))) + (t/is (= "italic" (:font-style styles))) + (t/is (= "300italic" (:font-variant-id styles)))) + (t/is (not-any? (ptk/type? ::ntf/show) @emitted)) + + (rx/dispose! store))) + (t/deftest test-toggle-token-none (t/testing "should apply token to all selected items, where no item has the token applied" (t/async diff --git a/frontend/test/frontend_tests/tokens/logic/token_dimension_application_test.cljs b/frontend/test/frontend_tests/tokens/logic/token_dimension_application_test.cljs new file mode 100644 index 0000000000..105c1deffe --- /dev/null +++ b/frontend/test/frontend_tests/tokens/logic/token_dimension_application_test.cljs @@ -0,0 +1,231 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.tokens.logic.token-dimension-application-test + (:require + [app.common.test-helpers.compositions :as ctho] + [app.common.test-helpers.ids-map :as cthi] + [app.common.test-helpers.shapes :as cths] + [app.common.test-helpers.tokens :as ctht] + [app.common.types.tokens-lib :as ctob] + [app.common.types.tokens-status :as ctos] + [app.main.data.workspace.tokens.application :as dwta] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.state :as ths] + [frontend-tests.helpers.wasm :as thw] + [frontend-tests.tokens.helpers.state :as tohs] + [frontend-tests.tokens.helpers.tokens :as toht])) + +(t/use-fixtures :each + {:before (fn [] + (cthi/reset-idmap!) + (thw/setup-wasm-mocks!)) + :after thw/teardown-wasm-mocks!}) + +(def ^:private tokens + [(ctob/make-token :name "dimension.md" :type :dimensions :value "24") + (ctob/make-token :name "dimension.negative" :type :dimensions :value "-8") + (ctob/make-token :name "spacing.md" :type :spacing :value "16") + (ctob/make-token :name "sizing.lg" :type :sizing :value "120")]) + +(defn- setup-file + "Three rectangles and a flex board with a rectangle inside, with + `tokens` in an active set." + [] + (-> (ctht/sample-file-with-tokens + :lib-fn (fn [lib] + (reduce #(ctob/add-token %1 (cthi/id :set) %2) + (ctob/add-set lib (ctob/make-token-set :id (cthi/new-id! :set) + :name "Base")) + tokens)) + :status-fn #(ctos/set-tokens-status % #{} #{(cthi/id :set)})) + (ctho/add-rect :rect) + (ctho/add-rect :rect-2) + (ctho/add-rect :rect-3) + (ctho/add-frame-with-child :layout :layout-child + {:frame-params {:layout :flex}}))) + +(defn- apply-token + [file token-name shape-label attributes on-update-shape] + (dwta/apply-token {:shape-ids [(cthi/id shape-label)] + :attributes attributes + :token (toht/get-token file token-name) + :on-update-shape on-update-shape})) + +(defn- run-events + "Runs `events` on a workspace showing `file`, then calls `check` with + the file before and after." + [file events check done] + (tohs/run-store-async + (ths/setup-store file) done events + (fn [state] + (check file (ths/get-file-from-state state))))) + +(t/deftest dimensions-token-on-shape + (t/async + done + (let [file (setup-file)] + (run-events + file + [(apply-token file "dimension.md" :rect #{:x :y} dwta/apply-dimensions-token) + (apply-token file "dimension.md" :rect #{:r1} dwta/apply-dimensions-token) + (apply-token file "dimension.md" :rect-2 #{:stroke-width-left} dwta/apply-dimensions-token) + (apply-token file "dimension.md" :rect-3 #{:r1 :r2 :r3 :r4} dwta/apply-dimensions-token)] + (fn [file file'] + (let [rect (cths/get-shape file :rect) + rect' (cths/get-shape file' :rect) + rect-2' (cths/get-shape file' :rect-2) + rect-3' (cths/get-shape file' :rect-3)] + (t/testing "moves the shape" + (t/is (= 24 (:x rect'))) + (t/is (= 24 (:y rect')))) + + (t/testing "rounds the chosen corners only" + (t/is (= {:x "dimension.md" :y "dimension.md" :r1 "dimension.md"} + (:applied-tokens rect'))) + (t/is (= 24 (:r1 rect'))) + (t/is (= (:r2 rect) (:r2 rect'))) + (t/is (= [24 24 24 24] ((juxt :r1 :r2 :r3 :r4) rect-3')))) + + (t/testing "a shape without stroke gets one, wide on the chosen side only" + (t/is (= {:stroke-width-left "dimension.md"} (:applied-tokens rect-2'))) + (t/is (= {:stroke-width-top 0 + :stroke-width-right 0 + :stroke-width-bottom 0 + :stroke-width-left 24} + (-> (get-in rect-2' [:strokes 0]) + (select-keys [:stroke-width-top :stroke-width-right + :stroke-width-bottom :stroke-width-left]))))))) + done)))) + +(t/deftest dimensions-token-on-layout + (t/async + done + (let [file (setup-file)] + (run-events + file + [(apply-token file "dimension.md" :layout #{:p1 :p3} dwta/apply-dimensions-token) + (apply-token file "dimension.md" :layout #{:row-gap} dwta/apply-dimensions-token)] + (fn [file file'] + (let [layout (cths/get-shape file :layout) + layout' (cths/get-shape file' :layout)] + (t/is (= {:p1 "dimension.md" :p3 "dimension.md" :row-gap "dimension.md"} + (:applied-tokens layout'))) + + (t/testing "sets the chosen paddings only" + (t/is (= 24 (get-in layout' [:layout-padding :p1]))) + (t/is (= 24 (get-in layout' [:layout-padding :p3]))) + (t/is (= (get-in layout [:layout-padding :p2]) + (get-in layout' [:layout-padding :p2])))) + + (t/testing "sets the chosen gap only" + (t/is (= 24 (get-in layout' [:layout-gap :row-gap]))) + (t/is (= (get-in layout [:layout-gap :column-gap]) + (get-in layout' [:layout-gap :column-gap])))))) + done)))) + +(t/deftest dimensions-token-on-layout-child + (t/async + done + (let [file (setup-file)] + (run-events + file + [(apply-token file "dimension.md" :layout-child #{:m1 :m2 :m3 :m4} dwta/apply-dimensions-token) + (apply-token file "dimension.md" :layout-child #{:layout-item-min-w :layout-item-max-h} + dwta/apply-dimensions-token)] + (fn [_ file'] + (let [child' (cths/get-shape file' :layout-child)] + (t/testing "sets the margins" + (t/is (= {:m1 24 :m2 24 :m3 24 :m4 24} (:layout-item-margin child')))) + + (t/testing "sets the chosen size limits only" + (t/is (= 24 (:layout-item-min-w child'))) + (t/is (= 24 (:layout-item-max-h child'))) + (t/is (nil? (:layout-item-max-w child'))) + (t/is (nil? (:layout-item-min-h child')))))) + done)))) + +(t/deftest negative-dimensions-token-on-size-limits + (t/async + done + (let [file (setup-file)] + (run-events + file + [(apply-token file "dimension.negative" :layout-child #{:layout-item-min-w} + dwta/apply-dimensions-token)] + (fn [_ file'] + (t/is (= 0 (:layout-item-min-w (cths/get-shape file' :layout-child))))) + done)))) + +(t/deftest spacing-token-on-layout + (t/async + done + (let [file (setup-file)] + (run-events + file + [(apply-token file "spacing.md" :layout #{:p1 :p2 :p3 :p4} dwta/apply-spacing-token) + (apply-token file "spacing.md" :layout-child #{:m2 :m4} dwta/apply-spacing-token) + (dwta/apply-spacing-token 16 [(cthi/id :rect)] #{:width})] + (fn [file file'] + (t/testing "sets the paddings of the board" + (t/is (= {:p1 16 :p2 16 :p3 16 :p4 16} + (:layout-padding (cths/get-shape file' :layout))))) + + (t/testing "sets the chosen margins of the child" + (let [margin (:layout-item-margin (cths/get-shape file :layout-child)) + margin' (:layout-item-margin (cths/get-shape file' :layout-child))] + (t/is (= 16 (:m2 margin'))) + (t/is (= 16 (:m4 margin'))) + (t/is (= (:m1 margin) (:m1 margin'))))) + + (t/testing "never resizes a shape" + (t/is (= (:width (cths/get-shape file :rect)) + (:width (cths/get-shape file' :rect)))))) + done)))) + +(t/deftest spacing-token-from-pill + (t/async + done + (let [file (setup-file)] + (run-events + file + [(dwta/apply-spacing-token-separated + {:token (toht/get-token file "spacing.md") + :shapes [(cths/get-shape file :layout) (cths/get-shape file :layout-child)]})] + (fn [_ file'] + (let [layout' (cths/get-shape file' :layout) + child' (cths/get-shape file' :layout-child)] + (t/testing "the board takes the token on its gaps" + (t/is (= {:row-gap "spacing.md" :column-gap "spacing.md"} + (:applied-tokens layout'))) + (t/is (= {:row-gap 16 :column-gap 16} (:layout-gap layout')))) + + (t/testing "the shape inside it takes the token on its margins" + (t/is (= {:m1 "spacing.md" :m2 "spacing.md" :m3 "spacing.md" :m4 "spacing.md"} + (:applied-tokens child'))) + (t/is (= {:m1 16 :m2 16 :m3 16 :m4 16} (:layout-item-margin child')))))) + done)))) + +(t/deftest sizing-token + (t/async + done + (let [file (setup-file)] + (run-events + file + [(apply-token file "sizing.lg" :rect-2 #{:width} dwta/apply-sizing-token) + (dwta/apply-sizing-token 120 [(cthi/id :rect-3)] #{:x})] + (fn [file file'] + (let [rect-2 (cths/get-shape file :rect-2) + rect-2' (cths/get-shape file' :rect-2) + rect-3 (cths/get-shape file :rect-3) + rect-3' (cths/get-shape file' :rect-3)] + (t/testing "on the width only, the height stays" + (t/is (= 120 (:width rect-2'))) + (t/is (= (:height rect-2) (:height rect-2')))) + + (t/testing "never moves a shape" + (t/is (= (:x rect-3) (:x rect-3')))))) + done))))