diff --git a/common/src/app/common/test_helpers/compositions.cljc b/common/src/app/common/test_helpers/compositions.cljc index 1a9d8f00af..f5c9b5a1ca 100644 --- a/common/src/app/common/test_helpers/compositions.cljc +++ b/common/src/app/common/test_helpers/compositions.cljc @@ -307,6 +307,7 @@ file' (thf/apply-changes file changes)] (when new-shape-label + (thi/rm-id! (:id new-shape)) (thi/set-id! new-shape-label (:id new-shape))) (if propagate-fn (propagate-fn file') diff --git a/common/src/app/common/test_helpers/ids_map.cljc b/common/src/app/common/test_helpers/ids_map.cljc index b249962874..5dd09f95fd 100644 --- a/common/src/app/common/test_helpers/ids_map.cljc +++ b/common/src/app/common/test_helpers/ids_map.cljc @@ -21,6 +21,10 @@ [label id] (swap! idmap assoc label id)) +(defn rm-id! + [id] + (swap! idmap #(into {} (remove (comp #{id} val) %)))) + (defn new-id! [label] (let [id (uuid/next)] diff --git a/common/src/app/common/test_helpers/variants.cljc b/common/src/app/common/test_helpers/variants.cljc index 8a886e4200..4975e7c2a6 100644 --- a/common/src/app/common/test_helpers/variants.cljc +++ b/common/src/app/common/test_helpers/variants.cljc @@ -56,6 +56,22 @@ (thc/update-component component2-label {:variant-id variant-id :variant-properties [{:name "Property 1" :value "Value2"}]})))) +(defn add-variant-with-copy + [file variant-label component1-label root1-label component2-label root2-label child1-label child2-label component-copy-label] + (let [file (ths/add-sample-shape file variant-label :type :frame :is-variant-container true) + variant-id (thi/id variant-label)] + (-> file + (ths/add-sample-shape root2-label :type :frame :parent-label variant-label :variant-id variant-id :variant-name "Value2") + (ths/add-sample-shape root1-label :type :frame :parent-label variant-label :variant-id variant-id :variant-name "Value1") + (thc/instantiate-component component-copy-label child1-label :parent-label root1-label) + (thc/instantiate-component component-copy-label child2-label :parent-label root2-label) + (thc/make-component component1-label root1-label) + (thc/update-component component1-label {:variant-id variant-id :variant-properties [{:name "Property 1" :value "Value1"}]}) + (thc/make-component component2-label root2-label) + (thc/update-component component2-label {:variant-id variant-id :variant-properties [{:name "Property 1" :value "Value2"}]})))) + + + (defn add-variant-with-text [file variant-label component1-label root1-label component2-label root2-label child1-label child2-label text1 text2 & {:keys [text1-params text2-params]}] diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index bd06cbde6d..62d09bcec4 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -1122,4 +1122,143 @@ ;; No second line (t/is (= (get-in copy-structure-mixed-t' font-size-path-0) "50")) (t/is (= (get-in copy-structure-mixed-t' text-path-0) "bye")) - (t/is (nil? (get-in copy-structure-mixed-t' font-size-path-1))))) \ No newline at end of file + (t/is (nil? (get-in copy-structure-mixed-t' font-size-path-1))))) + + +(t/deftest test-switch-variant-for-other-with-same-nested-component + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (tho/add-simple-component :external01 :external01-root :external01-child) + (tho/add-simple-component :external02 :external02-root :external02-child) + (thv/add-variant-with-copy + :v01 :c01 :m01 :c02 :m02 :cp01 :cp02 :external01) + + (thc/instantiate-component :c01 + :copy01 + :children-labels [:copy-cp01])) + + page (thf/current-page file) + copy01 (ths/get-shape file :copy01) + copy-cp01 (ths/get-shape file :copy-cp01) + copy-cp01-rect-id (-> copy-cp01 :shapes first) + + + ;; On :copy-cp01, change the width of the rect + changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) + #{copy-cp01-rect-id} + (fn [shape] + (assoc shape :width 25)) + (:objects page) + {}) + file (thf/apply-changes file changes) + copy-cp01-rect (ths/get-shape-by-id file copy-cp01-rect-id) + + ;; ==== Action + ;; Switch :c01 for :c02 + file' (tho/swap-component file copy01 :c02 {:new-shape-label :copy02 :keep-touched? true}) + copy02 (ths/get-shape file' :copy02) + copy-cp02' (ths/get-shape-by-id file' (-> copy02 :shapes first)) + copy-cp02-rect' (ths/get-shape-by-id file' (-> copy-cp02' :shapes first))] + + ;; The width of copy-cp01-rect was 25 + (t/is (= (:width copy-cp01-rect) 25)) + + ;; The width of copy-cp02-rect' is 25 (change is preserved) + (t/is (= (:width copy-cp02-rect') 25)))) + + + +(t/deftest test-switch-variant-that-has-swaped-copy + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (tho/add-simple-component :external01 :external01-root :external01-child) + (tho/add-simple-component :external02 :external02-root :external02-child) + (thv/add-variant-with-copy + :v01 :c01 :m01 :c02 :m02 :cp01 :cp02 :external01) + + (thc/instantiate-component :c01 + :copy01 + :children-labels [:copy-cp01])) + + copy01 (ths/get-shape file :copy01) + copy-cp01 (ths/get-shape file :copy-cp01) + external02 (thc/get-component file :external02) + + ;; On :c01, swap the copy of :external01 for a copy of :external02 + file (-> file + (tho/swap-component copy-cp01 :external02 {:new-shape-label :copy-cp02 :keep-touched? false})) + copy-cp02 (ths/get-shape file :copy-cp02) + + ;; ==== Action + ;; Switch :c01 for :c02 + file' (tho/swap-component file copy01 :c02 {:new-shape-label :copy02 :keep-touched? true}) + + + copy02' (ths/get-shape file' :copy02) + copy-cp02' (ths/get-shape file' :copy-cp02)] + (thf/dump-file file') + ;;copy-cp02 is a copy of external02 + (t/is (= (:component-id copy-cp02) (:id external02))) + ;;copy-01 had copy-cp02 as child + (t/is (= (-> copy01 :shapes first) (:id copy-cp02))) + + ;;copy-cp02' is a copy of external02 + (t/is (= (:component-id copy-cp02') (:id external02))) + ;;copy-02' had copy-cp02' as child + (t/is (= (-> copy02' :shapes first) (:id copy-cp02'))))) + + +(t/deftest test-switch-variant-that-has-swaped-copy-with-changed-attr + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (tho/add-simple-component :external01 :external01-root :external01-child) + (tho/add-simple-component :external02 :external02-root :external02-child) + (thv/add-variant-with-copy + :v01 :c01 :m01 :c02 :m02 :cp01 :cp02 :external01) + + (thc/instantiate-component :c01 + :copy01 + :children-labels [:copy-cp01])) + + page (thf/current-page file) + copy01 (ths/get-shape file :copy01) + copy-cp01 (ths/get-shape file :copy-cp01) + external02 (thc/get-component file :external02) + + ;; On :c01, swap the copy of :external01 for a copy of :external02 + file (-> file + (tho/swap-component copy-cp01 :external02 {:new-shape-label :copy-cp02 :keep-touched? false})) + copy-cp02 (ths/get-shape file :copy-cp02) + copy-cp02-rect-id (-> copy-cp02 :shapes first) + + changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) + #{copy-cp02-rect-id} + (fn [shape] + (assoc shape :width 25)) + (:objects page) + {}) + file (thf/apply-changes file changes) + copy-cp02-rect (ths/get-shape-by-id file copy-cp02-rect-id) + + ;; ==== Action + ;; Switch :c01 for :c02 + file' (tho/swap-component file copy01 :c02 {:new-shape-label :copy02 :keep-touched? true}) + + + copy02' (ths/get-shape file' :copy02) + copy-cp02' (ths/get-shape file' :copy-cp02) + copy-cp02-rect' (ths/get-shape-by-id file' (-> copy-cp02' :shapes first))] + (thf/dump-file file') + ;;copy-cp02 is a copy of external02 + (t/is (= (:component-id copy-cp02) (:id external02))) + ;;copy-01 had copy-cp02 as child + (t/is (= (-> copy01 :shapes first) (:id copy-cp02))) + ;; The width of copy-cp02-rect was 25 + (t/is (= (:width copy-cp02-rect) 25)) + + ;;copy-cp02' is a copy of external02 + (t/is (= (:component-id copy-cp02') (:id external02))) + ;;copy-02' had copy-cp02' as child + (t/is (= (-> copy02' :shapes first) (:id copy-cp02'))) + ;; The width of copy-cp02-rect' is 25 (change is preserved) + (t/is (= (:width copy-cp02-rect') 25))))