mirror of
https://github.com/penpot/penpot.git
synced 2025-08-03 09:58:21 +02:00
✅ Add tests for reset components
This commit is contained in:
parent
dd62653d4b
commit
caefaf6016
3 changed files with 387 additions and 599 deletions
343
common/test/common_tests/logic/comp_reset_test.cljc
Normal file
343
common/test/common_tests/logic/comp_reset_test.cljc
Normal file
|
@ -0,0 +1,343 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) KALEIDOS INC
|
||||
|
||||
(ns common-tests.logic.comp-reset-test
|
||||
(:require
|
||||
[app.common.files.changes-builder :as pcb]
|
||||
[app.common.logic.libraries :as cll]
|
||||
[app.common.logic.shapes :as cls]
|
||||
[clojure.test :as t]
|
||||
[common-tests.helpers.components :as thc]
|
||||
[common-tests.helpers.compositions :as tho]
|
||||
[common-tests.helpers.files :as thf]
|
||||
[common-tests.helpers.ids-map :as thi]
|
||||
[common-tests.helpers.shapes :as ths]))
|
||||
|
||||
(t/use-fixtures :each thi/test-fixture)
|
||||
|
||||
(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)
|
||||
|
||||
;; ==== 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)
|
||||
update-fn
|
||||
(:objects page)
|
||||
{})
|
||||
|
||||
file-mdf (thf/apply-changes file changes)
|
||||
page-mdf (thf/current-page file-mdf)
|
||||
|
||||
changes (cll/generate-reset-component (pcb/empty-changes)
|
||||
file-mdf
|
||||
{(:id file-mdf) file-mdf}
|
||||
page-mdf
|
||||
(:id copy-root)
|
||||
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')))
|
||||
fills' (:fills copy-child')
|
||||
fill' (first fills')]
|
||||
|
||||
;; ==== Check
|
||||
(t/is (= (count fills') 1))
|
||||
(t/is (= (:fill-color fill') "#abcdef"))
|
||||
(t/is (= (:fill-opacity fill') 1))
|
||||
(t/is (= (:touched copy-root') nil))
|
||||
(t/is (= (:touched copy-child') nil))))
|
||||
|
||||
(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")}))
|
||||
|
||||
file (-> (thf/sample-file :file)
|
||||
(thc/instantiate-component :component1 :copy-root :library library))
|
||||
|
||||
page (thf/current-page file)
|
||||
copy-root (ths/get-shape file :copy-root)
|
||||
|
||||
;; ==== 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)
|
||||
update-fn
|
||||
(:objects page)
|
||||
{})
|
||||
|
||||
file-mdf (thf/apply-changes file changes)
|
||||
page-mdf (thf/current-page file-mdf)
|
||||
|
||||
changes (cll/generate-reset-component (pcb/empty-changes)
|
||||
file-mdf
|
||||
{(:id file-mdf) file-mdf
|
||||
(:id library) library}
|
||||
page-mdf
|
||||
(:id copy-root)
|
||||
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')))
|
||||
fills' (:fills copy-child')
|
||||
fill' (first fills')]
|
||||
|
||||
;; ==== Check
|
||||
(t/is (= (count fills') 1))
|
||||
(t/is (= (:fill-color fill') "#abcdef"))
|
||||
(t/is (= (:fill-opacity fill') 1))
|
||||
(t/is (= (:touched copy-root') nil))
|
||||
(t/is (= (:touched copy-child') nil))))
|
||||
|
||||
(t/deftest test-reset-after-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))
|
||||
|
||||
page (thf/current-page file)
|
||||
copy-root (ths/get-shape file :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 (cls/generate-relocate-shapes (pcb/empty-changes)
|
||||
(:objects page)
|
||||
#{(:parent-id copy-root)} ; parents
|
||||
(thi/id :copy-root) ; parent-id
|
||||
(:id page) ; page-id
|
||||
0 ; to-index
|
||||
#{(thi/id :free-shape)}) ; ids
|
||||
|
||||
file-mdf (thf/apply-changes file changes)
|
||||
page-mdf (thf/current-page file-mdf)
|
||||
|
||||
changes (cll/generate-reset-component (pcb/empty-changes)
|
||||
file-mdf
|
||||
{(:id file-mdf) file-mdf}
|
||||
page-mdf
|
||||
(:id copy-root)
|
||||
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')))]
|
||||
|
||||
;; ==== Check
|
||||
(t/is (= (:touched copy-root') nil))
|
||||
(t/is (= (:touched copy-child') nil))))
|
||||
|
||||
(t/deftest test-reset-after-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 (ths/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]
|
||||
(cls/generate-delete-shapes (pcb/empty-changes)
|
||||
file
|
||||
page
|
||||
(:objects page)
|
||||
(set (:shapes copy-root))
|
||||
{:components-v2 true})
|
||||
|
||||
file-mdf (thf/apply-changes file changes)
|
||||
page-mdf (thf/current-page file-mdf)
|
||||
|
||||
changes (cll/generate-reset-component (pcb/empty-changes)
|
||||
file-mdf
|
||||
{(:id file-mdf) file-mdf}
|
||||
page-mdf
|
||||
(:id copy-root)
|
||||
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')))]
|
||||
|
||||
;; ==== Check
|
||||
(t/is (= (:touched copy-root') nil))
|
||||
(t/is (= (:touched copy-child') nil))))
|
||||
|
||||
(t/deftest test-reset-after-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)
|
||||
(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)))
|
||||
|
||||
;; ==== 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 (cls/generate-relocate-shapes (pcb/empty-changes)
|
||||
(:objects page)
|
||||
#{(:parent-id copy-child1)} ; parents
|
||||
(thi/id :copy-root) ; parent-id
|
||||
(:id page) ; page-id
|
||||
2 ; to-index
|
||||
#{(:id copy-child1)}) ; ids
|
||||
|
||||
file-mdf (thf/apply-changes file changes)
|
||||
page-mdf (thf/current-page file-mdf)
|
||||
|
||||
changes (cll/generate-reset-component (pcb/empty-changes)
|
||||
file-mdf
|
||||
{(:id file-mdf) file-mdf}
|
||||
page-mdf
|
||||
(:id copy-root)
|
||||
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')))]
|
||||
|
||||
;; ==== Check
|
||||
(t/is (= (:touched copy-root') nil))
|
||||
(t/is (= (:touched copy-child') nil))))
|
||||
|
||||
(t/deftest test-reset-after-changing-upper
|
||||
(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
|
||||
:main2-root-params {:fills (ths/sample-fills-color
|
||||
:fill-color "#abcdef")}))
|
||||
page (thf/current-page file)
|
||||
copy2-root (ths/get-shape file :copy2-root)
|
||||
|
||||
;; ==== 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))
|
||||
#{(:id copy2-root)}
|
||||
update-fn
|
||||
(:objects page)
|
||||
{})
|
||||
|
||||
file-mdf (thf/apply-changes file changes)
|
||||
page-mdf (thf/current-page file-mdf)
|
||||
|
||||
changes (cll/generate-reset-component (pcb/empty-changes)
|
||||
file-mdf
|
||||
{(:id file-mdf) file-mdf}
|
||||
page-mdf
|
||||
(:id copy2-root)
|
||||
true)
|
||||
|
||||
file' (thf/apply-changes file changes)
|
||||
|
||||
;; ==== Get
|
||||
copy2-root' (ths/get-shape file' :copy2-root)
|
||||
fills' (:fills copy2-root')
|
||||
fill' (first fills')]
|
||||
|
||||
;; ==== Check
|
||||
(t/is (= (count fills') 1))
|
||||
(t/is (= (:fill-color fill') "#abcdef"))
|
||||
(t/is (= (:fill-opacity fill') 1))
|
||||
(t/is (= (:touched copy2-root') nil))))
|
||||
|
||||
(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)
|
||||
|
||||
;; ==== 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)
|
||||
update-fn
|
||||
(:objects page)
|
||||
{})
|
||||
|
||||
file-mdf (thf/apply-changes file changes)
|
||||
page-mdf (thf/current-page file-mdf)
|
||||
|
||||
changes (cll/generate-reset-component (pcb/empty-changes)
|
||||
file-mdf
|
||||
{(:id file-mdf) file-mdf}
|
||||
page-mdf
|
||||
(:id copy2-root)
|
||||
true)
|
||||
|
||||
file' (thf/apply-changes file changes)
|
||||
|
||||
;; ==== Get
|
||||
copy2-root' (ths/get-shape file' :copy2-root)
|
||||
copy2-child' (ths/get-shape-by-id file' (first (:shapes copy2-root')))
|
||||
fills' (:fills copy2-child')
|
||||
fill' (first fills')]
|
||||
|
||||
;; ==== Check
|
||||
(t/is (= (count fills') 1))
|
||||
(t/is (= (:fill-color fill') "#FFFFFF"))
|
||||
(t/is (= (:fill-opacity fill') 1))
|
||||
(t/is (= (:touched copy2-root') nil))
|
||||
(t/is (= (:touched copy2-child') nil))))
|
|
@ -9,6 +9,7 @@
|
|||
[app.common.files.changes-builder :as pcb]
|
||||
[app.common.logic.shapes :as cls]
|
||||
[clojure.test :as t]
|
||||
[common-tests.helpers.components :as thc]
|
||||
[common-tests.helpers.compositions :as tho]
|
||||
[common-tests.helpers.files :as thf]
|
||||
[common-tests.helpers.ids-map :as thi]
|
||||
|
@ -53,6 +54,44 @@
|
|||
(t/is (= (:touched copy-root') nil))
|
||||
(t/is (= (:touched copy-child') #{:fill-group}))))
|
||||
|
||||
(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")}))
|
||||
|
||||
file (-> (thf/sample-file :file)
|
||||
(thc/instantiate-component :component1 :copy-root :library library))
|
||||
|
||||
page (thf/current-page file)
|
||||
copy-root (ths/get-shape file :copy-root)
|
||||
|
||||
;; ==== 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)
|
||||
update-fn
|
||||
(:objects page)
|
||||
{})
|
||||
|
||||
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')))
|
||||
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)
|
||||
|
@ -87,7 +126,7 @@
|
|||
(t/is (= (:touched copy-root') nil))
|
||||
(t/is (= (:touched copy-child') nil))))
|
||||
|
||||
(t/deftest test-touched-when-deleting-shape
|
||||
(t/deftest test-not-touched-when-deleting-shape
|
||||
(let [;; ==== Setup
|
||||
file (-> (thf/sample-file :file1)
|
||||
(tho/add-simple-component-with-copy :component1
|
||||
|
@ -165,8 +204,8 @@
|
|||
:main2-root
|
||||
:main2-nested-head
|
||||
:copy2-root
|
||||
:root2-params {:fills (ths/sample-fills-color
|
||||
:fill-color "#abcdef")}))
|
||||
:main2-root-params {:fills (ths/sample-fills-color
|
||||
:fill-color "#abcdef")}))
|
||||
page (thf/current-page file)
|
||||
copy2-root (ths/get-shape file :copy2-root)
|
||||
|
||||
|
@ -202,9 +241,7 @@
|
|||
:component2
|
||||
:main2-root
|
||||
:main2-nested-head
|
||||
:copy2-root
|
||||
:nested-head-params {:fills (ths/sample-fills-color
|
||||
:fill-color "#abcdef")}))
|
||||
:copy2-root))
|
||||
page (thf/current-page file)
|
||||
copy2-root (ths/get-shape file :copy2-root)
|
||||
|
||||
|
@ -231,4 +268,4 @@
|
|||
(t/is (= (:fill-color fill') "#fabada"))
|
||||
(t/is (= (:fill-opacity fill') 1))
|
||||
(t/is (= (:touched copy2-root') nil))
|
||||
(t/is (= (:touched copy2-child') #{:fill-group}))))
|
||||
(t/is (= (:touched copy2-child') #{:fill-group}))))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue