0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-15 01:01:30 -05:00

🎉 Components refactor: Add tests for remove swap slot on duplicate

This commit is contained in:
Pablo Alba 2024-05-08 23:05:33 +02:00 committed by Andrés Moya
parent bfe9caba15
commit 6f93db034f
2 changed files with 143 additions and 39 deletions

View file

@ -2120,26 +2120,26 @@
(reduce (fn [changes child] (reduce (fn [changes child]
(generate-duplicate-shape-change changes (generate-duplicate-shape-change changes
(:objects page') (:objects page')
page page
unames unames
update-unames! update-unames!
ids-map ids-map
child child
delta delta
level-delta level-delta
libraries libraries
library-data library-data
file-id file-id
(if frame? new-id frame-id) (if frame? new-id frame-id)
new-id new-id
duplicating-component? duplicating-component?
true true
(and remove-swap-slot? (and remove-swap-slot?
;; only remove swap slot of children when the current shape ;; only remove swap slot of children when the current shape
;; is not a subinstance head nor a instance root ;; is not a subinstance head nor a instance root
(not subinstance-head?) (not subinstance-head?)
(not instance-root?)))) (not instance-root?))))
changes changes
(map (d/getf objects) (:shapes obj))))))) (map (d/getf objects) (:shapes obj)))))))
@ -2162,17 +2162,17 @@
changes changes
(->> shapes (->> shapes
(reduce #(generate-duplicate-shape-change %1 (reduce #(generate-duplicate-shape-change %1
all-objects all-objects
page page
unames unames
update-unames! update-unames!
ids-map ids-map
%2 %2
delta delta
nil nil
libraries libraries
library-data library-data
file-id) file-id)
changes)) changes))
;; We need to check the changes to get the ids-map ;; We need to check the changes to get the ids-map

View file

