mirror of
https://github.com/penpot/penpot.git
synced 2025-01-23 06:58:58 -05:00
✅ Add more tests for touched
This commit is contained in:
parent
a40afd5b63
commit
77d4901db1
3 changed files with 148 additions and 215 deletions
|
@ -6,8 +6,8 @@
|
||||||
|
|
||||||
(ns common-tests.helpers.compositions
|
(ns common-tests.helpers.compositions
|
||||||
(:require
|
(:require
|
||||||
[common-tests.helpers.files :as thf]
|
[app.common.data :as d]
|
||||||
[common-tests.helpers.ids-map :as thi]))
|
[common-tests.helpers.files :as thf]))
|
||||||
|
|
||||||
(defn add-rect
|
(defn add-rect
|
||||||
[file rect-label & {:keys [] :as params}]
|
[file rect-label & {:keys [] :as params}]
|
||||||
|
@ -51,3 +51,31 @@
|
||||||
:root-params main-root-params
|
:root-params main-root-params
|
||||||
:child-params main-child-params)
|
:child-params main-child-params)
|
||||||
(thf/instantiate-component component-label copy-root-label copy-root-params)))
|
(thf/instantiate-component component-label copy-root-label copy-root-params)))
|
||||||
|
|
||||||
|
(defn add-component-with-many-children
|
||||||
|
[file component-label root-label child-labels
|
||||||
|
& {:keys [component-params root-params child-params-list]}]
|
||||||
|
(as-> file $
|
||||||
|
(add-frame $ root-label root-params)
|
||||||
|
(reduce (fn [file [label params]]
|
||||||
|
(thf/add-sample-shape file
|
||||||
|
label
|
||||||
|
(merge {:type :rect
|
||||||
|
:name "Rect1"
|
||||||
|
:parent-label root-label}
|
||||||
|
params)))
|
||||||
|
$
|
||||||
|
(d/zip-all child-labels child-params-list))
|
||||||
|
(thf/make-component $ component-label root-label component-params)))
|
||||||
|
|
||||||
|
(defn add-component-with-many-children-and-copy
|
||||||
|
[file component-label root-label child-labels copy-root-label
|
||||||
|
& {:keys [component-params root-params child-params-list copy-root-params]}]
|
||||||
|
(-> file
|
||||||
|
(add-component-with-many-children component-label
|
||||||
|
root-label
|
||||||
|
child-labels
|
||||||
|
:component-params component-params
|
||||||
|
:root-params root-params
|
||||||
|
:child-params-list child-params-list)
|
||||||
|
(thf/instantiate-component component-label copy-root-label copy-root-params)))
|
||||||
|
|
|
@ -28,10 +28,12 @@
|
||||||
copy-root (thf/get-shape file :copy-root)
|
copy-root (thf/get-shape file :copy-root)
|
||||||
|
|
||||||
;; Action
|
;; Action
|
||||||
|
update-fn (fn [shape]
|
||||||
|
(assoc shape :fills (thf/sample-fills-color :fill-color "#fabada")))
|
||||||
|
|
||||||
changes (cflh/generate-update-shapes (pcb/empty-changes nil (:id page))
|
changes (cflh/generate-update-shapes (pcb/empty-changes nil (:id page))
|
||||||
(:shapes copy-root)
|
(:shapes copy-root)
|
||||||
#(assoc % :fills (thf/sample-fills-color
|
update-fn
|
||||||
:fill-color "#fabada"))
|
|
||||||
(:objects page)
|
(:objects page)
|
||||||
{})
|
{})
|
||||||
|
|
||||||
|
@ -49,3 +51,101 @@
|
||||||
(t/is (= (:fill-opacity fill') 1))
|
(t/is (= (:fill-opacity fill') 1))
|
||||||
(t/is (= (:touched copy-root') nil))
|
(t/is (= (:touched copy-root') nil))
|
||||||
(t/is (= (:touched copy-child') #{:fill-group}))))
|
(t/is (= (:touched copy-child') #{:fill-group}))))
|
||||||
|
|
||||||
|
(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)
|
||||||
|
(thf/add-sample-shape :free-shape))
|
||||||
|
|
||||||
|
page (thf/current-page file)
|
||||||
|
|
||||||
|
;; Action
|
||||||
|
;; IMPORTANT: as modifying copies structure is now forbidden, this action
|
||||||
|
;; will not have any effect, and so the parent shape won't also be touched.
|
||||||
|
changes (cflh/generate-relocate-shapes (pcb/empty-changes)
|
||||||
|
(:objects page)
|
||||||
|
#{(thi/id :copy-root)} ; parents
|
||||||
|
(thi/id :copy-root) ; parent-id
|
||||||
|
(:id page) ; page-id
|
||||||
|
0 ; to-index
|
||||||
|
#{(thi/id :free-shape)}) ; ids
|
||||||
|
|
||||||
|
file' (thf/apply-changes file changes)
|
||||||
|
|
||||||
|
;; Get
|
||||||
|
copy-root' (thf/get-shape file' :copy-root)
|
||||||
|
copy-child' (thf/get-shape-by-id file' (first (:shapes copy-root')))]
|
||||||
|
|
||||||
|
;; Check
|
||||||
|
(t/is (= (:touched copy-root') nil))
|
||||||
|
(t/is (= (:touched copy-child') nil))))
|
||||||
|
|
||||||
|
(t/deftest test-touched-when-deleting-shape
|
||||||
|
(let [;; Setup
|
||||||
|
file (-> (thf/sample-file :file1)
|
||||||
|
(tho/add-simple-component-with-copy :component1
|
||||||
|
:main-root
|
||||||
|
:main-child
|
||||||
|
:copy-root))
|
||||||
|
|
||||||
|
page (thf/current-page file)
|
||||||
|
copy-root (thf/get-shape file :copy-root)
|
||||||
|
|
||||||
|
;; Action
|
||||||
|
;; IMPORTANT: as modifying copies structure is now forbidden, this action will not
|
||||||
|
;; delete the child shape, but hide it (thus setting the visibility group).
|
||||||
|
[_all-parents changes]
|
||||||
|
(cflh/generate-delete-shapes (pcb/empty-changes)
|
||||||
|
file
|
||||||
|
page
|
||||||
|
(:objects page)
|
||||||
|
(set (:shapes copy-root))
|
||||||
|
{:components-v2 true})
|
||||||
|
|
||||||
|
file' (thf/apply-changes file changes)
|
||||||
|
|
||||||
|
;; Get
|
||||||
|
copy-root' (thf/get-shape file' :copy-root)
|
||||||
|
copy-child' (thf/get-shape-by-id file' (first (:shapes copy-root')))]
|
||||||
|
|
||||||
|
;; Check
|
||||||
|
(t/is (= (:touched copy-root') nil))
|
||||||
|
(t/is (= (:touched copy-child') #{:visibility-group}))))
|
||||||
|
|
||||||
|
(t/deftest test-not-touched-when-moving-shape
|
||||||
|
(let [;; Setup
|
||||||
|
file (-> (thf/sample-file :file1)
|
||||||
|
(tho/add-component-with-many-children-and-copy :component1
|
||||||
|
:main-root
|
||||||
|
[:main-child1 :main-child2 :main-child3]
|
||||||
|
:copy-root)
|
||||||
|
(thf/add-sample-shape :free-shape))
|
||||||
|
|
||||||
|
page (thf/current-page file)
|
||||||
|
copy-root (thf/get-shape file :copy-root)
|
||||||
|
copy-child1 (thf/get-shape-by-id file (first (:shapes copy-root)))
|
||||||
|
|
||||||
|
;; Action
|
||||||
|
;; IMPORTANT: as modifying copies structure is now forbidden, this action
|
||||||
|
;; will not have any effect, and so the parent shape won't also be touched.
|
||||||
|
changes (cflh/generate-relocate-shapes (pcb/empty-changes)
|
||||||
|
(:objects page)
|
||||||
|
#{(thi/id :copy-root)} ; parents
|
||||||
|
(thi/id :copy-root) ; parent-id
|
||||||
|
(:id page) ; page-id
|
||||||
|
2 ; to-index
|
||||||
|
#{(:id copy-child1)}) ; ids
|
||||||
|
|
||||||
|
file' (thf/apply-changes file changes)
|
||||||
|
|
||||||
|
;; Get
|
||||||
|
copy-root' (thf/get-shape file' :copy-root)
|
||||||
|
copy-child' (thf/get-shape-by-id file' (first (:shapes copy-root')))]
|
||||||
|
|
||||||
|
;; Check
|
||||||
|
(t/is (= (:touched copy-root') nil))
|
||||||
|
(t/is (= (:touched copy-child') nil))))
|
||||||
|
|
|
@ -23,201 +23,6 @@
|
||||||
|
|
||||||
;; === Test touched ======================
|
;; === Test touched ======================
|
||||||
|
|
||||||
(t/deftest test-touched-children-add
|
|
||||||
(t/async done
|
|
||||||
(let [state (-> thp/initial-state
|
|
||||||
(thp/sample-page)
|
|
||||||
(thp/sample-shape :shape1 :rect
|
|
||||||
{:name "Rect 1"
|
|
||||||
:fill-color clr/white
|
|
||||||
:fill-opacity 1})
|
|
||||||
(thp/make-component :main1 :component1
|
|
||||||
[(thp/id :shape1)])
|
|
||||||
(thp/instantiate-component :instance1
|
|
||||||
(thp/id :component1))
|
|
||||||
(thp/sample-shape :shape2 :circle
|
|
||||||
{:name "Circle 1"}))
|
|
||||||
|
|
||||||
instance1 (thp/get-shape state :instance1)
|
|
||||||
shape2 (thp/get-shape state :shape2)
|
|
||||||
|
|
||||||
store (the/prepare-store state done
|
|
||||||
(fn [new-state]
|
|
||||||
;; Expected shape tree:
|
|
||||||
;; [Page: Page 1]
|
|
||||||
;; Root Frame
|
|
||||||
;; {Rect 1}
|
|
||||||
;; Rect1
|
|
||||||
;; Rect 1 #--> Rect 1
|
|
||||||
;; Rect 1 ---> Rect 1
|
|
||||||
;; Circle 1
|
|
||||||
;;
|
|
||||||
;; [Component: Rect 1] core.cljs:200:23
|
|
||||||
;; --> [Page 1] Rect 1
|
|
||||||
|
|
||||||
(let [[[group shape1] [c-group c-shape1] _component]
|
|
||||||
(thl/resolve-instance-and-main-allow-dangling
|
|
||||||
new-state
|
|
||||||
(thp/id :instance1))]
|
|
||||||
|
|
||||||
(t/is (= (:name group) "Rect 1"))
|
|
||||||
(t/is (nil? (:touched group)))
|
|
||||||
(t/is (= (:name shape1) "Rect 1"))
|
|
||||||
(t/is (= (:touched shape1) nil))
|
|
||||||
(t/is (not= (:shape-ref shape1) nil))
|
|
||||||
|
|
||||||
(t/is (= (:name c-group) "Rect 1"))
|
|
||||||
(t/is (= (:touched c-group) nil))
|
|
||||||
(t/is (= (:shape-ref c-group) nil))
|
|
||||||
(t/is (= (:name c-shape1) "Rect 1"))
|
|
||||||
(t/is (= (:touched c-shape1) nil))
|
|
||||||
(t/is (= (:shape-ref c-shape1) nil)))))]
|
|
||||||
|
|
||||||
(ptk/emit!
|
|
||||||
store
|
|
||||||
(dw/relocate-shapes #{(:id shape2)} (:id instance1) 0) ;; We cant't change the structure of component copies, so this operation will do nothing
|
|
||||||
:the/end))))
|
|
||||||
|
|
||||||
(t/deftest test-touched-children-delete
|
|
||||||
(t/async done
|
|
||||||
(let [state (-> thp/initial-state
|
|
||||||
(thp/sample-page)
|
|
||||||
(thp/sample-shape :shape1 :rect
|
|
||||||
{:name "Rect 1"})
|
|
||||||
(thp/sample-shape :shape2 :rect
|
|
||||||
{:name "Rect 2"})
|
|
||||||
(thp/make-component :main1 :component1
|
|
||||||
[(thp/id :shape1)
|
|
||||||
(thp/id :shape2)])
|
|
||||||
(thp/instantiate-component :instance1
|
|
||||||
(thp/id :component1)))
|
|
||||||
|
|
||||||
[_group1 shape1']
|
|
||||||
(thl/resolve-instance state (thp/id :instance1))
|
|
||||||
|
|
||||||
store (the/prepare-store state done
|
|
||||||
(fn [new-state]
|
|
||||||
;; Expected shape tree:
|
|
||||||
;;;
|
|
||||||
;; [Page]
|
|
||||||
;; Root Frame
|
|
||||||
;; Component 1
|
|
||||||
;; Rect 1
|
|
||||||
;; Rect 2
|
|
||||||
;; Component 1 #--> Component 1
|
|
||||||
;; Rect 1* ---> Rect 1
|
|
||||||
;; #{:visibility-group}
|
|
||||||
;; Rect 2 ---> Rect 2
|
|
||||||
;;;
|
|
||||||
;; [Component 1]
|
|
||||||
;; page1 / Component 1
|
|
||||||
;;
|
|
||||||
(let [[[group shape1 shape2] [c-group c-shape1 c-shape2] _component]
|
|
||||||
(thl/resolve-instance-and-main-allow-dangling
|
|
||||||
new-state
|
|
||||||
(thp/id :instance1))]
|
|
||||||
|
|
||||||
(t/is (= (:name group) "Component 1"))
|
|
||||||
(t/is (= (:touched group) nil))
|
|
||||||
(t/is (not= (:shape-ref group) nil))
|
|
||||||
(t/is (= (:name shape1) "Rect 1"))
|
|
||||||
(t/is (= (:hidden shape1) true)) ; Instance shapes are not deleted but hidden
|
|
||||||
(t/is (= (:touched shape1) #{:visibility-group}))
|
|
||||||
(t/is (not= (:shape-ref shape1) nil))
|
|
||||||
(t/is (= (:name shape2) "Rect 2"))
|
|
||||||
(t/is (= (:touched shape2) nil))
|
|
||||||
(t/is (not= (:shape-ref shape2) nil))
|
|
||||||
|
|
||||||
(t/is (= (:name c-group) "Component 1"))
|
|
||||||
(t/is (= (:touched c-group) nil))
|
|
||||||
(t/is (= (:shape-ref c-group) nil))
|
|
||||||
(t/is (= (:name c-shape1) "Rect 1"))
|
|
||||||
(t/is (= (:touched c-shape1) nil))
|
|
||||||
(t/is (= (:shape-ref c-shape1) nil))
|
|
||||||
(t/is (= (:name c-shape2) "Rect 2"))
|
|
||||||
(t/is (= (:touched c-shape2) nil))
|
|
||||||
(t/is (= (:shape-ref c-shape2) nil)))))]
|
|
||||||
|
|
||||||
(ptk/emit!
|
|
||||||
store
|
|
||||||
(dwsh/delete-shapes #{(:id shape1')})
|
|
||||||
:the/end))))
|
|
||||||
|
|
||||||
(t/deftest test-touched-children-move
|
|
||||||
(t/async done
|
|
||||||
(let [state (-> thp/initial-state
|
|
||||||
(thp/sample-page)
|
|
||||||
(thp/sample-shape :shape1 :rect
|
|
||||||
{:name "Rect 1"})
|
|
||||||
(thp/sample-shape :shape2 :rect
|
|
||||||
{:name "Rect 2"})
|
|
||||||
(thp/sample-shape :shape3 :rect
|
|
||||||
{:name "Rect 3"})
|
|
||||||
(thp/make-component :main1 :component1
|
|
||||||
[(thp/id :shape1)
|
|
||||||
(thp/id :shape2)
|
|
||||||
(thp/id :shape3)])
|
|
||||||
(thp/instantiate-component :instance1
|
|
||||||
(thp/id :component1)))
|
|
||||||
|
|
||||||
[group1' shape1']
|
|
||||||
(thl/resolve-instance state (thp/id :instance1))
|
|
||||||
|
|
||||||
store (the/prepare-store state done
|
|
||||||
(fn [new-state]
|
|
||||||
;; Expected shape tree:
|
|
||||||
;; [Page: Page 1]
|
|
||||||
;; Root Frame
|
|
||||||
;; {Component 1} #
|
|
||||||
;; Rect 1
|
|
||||||
;; Rect 2
|
|
||||||
;; Rect 3
|
|
||||||
;; Component 1 #--> Component 1
|
|
||||||
;; Rect 1 ---> Rect 1
|
|
||||||
;; Rect 2 ---> Rect 2
|
|
||||||
;; Rect 3 ---> Rect 3
|
|
||||||
;;
|
|
||||||
;; ========= Local library
|
|
||||||
;;
|
|
||||||
;; [Component: Component 1]
|
|
||||||
;; --> [Page 1] Component 1
|
|
||||||
|
|
||||||
(let [[[group shape1 shape2 shape3]
|
|
||||||
[c-group c-shape1 c-shape2 c-shape3] _component]
|
|
||||||
(thl/resolve-instance-and-main-allow-dangling
|
|
||||||
new-state
|
|
||||||
(thp/id :instance1))]
|
|
||||||
|
|
||||||
(t/is (= (:name group) "Component 1"))
|
|
||||||
(t/is (nil? (:touched group)))
|
|
||||||
(t/is (= (:name shape1) "Rect 1"))
|
|
||||||
(t/is (= (:touched shape1) nil))
|
|
||||||
(t/is (not= (:shape-ref shape1) nil))
|
|
||||||
(t/is (= (:name shape2) "Rect 2"))
|
|
||||||
(t/is (= (:touched shape2) nil))
|
|
||||||
(t/is (not= (:shape-ref shape2) nil))
|
|
||||||
(t/is (= (:name shape3) "Rect 3"))
|
|
||||||
(t/is (= (:touched shape3) nil))
|
|
||||||
(t/is (not= (:shape-ref shape3) nil))
|
|
||||||
|
|
||||||
(t/is (= (:name c-group) "Component 1"))
|
|
||||||
(t/is (= (:touched c-group) nil))
|
|
||||||
(t/is (= (:shape-ref c-group) nil))
|
|
||||||
(t/is (= (:name c-shape1) "Rect 1"))
|
|
||||||
(t/is (= (:touched c-shape1) nil))
|
|
||||||
(t/is (= (:shape-ref c-shape1) nil))
|
|
||||||
(t/is (= (:name c-shape2) "Rect 2"))
|
|
||||||
(t/is (= (:touched c-shape2) nil))
|
|
||||||
(t/is (= (:shape-ref c-shape2) nil))
|
|
||||||
(t/is (= (:name c-shape3) "Rect 3"))
|
|
||||||
(t/is (= (:touched c-shape3) nil))
|
|
||||||
(t/is (= (:shape-ref c-shape3) nil)))))]
|
|
||||||
|
|
||||||
(ptk/emit!
|
|
||||||
store
|
|
||||||
(dw/relocate-shapes #{(:id shape1')} (:id group1') 2) ;; We cant't change the structure of component copies, so this operation will do nothing
|
|
||||||
:the/end))))
|
|
||||||
|
|
||||||
(t/deftest test-touched-from-lib
|
(t/deftest test-touched-from-lib
|
||||||
(t/async
|
(t/async
|
||||||
done
|
done
|
||||||
|
|
Loading…
Add table
Reference in a new issue