From 3e5126251c194a4cf98e9b7f1b0dca69495c65ef Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Tue, 2 Jul 2024 15:19:31 +0200 Subject: [PATCH] Add failing logic test --- .../app/main/ui/workspace/tokens/core.cljs | 5 +- .../token_tests/logic/token_actions_test.cljs | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 frontend/test/token_tests/logic/token_actions_test.cljs diff --git a/frontend/src/app/main/ui/workspace/tokens/core.cljs b/frontend/src/app/main/ui/workspace/tokens/core.cljs index b375109da..9a025362d 100644 --- a/frontend/src/app/main/ui/workspace/tokens/core.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/core.cljs @@ -59,10 +59,9 @@ shapes)] (and (empty? with-token) (seq without-token)))) -(defn on-add-token [{:keys [token-type-props token shapes] :as _props}] +(defn on-add-token [{:keys [token-type-props token shape-ids] :as _props}] (p/let [sd-tokens (sd/resolve-workspace-tokens+)] (let [{:keys [attributes on-update-shape]} token-type-props - shape-ids (map :id shapes) resolved-value (-> (get sd-tokens (:id token)) (resolve-token-value)) tokenized-attributes (->> (map (fn [attr] {attr (:id token)}) attributes) @@ -84,7 +83,7 @@ (let [remove-tokens? (wtt/shapes-token-applied? token shapes (:attributes token-type-props))] (if remove-tokens? (on-remove-token props) - (on-add-token props)))) + (on-add-token (assoc props :shape-ids (map :id shapes)))))) (defn on-apply-token [{:keys [token token-type-props selected-shapes] :as _props}] (let [{:keys [attributes on-apply on-update-shape] diff --git a/frontend/test/token_tests/logic/token_actions_test.cljs b/frontend/test/token_tests/logic/token_actions_test.cljs new file mode 100644 index 000000000..18711cb39 --- /dev/null +++ b/frontend/test/token_tests/logic/token_actions_test.cljs @@ -0,0 +1,72 @@ +(ns token-tests.logic.token-actions-test + (:require + [app.common.test-helpers.compositions :as ctho] + [app.common.test-helpers.files :as cthf] + [app.common.test-helpers.ids-map :as cthi] + [app.main.ui.workspace.tokens.core :as wtc] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.pages :as thp] + [frontend-tests.helpers.state :as ths])) + +(t/use-fixtures :each + {:before thp/reset-idmap!}) + +(defn- setup-file + [] + (-> (cthf/sample-file :file-1) + (ctho/add-rect :file-1 :rect-1 {}) + (ctho/add-rect :file-1 :rect-2 {}) + (ctho/add-rect :file-1 :rect-3 {}))) + +(t/deftest test-update-shape + (t/async + done + (let [;; ==== Setup + file (setup-file) + store (ths/setup-store file) + + ;; ==== Action + events [(wtc/on-add-token {:token-type-props {:attributes {:rx :ry} + :on-update-shape #(fn [& _])} + :token {:id (random-uuid)}})]] + (ths/run-store + store done events + (fn [new-state] + (let [;; ==== Get + shape1' (get-in new-state [:workspace-data + :pages-index + (cthi/id :page1) + :objects + (cthi/id :shape1)]) + fills' (:fills shape1') + fill' (first fills')] + + ;; ==== Check + (t/is (some? shape1')) + (t/is (= (count fills') 1)) + (t/is (= (:fill-color fill') "#fabada")) + (t/is (= (:fill-opacity fill') 1)))))))) + + + +(comment + (defn make-printable + "Convert records that are not printable by cider inspect into regular maps." + [coll] + (letfn [(stringifyable? [x] + (not (or (map? x) + (sequential? x) + (keyword? x) + (number? x) + (uuid? x))))] + (clojure.walk/postwalk #(cond->> % + (record? %) (into {}) + (stringifyable? %) str) + coll))) + + (-> (cthf/sample-file :file-1) + (assoc :tokens {}) + (make-printable)) + + (make-printable (setup-file)) + nil)