mirror of
https://github.com/penpot/penpot.git
synced 2026-11-18 16:12:24 +00:00
🐛 Fix clicking a set in the theme editor (#13067)
Clicking a set's row in the theme editor threw an assertion error and left the set as it was: the row passed the set's name to the toggle, which looks the set up by id. The row now uses the checkbox's handler, so the two can't drift apart again. The editor's set and group toggles move into pure functions next to it, with tests. A new spec clicks a set's name to tick and untick it. Signed-off-by: Florian Schroedl <flo.schroedl@gmail.com>
This commit is contained in:
parent
445d02e012
commit
8b8def3b6e
@ -291,4 +291,30 @@ test.describe("Tokens: Themes modal", () => {
|
||||
tokenThemeUpdateCreateModal.getByText("Changed Group name"),
|
||||
).toBeVisible();
|
||||
});
|
||||
|
||||
test("Clicking a set name ticks and unticks it", async ({ page }) => {
|
||||
const { tokenThemeUpdateCreateModal, workspacePage } =
|
||||
await setupTokensFileRender(page);
|
||||
|
||||
await workspacePage.openTokenThemesModal();
|
||||
|
||||
await tokenThemeUpdateCreateModal
|
||||
.getByText("3 active sets")
|
||||
.first()
|
||||
.click();
|
||||
|
||||
const darkSet = tokenThemeUpdateCreateModal.getByRole("button", {
|
||||
name: "dark",
|
||||
exact: true,
|
||||
});
|
||||
const darkCheckbox = darkSet.getByRole("checkbox");
|
||||
|
||||
await expect(darkCheckbox).not.toBeChecked();
|
||||
|
||||
await darkSet.click();
|
||||
await expect(darkCheckbox).toBeChecked();
|
||||
|
||||
await darkSet.click();
|
||||
await expect(darkCheckbox).not.toBeChecked();
|
||||
});
|
||||
});
|
||||
|
||||
@ -260,6 +260,23 @@
|
||||
(ctos/make-tokens-status :active-theme-ids #{(:id theme)}
|
||||
:active-set-ids (into #{} (map ctob/get-id sets)))))
|
||||
|
||||
(defn- toggle-set
|
||||
"Ticks the set in the theme being edited, or unticks it when it is
|
||||
ticked already."
|
||||
[theme tokens-lib set-id]
|
||||
(let [set (ctob/get-set tokens-lib set-id)]
|
||||
(ctob/toggle-set theme (ctob/get-name set))))
|
||||
|
||||
(defn- toggle-set-group
|
||||
"Ticks every set in the group for the theme being edited. When some
|
||||
or all of them are ticked already, unticks them all."
|
||||
[theme sets group-path]
|
||||
(let [lib (make-lib-with-theme theme sets)
|
||||
set-names (into #{} (map ctob/get-name) (ctob/get-sets-at-path lib group-path))]
|
||||
(if (some (:sets theme) set-names)
|
||||
(ctob/disable-sets theme set-names)
|
||||
(ctob/enable-sets theme set-names))))
|
||||
|
||||
(mf/defc edit-create-theme*
|
||||
[{:keys [change-view theme on-save is-editing has-prev-view]}]
|
||||
(let [ordered-token-sets (mf/deref refs/workspace-ordered-token-sets)
|
||||
@ -325,33 +342,15 @@
|
||||
|
||||
on-toggle-token-set
|
||||
(mf/use-fn
|
||||
(mf/deps current-theme tokens-lib)
|
||||
(mf/deps tokens-lib)
|
||||
(fn [set-id]
|
||||
(let [set (ctob/get-set tokens-lib set-id)]
|
||||
(swap! current-theme* #(ctob/toggle-set % (ctob/get-name set))))))
|
||||
(swap! current-theme* toggle-set tokens-lib set-id)))
|
||||
|
||||
on-toggle-token-set-group
|
||||
(mf/use-fn
|
||||
(mf/deps current-theme ordered-token-sets)
|
||||
(mf/deps ordered-token-sets)
|
||||
(fn [group-path]
|
||||
(swap! current-theme* (fn [theme']
|
||||
; TODOstatus ver si se puede añadir una función en files.tokens o types.tokens
|
||||
; según si necesitamos al final el status o no (que creo que no)
|
||||
(let [lib' (make-lib-with-theme theme' ordered-token-sets)
|
||||
status' (make-status-with-theme lib' theme')
|
||||
active? (cfo/sets-at-path-all-active? status' lib' group-path)
|
||||
sets-at-path (ctob/get-sets-at-path lib' group-path)
|
||||
set-names (into #{} (map ctob/get-name) sets-at-path)]
|
||||
(if (contains? #{:all :partial} active?)
|
||||
(ctob/disable-sets theme' set-names)
|
||||
(ctob/enable-sets theme' set-names)))))))
|
||||
|
||||
on-click-token-set
|
||||
(mf/use-fn
|
||||
(mf/deps on-toggle-token-set)
|
||||
(fn [set-id]
|
||||
(let [set (ctob/get-set lib-with-theme set-id)]
|
||||
(on-toggle-token-set (ctob/get-name set)))))]
|
||||
(swap! current-theme* toggle-set-group ordered-token-sets group-path)))]
|
||||
|
||||
[:div {:class (stl/css :themes-modal-wrapper)}
|
||||
[:> heading* {:level 2 :typography "headline-medium" :class (stl/css :themes-modal-title)}
|
||||
@ -379,7 +378,7 @@
|
||||
:token-sets token-sets
|
||||
:is-token-set-active token-set-active?
|
||||
:is-token-set-group-active token-set-group-active?
|
||||
:on-select on-click-token-set
|
||||
:on-select on-toggle-token-set
|
||||
:can-edit false
|
||||
:on-toggle-token-set on-toggle-token-set
|
||||
:on-toggle-token-set-group on-toggle-token-set-group
|
||||
|
||||
@ -140,6 +140,7 @@
|
||||
[frontend-tests.tokens.referenced-token-value-test]
|
||||
[frontend-tests.tokens.shadow-form-test]
|
||||
[frontend-tests.tokens.style-dictionary-test]
|
||||
[frontend-tests.tokens.theme-create-modal-test]
|
||||
[frontend-tests.tokens.token-application-test]
|
||||
[frontend-tests.tokens.token-errors-test]
|
||||
[frontend-tests.tokens.token-group-test]
|
||||
@ -359,6 +360,7 @@
|
||||
'frontend-tests.tokens.referenced-token-value-test
|
||||
'frontend-tests.tokens.shadow-form-test
|
||||
'frontend-tests.tokens.style-dictionary-test
|
||||
'frontend-tests.tokens.theme-create-modal-test
|
||||
'frontend-tests.tokens.token-application-test
|
||||
'frontend-tests.tokens.token-errors-test
|
||||
'frontend-tests.tokens.token-group-test
|
||||
|
||||
@ -0,0 +1,79 @@
|
||||
;; 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.theme-create-modal-test
|
||||
"The sets list of the theme editor: which sets it shows as ticked, and
|
||||
what ticking a set or a group does to the theme."
|
||||
(:require
|
||||
[app.common.files.tokens :as cfo]
|
||||
[app.common.types.tokens-lib :as ctob]
|
||||
[app.common.types.tokens-status :as ctos]
|
||||
[app.main.ui.workspace.tokens.themes.create-modal :as create-modal]
|
||||
[cljs.test :as t :include-macros true]))
|
||||
|
||||
(def ^:private toggle-set @#'create-modal/toggle-set)
|
||||
(def ^:private toggle-set-group @#'create-modal/toggle-set-group)
|
||||
(def ^:private make-lib-with-theme @#'create-modal/make-lib-with-theme)
|
||||
(def ^:private make-status-with-theme @#'create-modal/make-status-with-theme)
|
||||
|
||||
(defn- sample-lib
|
||||
"A library with the set `light` and the group `brand` holding the sets
|
||||
`a` and `b` and the subgroup `x` with the set `c`."
|
||||
[]
|
||||
(-> (ctob/make-tokens-lib)
|
||||
(ctob/add-set (ctob/make-token-set :name "light"))
|
||||
(ctob/add-set (ctob/make-token-set :name "brand/a"))
|
||||
(ctob/add-set (ctob/make-token-set :name "brand/b"))
|
||||
(ctob/add-set (ctob/make-token-set :name "brand/x/c"))))
|
||||
|
||||
(defn- sample-theme
|
||||
[& set-names]
|
||||
(ctob/make-token-theme :name "Light" :sets (set set-names)))
|
||||
|
||||
(defn- set-id
|
||||
[lib set-name]
|
||||
(ctob/get-id (ctob/get-set-by-name lib set-name)))
|
||||
|
||||
(t/deftest toggle-set-ticks-and-unticks-by-id
|
||||
(let [lib (sample-lib)
|
||||
id (set-id lib "light")
|
||||
ticked (toggle-set (sample-theme) lib id)]
|
||||
(t/is (= #{"light"} (:sets ticked)))
|
||||
(t/is (= #{} (:sets (toggle-set ticked lib id))))))
|
||||
|
||||
(t/deftest toggle-set-keeps-the-other-sets
|
||||
(let [lib (sample-lib)]
|
||||
(t/is (= #{"brand/a" "light"}
|
||||
(:sets (toggle-set (sample-theme "brand/a") lib (set-id lib "light")))))))
|
||||
|
||||
(t/deftest toggle-set-group-ticks-all-its-sets-when-none-is-ticked
|
||||
(let [sets (ctob/get-sets (sample-lib))]
|
||||
(t/is (= #{"light" "brand/a" "brand/b" "brand/x/c"}
|
||||
(:sets (toggle-set-group (sample-theme "light") sets ["brand"]))))))
|
||||
|
||||
(t/deftest toggle-set-group-unticks-all-its-sets-when-some-are-ticked
|
||||
(let [sets (ctob/get-sets (sample-lib))]
|
||||
(t/is (= #{"light"}
|
||||
(:sets (toggle-set-group (sample-theme "light" "brand/a") sets ["brand"]))))))
|
||||
|
||||
(t/deftest toggle-set-group-unticks-all-its-sets-when-only-a-nested-one-is-ticked
|
||||
(let [sets (ctob/get-sets (sample-lib))]
|
||||
(t/is (= #{"light"}
|
||||
(:sets (toggle-set-group (sample-theme "light" "brand/x/c") sets ["brand"]))))))
|
||||
|
||||
(t/deftest toggle-set-group-unticks-all-its-sets-when-all-are-ticked
|
||||
(let [sets (ctob/get-sets (sample-lib))]
|
||||
(t/is (= #{}
|
||||
(:sets (toggle-set-group (sample-theme "brand/a" "brand/b" "brand/x/c") sets ["brand"]))))))
|
||||
|
||||
(t/deftest the-editor-shows-the-sets-of-the-theme-as-ticked
|
||||
(let [lib (sample-lib)
|
||||
theme (sample-theme "brand/a")
|
||||
lib' (make-lib-with-theme theme (ctob/get-sets lib))
|
||||
status (make-status-with-theme lib' theme)]
|
||||
(t/is (ctos/set-active? status (set-id lib "brand/a")))
|
||||
(t/is (not (ctos/set-active? status (set-id lib "light"))))
|
||||
(t/is (= :partial (cfo/sets-at-path-all-active? status lib' ["brand"])))))
|
||||
Loading…
x
Reference in New Issue
Block a user