Skip to content

Commit

Permalink
✅ Add more tests for touched
Browse files Browse the repository at this point in the history
  • Loading branch information
hirunatan committed Apr 30, 2024
1 parent a40afd5 commit 77d4901
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 215 deletions.
32 changes: 30 additions & 2 deletions common/test/common_tests/helpers/compositions.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

(ns common-tests.helpers.compositions
(:require
[common-tests.helpers.files :as thf]
[common-tests.helpers.ids-map :as thi]))
[app.common.data :as d]
[common-tests.helpers.files :as thf]))

(defn add-rect
[file rect-label & {:keys [] :as params}]
Expand Down Expand Up @@ -51,3 +51,31 @@
:root-params main-root-params
:child-params main-child-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)))
136 changes: 118 additions & 18 deletions common/test/common_tests/logic/components_touched_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,135 @@

(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 (thf/sample-fills-color
:fill-color "#abcdef")}))
page (thf/current-page file)
file (-> (thf/sample-file :file1)
(tho/add-simple-component-with-copy :component1
:main-root
:main-child
:copy-root
:main-child-params {:fills (thf/sample-fills-color
:fill-color "#abcdef")}))
page (thf/current-page file)
copy-root (thf/get-shape file :copy-root)

;; Action
changes (cflh/generate-update-shapes (pcb/empty-changes nil (:id page))
(:shapes copy-root)
#(assoc % :fills (thf/sample-fills-color
:fill-color "#fabada"))
(:objects page)
{})
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))
(:shapes copy-root)
update-fn
(:objects page)
{})

file' (thf/apply-changes file changes)
file' (thf/apply-changes file changes)

;; Get
copy-root' (thf/get-shape file' :copy-root)
copy-root' (thf/get-shape file' :copy-root)
copy-child' (thf/get-shape-by-id file' (first (:shapes copy-root')))
fills' (:fills copy-child')
fill' (first fills')]
fills' (:fills copy-child')
fill' (first fills')]

;; Check
(t/is (= (count fills') 1))
(t/is (= (:fill-color fill') "#fabada"))
(t/is (= (:fill-opacity fill') 1))
(t/is (= (:touched copy-root') nil))
(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))))

0 comments on commit 77d4901

Please sign in to comment.