diff --git a/common/test/common_tests/types/token_test.cljc b/common/test/common_tests/types/token_test.cljc index e29cd1b77b..70632607ba 100644 --- a/common/test/common_tests/types/token_test.cljc +++ b/common/test/common_tests/types/token_test.cljc @@ -8,6 +8,7 @@ (:require [app.common.schema :as sm] [app.common.types.token :as cto] + [clojure.set :as set] [clojure.test :as t])) (t/deftest test-valid-token-name @@ -45,6 +46,51 @@ (t/is (false? (cto/per-side-stroke-shape? :group))) (t/is (false? (cto/per-side-stroke-shape? nil))))) +(t/deftest shape-type->attributes-test + (t/testing "rectangles and images take radius tokens but no spacing or typography" + (let [attrs (cto/shape-type->attributes :rect false)] + (t/is (set/subset? cto/border-radius-keys attrs)) + (t/is (set/subset? cto/color-keys attrs)) + (t/is (set/subset? #{:x :y} attrs)) + (t/is (empty? (set/intersection cto/spacing-gap-padding-keys attrs))) + (t/is (empty? (set/intersection cto/typography-keys attrs))) + (t/is (= attrs (cto/shape-type->attributes :image false))))) + + (t/testing "boards take gap and padding tokens only when they have a layout" + (let [board (cto/shape-type->attributes :frame false) + layout-board (cto/shape-type->attributes :frame true)] + (t/is (= (cto/shape-type->attributes :rect false) board)) + (t/is (empty? (set/intersection cto/spacing-gap-padding-keys board))) + (t/is (set/subset? cto/spacing-gap-padding-keys layout-board)) + (t/is (set/subset? board layout-board)))) + + (t/testing "texts take typography and number tokens but no radius" + (let [attrs (cto/shape-type->attributes :text false)] + (t/is (set/subset? cto/typography-keys attrs)) + (t/is (set/subset? cto/number-keys attrs)) + (t/is (set/subset? cto/color-keys attrs)) + (t/is (empty? (set/intersection cto/border-radius-keys attrs))))) + + (t/testing "circles, paths, booleans and svgs share the generic attributes" + (let [attrs (cto/shape-type->attributes :circle false)] + (t/is (set/subset? cto/stroke-width-keys attrs)) + (t/is (set/subset? cto/shadow-keys attrs)) + (t/is (empty? (set/intersection cto/border-radius-keys attrs))) + (t/is (= attrs + (cto/shape-type->attributes :path false) + (cto/shape-type->attributes :bool false) + (cto/shape-type->attributes :svg-raw false))))) + + (t/testing "the layout flag only matters for boards" + (t/is (= (cto/shape-type->attributes :rect false) + (cto/shape-type->attributes :rect true))) + (t/is (= (cto/shape-type->attributes :text false) + (cto/shape-type->attributes :text true)))) + + (t/testing "groups take no tokens" + (t/is (nil? (cto/shape-type->attributes :group false))) + (t/is (nil? (cto/shape-type->attributes :group true))))) + (t/deftest token-value-with-refs (t/testing "empty value" (t/is (= (cto/insert-ref "" 0 "token1") diff --git a/common/test/common_tests/types/tokens_lib_test.cljc b/common/test/common_tests/types/tokens_lib_test.cljc index 80d8119a1a..41761083ee 100644 --- a/common/test/common_tests/types/tokens_lib_test.cljc +++ b/common/test/common_tests/types/tokens_lib_test.cljc @@ -12,6 +12,7 @@ #?(:clj [clojure.datafy :refer [datafy]]) [app.common.data :as d] [app.common.files.tokens :as cfo] + [app.common.path-names :as cpn] [app.common.test-helpers.ids-map :as thi] [app.common.time :as ct] [app.common.transit :as tr] @@ -2121,6 +2122,69 @@ (t/is (false? (ctob/token-name-path-exists? "other" {"border-radius" {:name "sm"}}))) (t/is (false? (ctob/token-name-path-exists? "dark.border-radius.md" {"dark" {"border-radius" {"sm" {:name "sm"}}}})))) +(t/deftest normalize-set-name + (t/testing "spaces around the group separators and empty groups are dropped" + (t/is (= "set" (ctob/normalize-set-name "set"))) + (t/is (= "group/set" (ctob/normalize-set-name " group / set "))) + (t/is (= "group/set" (ctob/normalize-set-name "group//set/"))) + (t/is (= "" (ctob/normalize-set-name " / ")))) + + (t/testing "a name relative to another set keeps that set's groups" + (t/is (= "group/sub/new" (ctob/normalize-set-name "new" "group/sub/old"))) + (t/is (= "group/extra/new" (ctob/normalize-set-name " extra / new " "group/old"))) + (t/is (= "new" (ctob/normalize-set-name "new" "old"))))) + +(defn- token-tree + [& names] + (cpn/build-tree-root (map #(ctob/make-token :name % :type :color :value "#000000") names) + ctob/token-separator)) + +(defn- find-node + [nodes path] + (some #(if (= path (:path %)) % (find-node (:children %) path)) nodes)) + +(t/deftest rename-path + (let [tree (token-tree "color.brand.primary" "color.brand.secondary" "color.neutral") + color (find-node tree "color") + brand (find-node tree "color.brand") + primary (find-node tree "color.brand.primary") + token (ctob/make-token :name "color.brand.primary" :type :color :value "#000000")] + + (t/testing "renaming a group changes only its own segment of the path" + (t/is (= "palette" (ctob/rename-path color "palette"))) + (t/is (= "color.accent" (ctob/rename-path brand "accent")))) + + (t/testing "a token inside a renamed group moves along with it" + (t/is (= "palette.brand.primary" (ctob/rename-path color token "palette"))) + (t/is (= "color.accent.primary" (ctob/rename-path brand token "accent")))) + + (t/testing "renaming a token changes its last segment" + (t/is (= "color.brand.main" (ctob/rename-path primary "main"))) + (t/is (= "color.brand.main" (ctob/rename-path primary token "main")))))) + +(t/deftest update-tokens-group + (let [color-token #(ctob/make-token :name % :type :color :value "#000000") + active-tokens (d/ordered-map + "color.brand.primary" (color-token "color.brand.primary") + "color.brand.secondary" (color-token "color.brand.secondary") + "color.neutral.gray" (color-token "color.neutral.gray") + "spacing.sm" (ctob/make-token :name "spacing.sm" :type :spacing :value "4"))] + + (t/testing "tokens of the renamed group get the new group name" + (let [result (ctob/update-tokens-group active-tokens "color.brand" "brand" "accent")] + (t/is (= ["color.accent.primary" "color.accent.secondary" "color.neutral.gray" "spacing.sm"] + (map first result))) + (t/is (= (map first result) (map (comp :name second) result))))) + + (t/testing "tokens without the group's name in their path stay as they are" + (let [result (into {} (ctob/update-tokens-group active-tokens "color.brand" "brand" "accent"))] + (t/is (= (get active-tokens "color.neutral.gray") (get result "color.neutral.gray"))) + (t/is (= (get active-tokens "spacing.sm") (get result "spacing.sm"))))) + + (t/testing "renaming a top level group renames it in the tokens that have its name" + (t/is (= ["palette.brand.primary" "palette.brand.secondary" "palette.neutral.gray" "spacing.sm"] + (map first (ctob/update-tokens-group active-tokens "color" "color" "palette"))))))) + #?(:clj (t/deftest token-set-encode-decode-roundtrip-with-invalid-set-name (binding [ct/*clock* (ct/tick-millis-clock)] diff --git a/common/test/common_tests/types/tokens_migrations_test.cljc b/common/test/common_tests/types/tokens_migrations_test.cljc index 880d0c9432..54060eea0d 100644 --- a/common/test/common_tests/types/tokens_migrations_test.cljc +++ b/common/test/common_tests/types/tokens_migrations_test.cljc @@ -82,6 +82,31 @@ (t/is (= (ctob/get-name theme2') (ctob/get-name theme2))) (t/is (= (ctob/get-description theme2') (ctob/get-description theme2)))))) +(t/deftest test-fix-duplicate-token-set-ids + (t/testing "a library without duplicated set ids stays the same" + (let [tokens-lib (-> (ctob/make-tokens-lib) + (ctob/add-set (ctob/make-token-set :id (thi/new-id! :set1) :name "set1")) + (ctob/add-set (ctob/make-token-set :id (thi/new-id! :set2) :name "group/set2"))) + tokens-lib' (ctob/fix-duplicate-token-set-ids tokens-lib)] + (t/is (empty? (d/map-diff (datafy tokens-lib) (datafy tokens-lib')))))) + + (t/testing "a set sharing the id of an earlier set gets a new id and keeps its name and tokens" + (let [token (ctob/make-token :name "color.primary" :type :color :value "#ff0000") + tokens-lib (-> (ctob/make-tokens-lib) + (ctob/add-set (ctob/make-token-set :id (thi/new-id! :set1) :name "set1")) + (ctob/add-set (ctob/make-token-set :id (thi/id :set1) + :name "group/set2" + :tokens {"color.primary" token}))) + tokens-lib' (ctob/fix-duplicate-token-set-ids tokens-lib) + [set1 set2] (ctob/get-sets tokens-lib')] + (t/is (ctob/valid-tokens-lib? tokens-lib')) + (t/is (= ["set1" "group/set2"] (ctob/get-set-names tokens-lib'))) + (t/is (= (thi/id :set1) (ctob/get-id set1))) + (t/is (uuid? (ctob/get-id set2))) + (t/is (not= (thi/id :set1) (ctob/get-id set2))) + (t/is (empty? (ctob/get-tokens tokens-lib' (ctob/get-id set1)))) + (t/is (= token (get (ctob/get-tokens tokens-lib' (ctob/get-id set2)) "color.primary")))))) + (t/deftest make-tokens-status-from-tokens-lib (t/testing "active themes and sets from a legacy tokens-lib should be added to the status" (let [base-lib (-> (ctob/make-tokens-lib) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 15343eaffc..4c812f7007 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -153,12 +153,15 @@ [frontend-tests.tokens.style-dictionary-test] [frontend-tests.tokens.theme-create-modal-test] [frontend-tests.tokens.token-application-test] + [frontend-tests.tokens.token-color-test] [frontend-tests.tokens.token-errors-test] + [frontend-tests.tokens.token-format-test] [frontend-tests.tokens.token-group-test] [frontend-tests.tokens.token-parsing-test] [frontend-tests.tokens.token-path-test] [frontend-tests.tokens.token-pill-test] [frontend-tests.tokens.token-tree-test] + [frontend-tests.tokens.token-warnings-test] [frontend-tests.tokens.tokenscript-test] [frontend-tests.tokens.typography-form-test] [frontend-tests.tokens.workspace-tokens-remap-test] @@ -387,12 +390,15 @@ 'frontend-tests.tokens.style-dictionary-test 'frontend-tests.tokens.theme-create-modal-test 'frontend-tests.tokens.token-application-test + 'frontend-tests.tokens.token-color-test 'frontend-tests.tokens.token-errors-test + 'frontend-tests.tokens.token-format-test 'frontend-tests.tokens.token-group-test 'frontend-tests.tokens.token-parsing-test 'frontend-tests.tokens.token-path-test 'frontend-tests.tokens.token-pill-test 'frontend-tests.tokens.token-tree-test + 'frontend-tests.tokens.token-warnings-test 'frontend-tests.tokens.tokenscript-test 'frontend-tests.tokens.typography-form-test 'frontend-tests.tokens.logic.tokens-source-test diff --git a/frontend/test/frontend_tests/tokens/token_color_test.cljs b/frontend/test/frontend_tests/tokens/token_color_test.cljs new file mode 100644 index 0000000000..7ef968ceff --- /dev/null +++ b/frontend/test/frontend_tests/tokens/token_color_test.cljs @@ -0,0 +1,39 @@ +;; 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.token-color-test + (:require + [app.common.types.tokens-lib :as ctob] + [app.main.data.workspace.tokens.color :as wtc] + [cljs.test :as t :include-macros true])) + +(t/deftest color-bullet-color + (t/testing "a valid color shows as hex with its opacity" + (t/is (= {:color "#ff0000" :opacity 1} (wtc/color-bullet-color "#ff0000"))) + (t/is (= {:color "#ff0000" :opacity 1} (wtc/color-bullet-color "#f00"))) + (t/is (= {:color "#ff0000" :opacity 1} (wtc/color-bullet-color "red"))) + (t/is (= {:color "#ff0000" :opacity 0.5} (wtc/color-bullet-color "rgba(255, 0, 0, 0.5)"))) + (t/is (= {:color "#000000" :opacity 0} (wtc/color-bullet-color "transparent")))) + + (t/testing "invalid colors show no bullet" + (t/is (nil? (wtc/color-bullet-color "not-a-color"))) + (t/is (nil? (wtc/color-bullet-color "ff0000"))) + (t/is (nil? (wtc/color-bullet-color ""))) + (t/is (nil? (wtc/color-bullet-color nil))))) + +(t/deftest resolved-token-bullet-color + (let [color-token (ctob/make-token :name "color.primary" :type :color :value "{color.base}")] + (t/testing "a resolved color token shows its resolved color" + (t/is (= {:color "#00ff00" :opacity 1} + (wtc/resolved-token-bullet-color (assoc color-token :resolved-value "#00ff00"))))) + + (t/testing "an unresolved color token shows no bullet" + (t/is (nil? (wtc/resolved-token-bullet-color color-token))) + (t/is (nil? (wtc/resolved-token-bullet-color (assoc color-token :resolved-value "not-a-color"))))) + + (t/testing "tokens of other types show no bullet, even when their value is a color" + (let [text (ctob/make-token :name "label.warning" :type :string :value "red")] + (t/is (nil? (wtc/resolved-token-bullet-color (assoc text :resolved-value "red")))))))) diff --git a/frontend/test/frontend_tests/tokens/token_errors_test.cljs b/frontend/test/frontend_tests/tokens/token_errors_test.cljs index 02fcbf3201..5089324913 100644 --- a/frontend/test/frontend_tests/tokens/token_errors_test.cljs +++ b/frontend/test/frontend_tests/tokens/token_errors_test.cljs @@ -7,7 +7,16 @@ (ns frontend-tests.tokens.token-errors-test (:require [app.main.data.workspace.tokens.errors :as wte] - [cljs.test :as t :include-macros true])) + [app.util.i18n :as i18n :refer [tr]] + [cljs.test :as t :include-macros true] + [clojure.string :as str])) + +;; The test run loads no translations, so `tr` returns the bare key and +;; drops its arguments. Tests about the values in a message echo them with +;; this stub. Both arities, like `tr`. +(defn- tr-stub + ([key] key) + ([key & args] (str key "(" (str/join ", " args) ")"))) ;; --------------------------------------------------------------------------- ;; resolve-error-message @@ -29,6 +38,15 @@ (let [error {:error/fn nil :message "fallback message"}] (t/is (= "fallback message" (wte/resolve-error-message error)))))) +(t/deftest resolve-error-message-known-error + (t/testing "a known error code shows its translated message" + (t/is (= (tr "errors.tokens.invalid-color" "#zzz") + (wte/resolve-error-message (wte/error-with-value :error.token/invalid-color "#zzz")))))) + +(t/deftest resolve-error-message-without-error + (t/testing "without an error the message is the unknown error" + (t/is (= (tr "labels.unknown-error") (wte/resolve-error-message nil))))) + ;; --------------------------------------------------------------------------- ;; resolve-error-assoc-message ;; --------------------------------------------------------------------------- @@ -51,3 +69,72 @@ (t/testing "returns the error map unchanged when :error/fn is explicitly nil" (let [error {:error/fn nil :message "fallback"}] (t/is (= error (wte/resolve-error-assoc-message error)))))) + +(t/deftest resolve-error-assoc-message-without-error + (t/testing "without an error the message is the unknown error" + (t/is (= {:message (tr "labels.unknown-error")} + (wte/resolve-error-assoc-message nil))))) + +;; --------------------------------------------------------------------------- +;; humanize-errors +;; --------------------------------------------------------------------------- + +(t/deftest humanize-errors + (t/testing "known error codes show their translated message" + (t/is (= [(tr "errors.tokens.empty-input") + (tr "errors.tokens.invalid-color" "#zzz")] + (wte/humanize-errors [(wte/error-with-value :error.token/empty-input nil) + (wte/error-with-value :error.token/invalid-color "#zzz")])))) + + (t/testing "incompatible units are listed in one message" + (with-redefs [i18n/tr tr-stub] + (t/is (= [(tr "errors.tokens.incompatible-units" "px, rem")] + (wte/humanize-errors [(wte/error-with-value :error.token/incompatible-units ["px" "rem"])]))))) + + (t/testing "an unknown error code shows the unknown error message" + (t/is (= [(tr "labels.unknown-error")] + (wte/humanize-errors [(wte/error-with-value :error.token/not-a-code "x")])))) + + (t/testing "reference errors on import show the first reference error after the import error" + (t/is (= [(str (tr "errors.tokens.import-error") "\n\n" "Some token references could not be found")] + (wte/humanize-errors [(wte/error-with-value :error.import/style-dictionary-reference-errors + ["Some token references could not be found" + "color.primary"])])))) + + (t/testing "an error that carries its own message shows it" + (t/is (= ["Value must be positive"] + (wte/humanize-errors [{:error/message "Value must be positive"}])))) + + (t/testing "an error that is already a message shows as is" + (t/is (= ["Plain message"] (wte/humanize-errors ["Plain message"])))) + + (t/testing "no errors give no messages" + (t/is (empty? (wte/humanize-errors []))))) + +;; --------------------------------------------------------------------------- +;; detail-errors +;; --------------------------------------------------------------------------- + +(t/deftest detail-errors + (t/testing "errors without details give no details at all" + (t/is (nil? (wte/detail-errors [(wte/error-with-value :error.token/empty-input nil) + (wte/error-with-value :error.import/json-parse-error nil)]))) + (t/is (nil? (wte/detail-errors [])))) + + (t/testing "an invalid token name on import names the token in the details, escaped for html" + (with-redefs [i18n/tr tr-stub] + (t/is (= [(tr "errors.tokens.invalid-json-token-name-detail" "color.<bad>")] + (wte/detail-errors [(wte/error-with-value :error.import/invalid-token-name "color.")]))))) + + (t/testing "reference errors on import list the remaining references, escaped for html" + (t/is (= ["color.<primary>\n\nspacing & sizing"] + (wte/detail-errors [(wte/error-with-value :error.import/style-dictionary-reference-errors + ["Some token references could not be found" + "color." + "spacing & sizing"])])))) + + (t/testing "only errors with details are listed" + (with-redefs [i18n/tr tr-stub] + (t/is (= [(tr "errors.tokens.invalid-json-token-name-detail" "a.b")] + (wte/detail-errors [(wte/error-with-value :error.token/empty-input nil) + (wte/error-with-value :error.import/invalid-token-name "a.b")])))))) diff --git a/frontend/test/frontend_tests/tokens/token_format_test.cljs b/frontend/test/frontend_tests/tokens/token_format_test.cljs new file mode 100644 index 0000000000..4d24d59ced --- /dev/null +++ b/frontend/test/frontend_tests/tokens/token_format_test.cljs @@ -0,0 +1,72 @@ +;; 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.token-format-test + (:require + [app.common.types.tokens-lib :as ctob] + [app.main.data.workspace.tokens.format :as wtf] + [cljs.test :as t :include-macros true])) + +(t/deftest format-plain-values + (t/testing "a single value shows as text" + (t/is (= "8" (wtf/format-token-value "8"))) + (t/is (= "16" (wtf/format-token-value 16))) + (t/is (= "{color.primary}" (wtf/format-token-value "{color.primary}")))) + + (t/testing "no value shows as empty text" + (t/is (= "" (wtf/format-token-value nil))))) + +(t/deftest format-list-values + (t/testing "a font family list shows comma separated" + (t/is (= "Inter, sans-serif" + (wtf/format-token-value (:value (ctob/make-token :name "font.body" :type :font-family :value ["Inter" "sans-serif"]))))))) + +(t/deftest format-typography-values + (t/testing "a composite typography shows one line per property, with its label" + (let [token (ctob/make-token :name "typo.body" + :type :typography + :value {:font-family ["Inter" "sans-serif"] + :font-size "16px" + :font-weight "bold" + :line-height "1.5" + :letter-spacing "0.05em" + :text-case "uppercase" + :text-decoration "underline"})] + (t/is (= (str "\n- Font Family: Inter, sans-serif" + "\n- Font Size: 16px" + "\n- Font Weight: bold" + "\n- Line Height: 1.5" + "\n- Letter Spacing: 0.05em" + "\n- Text Case: uppercase" + "\n- Text Decoration: underline") + (wtf/format-token-value (:value token)))))) + + (t/testing "property names given as strings get their label too" + (t/is (= "\n- Font Size: 16px" (wtf/format-token-value {"font-size" "16px"}))))) + +(t/deftest format-shadow-values + (let [shadow-1 {:offset-x "0" :offset-y "2px" :blur "4px" :spread "0" :color "#000000" :inset false} + shadow-2 {:offset-x "1" :offset-y "8px" :blur "16px" :spread "2px" :color "#ff0000" :inset true} + lines-1 (str "\n- X: 0" + "\n- Y: 2px" + "\n- Blur: 4px" + "\n- Spread: 0" + "\n- Color: #000000" + "\n- Inner Shadow: false") + lines-2 (str "\n- X: 1" + "\n- Y: 8px" + "\n- Blur: 16px" + "\n- Spread: 2px" + "\n- Color: #ff0000" + "\n- Inner Shadow: true")] + + (t/testing "a shadow shows one line per property, with its label" + (let [token (ctob/make-token :name "shadow.sm" :type :shadow :value [shadow-1])] + (t/is (= lines-1 (wtf/format-token-value (:value token)))))) + + (t/testing "several shadows show one after the other" + (let [token (ctob/make-token :name "shadow.md" :type :shadow :value [shadow-1 shadow-2])] + (t/is (= (str lines-1 "\n" lines-2) (wtf/format-token-value (:value token)))))))) diff --git a/frontend/test/frontend_tests/tokens/token_warnings_test.cljs b/frontend/test/frontend_tests/tokens/token_warnings_test.cljs new file mode 100644 index 0000000000..9f1cf24778 --- /dev/null +++ b/frontend/test/frontend_tests/tokens/token_warnings_test.cljs @@ -0,0 +1,30 @@ +;; 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.token-warnings-test + (:require + [app.main.data.workspace.tokens.warnings :as wtw] + [app.util.i18n :refer [tr]] + [cljs.test :as t :include-macros true])) + +(t/deftest humanize-warnings + (t/testing "an out of range opacity shows the resolved value and the allowed range" + (t/is (= [(str (tr "workspace.tokens.resolved-value" 1.5) ".\n" (tr "errors.tokens.opacity-range"))] + (wtw/humanize-warnings [(wtw/warning-with-value :warning.style-dictionary/invalid-referenced-token-value-opacity 1.5)])))) + + (t/testing "an out of range stroke width shows the resolved value and the allowed range" + (t/is (= [(str (tr "workspace.tokens.resolved-value" -2) ".\n" (tr "errors.tokens.stroke-width-range"))] + (wtw/humanize-warnings [(wtw/warning-with-value :warning.style-dictionary/invalid-referenced-token-value-stroke-width -2)])))) + + (t/testing "an unknown warning code shows the unknown warning message" + (t/is (= ["Unknown warning"] + (wtw/humanize-warnings [(wtw/warning-with-value :warning.style-dictionary/not-a-code 1)])))) + + (t/testing "a warning that is already a message shows as is" + (t/is (= ["Plain message"] (wtw/humanize-warnings ["Plain message"])))) + + (t/testing "no warnings give no messages" + (t/is (empty? (wtw/humanize-warnings nil)))))