From 8b4e52a2be51772b04b3ca643dd73a2d85fc3592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Tue, 7 May 2024 18:07:03 +0200 Subject: [PATCH] :lipstick: Improve copy child selection and test robustness --- .../test/common_tests/helpers/components.cljc | 1 + .../common_tests/helpers/compositions.cljc | 6 +- ...tion_test.cljc => comp_creation_test.cljc} | 2 +- .../common_tests/logic/comp_reset_test.cljc | 106 ++++++++++------ ...uched_test.cljc => comp_touched_test.cljc} | 119 ++++++++++-------- 5 files changed, 140 insertions(+), 94 deletions(-) rename common/test/common_tests/logic/{component_creation_test.cljc => comp_creation_test.cljc} (97%) rename common/test/common_tests/logic/{components_touched_test.cljc => comp_touched_test.cljc} (71%) diff --git a/common/test/common_tests/helpers/components.cljc b/common/test/common_tests/helpers/components.cljc index 56ebf488d..a8657c325 100644 --- a/common/test/common_tests/helpers/components.cljc +++ b/common/test/common_tests/helpers/components.cljc @@ -129,6 +129,7 @@ (when children-labels (dotimes [idx (count children-labels)] (set-child-label file' copy-root-label idx (nth children-labels idx)))) + file')) (defn component-swap diff --git a/common/test/common_tests/helpers/compositions.cljc b/common/test/common_tests/helpers/compositions.cljc index cf1a02d9a..21f4217d0 100644 --- a/common/test/common_tests/helpers/compositions.cljc +++ b/common/test/common_tests/helpers/compositions.cljc @@ -140,8 +140,8 @@ component2-params))) (defn add-nested-component-with-copy - [file component1-label main1-root-label main1-child-label component2-label main2-root-label nested-head-label copy2-label - & {:keys [component1-params root1-params main1-child-params component2-params main2-root-params nested-head-params copy2-params]}] + [file component1-label main1-root-label main1-child-label component2-label main2-root-label nested-head-label copy2-root-label + & {:keys [component1-params root1-params main1-child-params component2-params main2-root-params nested-head-params copy2-root-params]}] ;; Generated shape tree: ;; {:main1-root-label} [:name: Frame1] # [Component :component1-label] ;; :main1-child-label [:name: Rect1] @@ -166,4 +166,4 @@ :component2-params component2-params :main2-root-params main2-root-params :nested-head-params nested-head-params) - (thc/instantiate-component component2-label copy2-label copy2-params))) \ No newline at end of file + (thc/instantiate-component component2-label copy2-root-label copy2-root-params))) \ No newline at end of file diff --git a/common/test/common_tests/logic/component_creation_test.cljc b/common/test/common_tests/logic/comp_creation_test.cljc similarity index 97% rename from common/test/common_tests/logic/component_creation_test.cljc rename to common/test/common_tests/logic/comp_creation_test.cljc index 13a7533b8..bed320a0a 100644 --- a/common/test/common_tests/logic/component_creation_test.cljc +++ b/common/test/common_tests/logic/comp_creation_test.cljc @@ -4,7 +4,7 @@ ;; ;; Copyright (c) KALEIDOS INC -(ns common-tests.logic.component-creation-test +(ns common-tests.logic.comp-creation-test (:require [app.common.files.changes-builder :as pcb] [app.common.logic.libraries :as cll] diff --git a/common/test/common_tests/logic/comp_reset_test.cljc b/common/test/common_tests/logic/comp_reset_test.cljc index 22c4a9538..fecab3362 100644 --- a/common/test/common_tests/logic/comp_reset_test.cljc +++ b/common/test/common_tests/logic/comp_reset_test.cljc @@ -20,22 +20,24 @@ (t/deftest test-reset-after-changing-attribute (let [;; ==== Setup - file (-> (thf/sample-file :file1) - (tho/add-simple-component-with-copy :component1 - :main-root - :main-child - :copy-root - :main-child-params {:fills (ths/sample-fills-color - :fill-color "#abcdef")})) - page (thf/current-page file) - copy-root (ths/get-shape file :copy-root) + file (-> (thf/sample-file :file1) + (tho/add-simple-component-with-copy :component1 + :main-root + :main-child + :copy-root + :main-child-params {:fills (ths/sample-fills-color + :fill-color "#abcdef")} + :copy-root-params {:children-labels [:copy-child]})) + page (thf/current-page file) + copy-root (ths/get-shape file :copy-root) + copy-child (ths/get-shape file :copy-child) ;; ==== Action update-fn (fn [shape] (assoc shape :fills (ths/sample-fills-color :fill-color "#fabada"))) changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) - (:shapes copy-root) + #{(:id copy-child)} update-fn (:objects page) {}) @@ -54,11 +56,13 @@ ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root'))) + copy-child' (ths/get-shape file' :copy-child) fills' (:fills copy-child') fill' (first fills')] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (count fills') 1)) (t/is (= (:fill-color fill') "#abcdef")) (t/is (= (:fill-opacity fill') 1)) @@ -67,23 +71,26 @@ (t/deftest test-reset-from-library (let [;; ==== Setup - library (-> (thf/sample-file :library :is-shared true) - (tho/add-simple-component :component1 :main-root :main-child - :child-params {:fills (ths/sample-fills-color - :fill-color "#abcdef")})) + library (-> (thf/sample-file :library :is-shared true) + (tho/add-simple-component :component1 :main-root :main-child + :child-params {:fills (ths/sample-fills-color + :fill-color "#abcdef")})) - file (-> (thf/sample-file :file) - (thc/instantiate-component :component1 :copy-root :library library)) + file (-> (thf/sample-file :file) + (thc/instantiate-component :component1 :copy-root + :library library + :children-labels [:copy-child])) - page (thf/current-page file) - copy-root (ths/get-shape file :copy-root) + page (thf/current-page file) + copy-root (ths/get-shape file :copy-root) + copy-child (ths/get-shape file :copy-child) ;; ==== Action update-fn (fn [shape] (assoc shape :fills (ths/sample-fills-color :fill-color "#fabada"))) changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) - (:shapes copy-root) + #{(:id copy-child)} update-fn (:objects page) {}) @@ -103,11 +110,13 @@ ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root'))) + copy-child' (ths/get-shape file' :copy-child) fills' (:fills copy-child') fill' (first fills')] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (count fills') 1)) (t/is (= (:fill-color fill') "#abcdef")) (t/is (= (:fill-opacity fill') 1)) @@ -120,7 +129,8 @@ (tho/add-simple-component-with-copy :component1 :main-root :main-child - :copy-root) + :copy-root + :copy-root-params {:children-labels [:copy-child]}) (ths/add-sample-shape :free-shape)) page (thf/current-page file) @@ -152,9 +162,11 @@ ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root')))] + copy-child' (ths/get-shape file' :copy-child)] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (:touched copy-root') nil)) (t/is (= (:touched copy-child') nil)))) @@ -164,10 +176,12 @@ (tho/add-simple-component-with-copy :component1 :main-root :main-child - :copy-root)) + :copy-root + :copy-root-params {:children-labels [:copy-child]})) page (thf/current-page file) copy-root (ths/get-shape file :copy-root) + copy-child (ths/get-shape file :copy-child) ;; ==== Action @@ -178,7 +192,7 @@ file page (:objects page) - (set (:shapes copy-root)) + #{(:id copy-child)} {:components-v2 true}) file-mdf (thf/apply-changes file changes) @@ -195,9 +209,11 @@ ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root')))] + copy-child' (ths/get-shape file' :copy-child)] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (:touched copy-root') nil)) (t/is (= (:touched copy-child') nil)))) @@ -207,12 +223,13 @@ (tho/add-component-with-many-children-and-copy :component1 :main-root [:main-child1 :main-child2 :main-child3] - :copy-root) + :copy-root + :copy-root-params {:children-labels [:copy-child]}) (ths/add-sample-shape :free-shape)) page (thf/current-page file) copy-root (ths/get-shape file :copy-root) - copy-child1 (ths/get-shape-by-id file (first (:shapes copy-root))) + copy-child1 (ths/get-shape file :copy-child) ;; ==== Action @@ -240,9 +257,11 @@ ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root')))] + copy-child' (ths/get-shape file' :copy-child)] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (:touched copy-root') nil)) (t/is (= (:touched copy-child') nil)))) @@ -289,6 +308,7 @@ fill' (first fills')] ;; ==== Check + (t/is (some? copy2-root')) (t/is (= (count fills') 1)) (t/is (= (:fill-color fill') "#abcdef")) (t/is (= (:fill-opacity fill') 1)) @@ -296,23 +316,25 @@ (t/deftest test-reset-after-changing-lower (let [;; ==== Setup - file (-> (thf/sample-file :file1) - (tho/add-nested-component-with-copy :component1 - :main1-root - :main1-child - :component2 - :main2-root - :main2-nested-head - :copy2-root)) - page (thf/current-page file) - copy2-root (ths/get-shape file :copy2-root) + file (-> (thf/sample-file :file1) + (tho/add-nested-component-with-copy :component1 + :main1-root + :main1-child + :component2 + :main2-root + :main2-nested-head + :copy2-root + :copy2-root-params {:children-labels [:copy2-child]})) + page (thf/current-page file) + copy2-root (ths/get-shape file :copy2-root) + copy2-child (ths/get-shape file :copy2-child) ;; ==== Action update-fn (fn [shape] (assoc shape :fills (ths/sample-fills-color :fill-color "#fabada"))) changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) - (:shapes copy2-root) + #{(:id copy2-child)} update-fn (:objects page) {}) @@ -331,11 +353,13 @@ ;; ==== Get copy2-root' (ths/get-shape file' :copy2-root) - copy2-child' (ths/get-shape-by-id file' (first (:shapes copy2-root'))) + copy2-child' (ths/get-shape file' :copy2-child) fills' (:fills copy2-child') fill' (first fills')] ;; ==== Check + (t/is (some? copy2-root')) + (t/is (some? copy2-child')) (t/is (= (count fills') 1)) (t/is (= (:fill-color fill') "#FFFFFF")) (t/is (= (:fill-opacity fill') 1)) diff --git a/common/test/common_tests/logic/components_touched_test.cljc b/common/test/common_tests/logic/comp_touched_test.cljc similarity index 71% rename from common/test/common_tests/logic/components_touched_test.cljc rename to common/test/common_tests/logic/comp_touched_test.cljc index 426e9c3cb..c4f658fea 100644 --- a/common/test/common_tests/logic/components_touched_test.cljc +++ b/common/test/common_tests/logic/comp_touched_test.cljc @@ -4,7 +4,7 @@ ;; ;; Copyright (c) KALEIDOS INC -(ns common-tests.logic.components-touched-test +(ns common-tests.logic.comp-touched-test (:require [app.common.files.changes-builder :as pcb] [app.common.logic.shapes :as cls] @@ -19,22 +19,23 @@ (t/deftest test-touched-when-changing-attribute (let [;; ==== Setup - file (-> (thf/sample-file :file1) - (tho/add-simple-component-with-copy :component1 - :main-root - :main-child - :copy-root - :main-child-params {:fills (ths/sample-fills-color - :fill-color "#abcdef")})) - page (thf/current-page file) - copy-root (ths/get-shape file :copy-root) + file (-> (thf/sample-file :file1) + (tho/add-simple-component-with-copy :component1 + :main-root + :main-child + :copy-root + :main-child-params {:fills (ths/sample-fills-color + :fill-color "#abcdef")} + :copy-root-params {:children-labels [:copy-child]})) + page (thf/current-page file) + copy-child (ths/get-shape file :copy-child) ;; ==== Action update-fn (fn [shape] (assoc shape :fills (ths/sample-fills-color :fill-color "#fabada"))) changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) - (:shapes copy-root) + #{(:id copy-child)} update-fn (:objects page) {}) @@ -43,11 +44,13 @@ ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root'))) + copy-child' (ths/get-shape file' :copy-child) fills' (:fills copy-child') fill' (first fills')] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (count fills') 1)) (t/is (= (:fill-color fill') "#fabada")) (t/is (= (:fill-opacity fill') 1)) @@ -56,23 +59,25 @@ (t/deftest test-touched-from-library (let [;; ==== Setup - library (-> (thf/sample-file :library :is-shared true) - (tho/add-simple-component :component1 :main-root :main-child - :child-params {:fills (ths/sample-fills-color - :fill-color "#abcdef")})) + library (-> (thf/sample-file :library :is-shared true) + (tho/add-simple-component :component1 :main-root :main-child + :child-params {:fills (ths/sample-fills-color + :fill-color "#abcdef")})) - file (-> (thf/sample-file :file) - (thc/instantiate-component :component1 :copy-root :library library)) + file (-> (thf/sample-file :file) + (thc/instantiate-component :component1 :copy-root + :library library + :children-labels [:copy-child])) - page (thf/current-page file) - copy-root (ths/get-shape file :copy-root) + page (thf/current-page file) + copy-child (ths/get-shape file :copy-child) ;; ==== Action update-fn (fn [shape] (assoc shape :fills (ths/sample-fills-color :fill-color "#fabada"))) changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) - (:shapes copy-root) + #{(:id copy-child)} update-fn (:objects page) {}) @@ -81,11 +86,13 @@ ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root'))) + copy-child' (ths/get-shape file' :copy-child) fills' (:fills copy-child') fill' (first fills')] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (count fills') 1)) (t/is (= (:fill-color fill') "#fabada")) (t/is (= (:fill-opacity fill') 1)) @@ -94,12 +101,13 @@ (t/deftest test-not-touched-when-adding-shape (let [;; ==== Setup - file (-> (thf/sample-file :file1) - (tho/add-simple-component-with-copy :component1 - :main-root - :main-child - :copy-root) - (ths/add-sample-shape :free-shape)) + file (-> (thf/sample-file :file1) + (tho/add-simple-component-with-copy :component1 + :main-root + :main-child + :copy-root + :copy-root-params {:children-labels [:copy-child]}) + (ths/add-sample-shape :free-shape)) page (thf/current-page file) copy-root (ths/get-shape file :copy-root) @@ -120,9 +128,11 @@ ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root')))] + copy-child' (ths/get-shape file' :copy-child)] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (:touched copy-root') nil)) (t/is (= (:touched copy-child') nil)))) @@ -132,10 +142,11 @@ (tho/add-simple-component-with-copy :component1 :main-root :main-child - :copy-root)) + :copy-root + :copy-root-params {:children-labels [:copy-child]})) page (thf/current-page file) - copy-root (ths/get-shape file :copy-root) + copy-child (ths/get-shape file :copy-child) ;; ==== Action @@ -146,16 +157,18 @@ file page (:objects page) - (set (:shapes copy-root)) + #{(:id copy-child)} {:components-v2 true}) file' (thf/apply-changes file changes) ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root')))] + copy-child' (ths/get-shape file' :copy-child)] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (:touched copy-root') nil)) (t/is (= (:touched copy-child') #{:visibility-group})))) @@ -165,12 +178,14 @@ (tho/add-component-with-many-children-and-copy :component1 :main-root [:main-child1 :main-child2 :main-child3] - :copy-root) + :copy-root + :copy-root-params {:children-labels [:copy-child1 + :copy-child2 + :copy-child3]}) (ths/add-sample-shape :free-shape)) page (thf/current-page file) - copy-root (ths/get-shape file :copy-root) - copy-child1 (ths/get-shape-by-id file (first (:shapes copy-root))) + copy-child1 (ths/get-shape file :copy-child1) ;; ==== Action @@ -188,9 +203,11 @@ ;; ==== Get copy-root' (ths/get-shape file' :copy-root) - copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root')))] + copy-child' (ths/get-shape file' :copy-child1)] ;; ==== Check + (t/is (some? copy-root')) + (t/is (some? copy-child')) (t/is (= (:touched copy-root') nil)) (t/is (= (:touched copy-child') nil)))) @@ -227,6 +244,7 @@ fill' (first fills')] ;; ==== Check + (t/is (some? copy2-root')) (t/is (= (count fills') 1)) (t/is (= (:fill-color fill') "#fabada")) (t/is (= (:fill-opacity fill') 1)) @@ -234,23 +252,24 @@ (t/deftest test-touched-when-changing-lower (let [;; ==== Setup - file (-> (thf/sample-file :file1) - (tho/add-nested-component-with-copy :component1 - :main1-root - :main1-child - :component2 - :main2-root - :main2-nested-head - :copy2-root)) - page (thf/current-page file) - copy2-root (ths/get-shape file :copy2-root) + file (-> (thf/sample-file :file1) + (tho/add-nested-component-with-copy :component1 + :main1-root + :main1-child + :component2 + :main2-root + :main2-nested-head + :copy2-root + :copy2-root-params {:children-labels [:copy2-child]})) + page (thf/current-page file) + copy2-child (ths/get-shape file :copy2-child) ;; ==== Action update-fn (fn [shape] (assoc shape :fills (ths/sample-fills-color :fill-color "#fabada"))) changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) - (:shapes copy2-root) + #{(:id copy2-child)} update-fn (:objects page) {}) @@ -259,11 +278,13 @@ ;; ==== Get copy2-root' (ths/get-shape file' :copy2-root) - copy2-child' (ths/get-shape-by-id file' (first (:shapes copy2-root'))) + copy2-child' (ths/get-shape file' :copy2-child) fills' (:fills copy2-child') fill' (first fills')] ;; ==== Check + (t/is (some? copy2-root')) + (t/is (some? copy2-child')) (t/is (= (count fills') 1)) (t/is (= (:fill-color fill') "#fabada")) (t/is (= (:fill-opacity fill') 1))