@ -7,6 +7,8 @@
(ns common-tests.logic.comp-remove-swap-slots-test (ns common-tests.logic.comp-remove-swap-slots-test
(:require (:require
[app.common.files.changes-builder :as pcb] [app.common.files.changes-builder :as pcb]
[app.common.geom.point :as gpt]
[app.common.logic.libraries :as cll]
[app.common.logic.shapes :as cls] [app.common.logic.shapes :as cls]
[app.common.types.component :as ctk] [app.common.types.component :as ctk]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
@ -36,14 +38,14 @@
(-> (thf/sample-file :file1) (-> (thf/sample-file :file1)
(tho/add-frame :frame-red) (tho/add-frame :frame-red)
(thc/make-component :red :frame-red) (thc/make-component :red :frame-red)
(tho/add-frame :frame-blue) (tho/add-frame :frame-blue :name "frame-blue")
(thc/make-component :blue :frame-blue) (thc/make-component :blue :frame-blue)
(tho/add-frame :frame-green) (tho/add-frame :frame-green)
(thc/make-component :green :frame-green) (thc/make-component :green :frame-green)
(thc/instantiate-component :red :red-copy-green :parent-label :frame-green) (thc/instantiate-component :red :red-copy-green :parent-label :frame-green)
(tho/add-frame :frame-b1) (tho/add-frame :frame-b1)
(thc/make-component :b1 :frame-b1) (thc/make-component :b1 :frame-b1)
(tho/add-frame :frame-yellow :parent-label :frame-b1) (tho/add-frame :frame-yellow :parent-label :frame-b1 :name "frame-yellow")
(thc/instantiate-component :red :red-copy :parent-label :frame-b1) (thc/instantiate-component :red :red-copy :parent-label :frame-b1)
(thc/component-swap :red-copy :blue :blue1) (thc/component-swap :red-copy :blue :blue1)
(thc/instantiate-component :green :green-copy :parent-label :frame-b1 :children-labels [:red-copy-in-green-copy]) (thc/instantiate-component :green :green-copy :parent-label :frame-b1 :children-labels [:red-copy-in-green-copy])
@ -51,7 +53,7 @@
(tho/add-frame :frame-b2) (tho/add-frame :frame-b2)
(thc/make-component :b2 :frame-b2))) (thc/make-component :b2 :frame-b2)))
(t/deftest test-keep-swap-slot-relocating-blue1-to-root (t/deftest test-remove-swap-slot-relocating-blue1-to-root
(let [;; ==== Setup (let [;; ==== Setup
file (setup-file) file (setup-file)
@ -80,7 +82,7 @@
(t/is (some? blue1')) (t/is (some? blue1'))
(t/is (nil? (ctk/get-swap-slot blue1'))))) (t/is (nil? (ctk/get-swap-slot blue1')))))
(t/deftest test-keep-swap-slot-move-blue1-to-root (t/deftest test-remove-swap-slot-move-blue1-to-root
(let [;; ==== Setup (let [;; ==== Setup
file (setup-file) file (setup-file)
page (thf/current-page file) page (thf/current-page file)
@ -110,7 +112,7 @@
(t/is (nil? (ctk/get-swap-slot blue1'))))) (t/is (nil? (ctk/get-swap-slot blue1')))))
(t/deftest test-keep-swap-slot-relocating-blue1-to-b2 (t/deftest test-remove-swap-slot-relocating-blue1-to-b2
(let [;; ==== Setup (let [;; ==== Setup
file (setup-file) file (setup-file)
page (thf/current-page file) page (thf/current-page file)
@ -140,7 +142,7 @@
(t/is (some? blue1')) (t/is (some? blue1'))
(t/is (nil? (ctk/get-swap-slot blue1'))))) (t/is (nil? (ctk/get-swap-slot blue1')))))
(t/deftest test-keep-swap-slot-move-blue1-to-b2 (t/deftest test-remove-swap-slot-move-blue1-to-b2
(let [;; ==== Setup (let [;; ==== Setup
file (setup-file) file (setup-file)
page (thf/current-page file) page (thf/current-page file)
@ -171,7 +173,7 @@
(t/is (some? blue1')) (t/is (some? blue1'))
(t/is (nil? (ctk/get-swap-slot blue1'))))) (t/is (nil? (ctk/get-swap-slot blue1')))))
(t/deftest test-keep-swap-slot-relocating-yellow-to-root (t/deftest test-remove-swap-slot-relocating-yellow-to-root
(let [;; ==== Setup (let [;; ==== Setup
file (setup-file) file (setup-file)
page (thf/current-page file) page (thf/current-page file)
@ -214,7 +216,7 @@
(t/is (some? blue1'')) (t/is (some? blue1''))
(t/is (nil? (ctk/get-swap-slot blue1''))))) (t/is (nil? (ctk/get-swap-slot blue1'')))))
(t/deftest test-keep-swap-slot-move-yellow-to-root (t/deftest test-remove-swap-slot-move-yellow-to-root
(let [;; ==== Setup (let [;; ==== Setup
file (setup-file) file (setup-file)
page (thf/current-page file) page (thf/current-page file)
@ -258,7 +260,7 @@
(t/is (nil? (ctk/get-swap-slot blue1''))))) (t/is (nil? (ctk/get-swap-slot blue1'')))))
(t/deftest test-keep-swap-slot-relocating-yellow-to-b2 (t/deftest test-remove-swap-slot-relocating-yellow-to-b2
(let [;; ==== Setup (let [;; ==== Setup
file (setup-file) file (setup-file)
page (thf/current-page file) page (thf/current-page file)
@ -302,7 +304,7 @@
(t/is (some? blue1'')) (t/is (some? blue1''))
(t/is (nil? (ctk/get-swap-slot blue1''))))) (t/is (nil? (ctk/get-swap-slot blue1'')))))
(t/deftest test-keep-swap-slot-move-yellow-to-b2 (t/deftest test-remove-swap-slot-move-yellow-to-b2
(let [;; ==== Setup (let [;; ==== Setup
file (setup-file) file (setup-file)
page (thf/current-page file) page (thf/current-page file)
@ -346,3 +348,105 @@
;; blue1 has not swap-id after move ;; blue1 has not swap-id after move
(t/is (some? blue1'')) (t/is (some? blue1''))
(t/is (nil? (ctk/get-swap-slot blue1''))))) (t/is (nil? (ctk/get-swap-slot blue1'')))))
(defn- find-duplicated-shape
[original-shape page]
;; duplicated shape has the same name, the same parent, and doesn't have a label
(->> (vals (:objects page))
(filter #(and (= (:name %) (:name original-shape))
(= (:parent-id %) (:parent-id original-shape))
(nil? (thi/label (:id %)))))
first))
(t/deftest test-remove-swap-slot-duplicating-blue1
(let [;; ==== Setup
file (setup-file)
page (thf/current-page file)
blue1 (ths/get-shape file :blue1)
;; ==== Action
changes (-> (pcb/empty-changes nil)
(cll/generate-duplicate-changes (:objects page) ;; objects
page ;; page
#{(:id blue1)} ;; ids
(gpt/point 0 0) ;; delta
{(:id file) file} ;; libraries
(:data file) ;; library-data
(:id file)) ;; file-id
(cll/generate-duplicate-changes-update-indices (:objects page) ;; objects
#{(:id blue1)})) ;; ids
file' (thf/apply-changes file changes)
;; ==== Get
page' (thf/current-page file')
blue1' (ths/get-shape file' :blue1)
duplicated-blue1' (find-duplicated-shape blue1' page')]
;; ==== Check
;; blue1 has swap-id
(t/is (some? (ctk/get-swap-slot blue1')))
;; duplicated-blue1 has not swap-id
(t/is (some? duplicated-blue1'))
(t/is (nil? (ctk/get-swap-slot duplicated-blue1')))))
(t/deftest test-remove-swap-slot-duplicate-yellow
(let [;; ==== Setup
file (setup-file)
page (thf/current-page file)
blue1 (ths/get-shape file :blue1)
yellow (ths/get-shape file :frame-yellow)
;; ==== Action
;; Move blue1 into yellow
changes (cls/generate-move-shapes-to-frame (pcb/empty-changes nil)
#{(:id blue1)} ;; ids
(:id yellow) ;; frame-id
(:id page) ;; page-id
(:objects page) ;; objects
0 ;; drop-index
nil) ;; cell
file' (thf/apply-changes file changes)
page' (thf/current-page file')
yellow' (ths/get-shape file' :frame-yellow)
;; Duplicate yellow
changes' (-> (pcb/empty-changes nil)
(cll/generate-duplicate-changes (:objects page') ;; objects
page' ;; page
#{(:id yellow')} ;; ids
(gpt/point 0 0) ;; delta
{(:id file') file'} ;; libraries
(:data file') ;; library-data
(:id file')) ;; file-id
(cll/generate-duplicate-changes-update-indices (:objects page') ;; objects
#{(:id yellow')})) ;; ids
file'' (thf/apply-changes file' changes')
;; ==== Get
page'' (thf/current-page file'')
blue1'' (ths/get-shape file'' :blue1)
yellow'' (ths/get-shape file'' :frame-yellow)
duplicated-yellow'' (find-duplicated-shape yellow'' page'')
duplicated-blue1-id'' (-> duplicated-yellow''
:shapes
first)
duplicated-blue1'' (get (:objects page'') duplicated-blue1-id'')]
;; ==== Check
;; blue1'' has swap-id
(t/is (some? (ctk/get-swap-slot blue1'')))
;; duplicated-blue1'' has not swap-id
(t/is (some? duplicated-blue1''))
(t/is (nil? (ctk/get-swap-slot duplicated-blue1'')))))