0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-22 14:39:45 -05:00

Add test for touched shapes

This commit is contained in:
Andrés Moya 2024-04-29 15:15:10 +02:00
parent 5611fcfc2c
commit a40afd5b63
8 changed files with 122 additions and 111 deletions

View file

@ -35,7 +35,7 @@
(log/set-level! :warn)
(defn generate-update-shapes
[changes ids update-fn objects {:keys [attrs ignore-tree ignore-touched with-objects?]}]
[changes ids update-fn objects {:keys [attrs ignore-tree ignore-touched with-objects?]}]
(let [changes (reduce
(fn [changes id]
(let [opts {:attrs attrs

View file

@ -10,35 +10,44 @@
[common-tests.helpers.ids-map :as thi]))
(defn add-rect
[file rect-label]
[file rect-label & {:keys [] :as params}]
(thf/add-sample-shape file rect-label
:type :rect
:name "Rect1"))
(merge {:type :rect
:name "Rect1"}
params)))
(defn add-frame
([file frame-label & {:keys [parent-label]}]
(thf/add-sample-shape file frame-label
:type :frame
:name "Frame1"
:parent-label parent-label)))
[file frame-label & {:keys [] :as params}]
(thf/add-sample-shape file frame-label
(merge {:type :frame
:name "Frame1"}
params)))
(defn add-frame-with-child
[file frame-label child-label]
[file frame-label child-label & {:keys [frame-params child-params]}]
(-> file
(add-frame frame-label)
(add-frame frame-label frame-params)
(thf/add-sample-shape child-label
:type :rect
:name "Rect1"
:parent-label frame-label)))
(merge {:type :rect
:name "Rect1"
:parent-label frame-label}
child-params))))
(defn add-simple-component
[file component-label root-label child-label]
[file component-label root-label child-label
& {:keys [component-params root-params child-params]}]
(-> file
(add-frame-with-child root-label child-label)
(thf/make-component component-label root-label)))
(add-frame-with-child root-label child-label :frame-params root-params :child-params child-params)
(thf/make-component component-label root-label component-params)))
(defn add-simple-component-with-copy
[file component-label main-root-label main-child-label copy-root-label]
[file component-label main-root-label main-child-label copy-root-label
& {:keys [component-params main-root-params main-child-params copy-root-params]}]
(-> file
(add-simple-component component-label main-root-label main-child-label)
(thf/instantiate-component component-label copy-root-label)))
(add-simple-component component-label
main-root-label
main-child-label
:component-params component-params
:root-params main-root-params
:child-params main-child-params)
(thf/instantiate-component component-label copy-root-label copy-root-params)))

View file

@ -6,6 +6,7 @@
(ns common-tests.helpers.files
(:require
[app.common.colors :as clr]
[app.common.data.macros :as dm]
[app.common.features :as ffeat]
[app.common.files.changes :as cfc]
@ -169,7 +170,7 @@
;; ----- Components
(defn make-component
[file label root-label]
[file label root-label & {:keys [] :as params}]
(let [page (current-page file)
root (get-shape file root-label)]
@ -194,12 +195,12 @@
#(update % :objects assoc (:id shape) shape)))
$
updated-shapes)
(ctkl/add-component $
{:id (:component-id updated-root)
:name (:name updated-root)
:main-instance-id (:id updated-root)
:main-instance-page (:id page)
:shapes updated-shapes})))))))
(ctkl/add-component $ (assoc params
:id (:component-id updated-root)
:name (:name updated-root)
:main-instance-id (:id updated-root)
:main-instance-page (:id page)
:shapes updated-shapes))))))))
(defn get-component
[file label]
@ -306,7 +307,21 @@
[label & {:keys [] :as params}]
(ctc/make-color (assoc params :id (thi/new-id! label))))
(defn add-sample-color
(defn sample-fill-color
[& {:keys [fill-color fill-opacity] :as params}]
(let [params (cond-> params
(nil? fill-color)
(assoc :fill-color clr/black)
(nil? fill-opacity)
(assoc :fill-opacity 1))]
params))
(defn sample-fills-color
[& {:keys [] :as params}]
[(sample-fill-color params)])
(defn add-sample-library-color
[file label & {:keys [] :as params}]
(let [color (sample-color label params)]
(ctf/update-file-data file #(ctcl/add-color % color))))

View file

@ -4,7 +4,7 @@
;;
;; Copyright (c) KALEIDOS INC
(ns common-tests.logic.logic-comp-creation-test
(ns common-tests.logic.component-creation-test
(:require
[app.common.files.changes-builder :as pcb]
[app.common.files.libraries-helpers :as cflh]
@ -15,14 +15,14 @@
(t/use-fixtures :each thi/test-fixture)
(t/deftest test-add-component-from-single-shape
(let [; Setup
(let [;; Setup
file (-> (thf/sample-file :file1)
(thf/add-sample-shape :shape1 :type :frame))
page (thf/current-page file)
shape1 (thf/get-shape file :shape1)
; Action
;; Action
[_ component-id changes]
(cflh/generate-add-component (pcb/empty-changes)
[shape1]
@ -35,11 +35,11 @@
file' (thf/apply-changes file changes)
; Get
;; Get
component (thf/get-component-by-id file' component-id)
root (thf/get-shape-by-id file' (:main-instance-id component))]
; Check
;; Check
(t/is (some? component))
(t/is (some? root))
(t/is (= (:component-id root) (:id component)))))

View file

@ -0,0 +1,51 @@
;; 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.components-touched-test
(:require
[app.common.files.changes-builder :as pcb]
[app.common.files.libraries-helpers :as cflh]
[clojure.test :as t]
[common-tests.helpers.compositions :as tho]
[common-tests.helpers.files :as thf]
[common-tests.helpers.ids-map :as thi]))
(t/use-fixtures :each thi/test-fixture)
(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)
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)
{})
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')))
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}))))

View file

@ -70,21 +70,21 @@
(t/is (= (:name f1) "Test file"))))
(t/deftest test-absorb-components
(let [; Setup
(let [;; Setup
library (-> (thf/sample-file :library :is-shared true)
(tho/add-simple-component :component1 :main-root :rect1))
file (-> (thf/sample-file :file)
(thf/instantiate-component :component1 :copy-root :library library))
; Action
;; Action
file' (ctf/update-file-data
file
#(ctf/absorb-assets % (:data library)))
_ (thf/validate-file! file')
; Get
;; Get
pages' (ctpl/pages-seq (ctf/file-data file'))
components' (ctkl/components-seq (ctf/file-data file'))
component' (first components')
@ -92,7 +92,7 @@
copy-root' (thf/get-shape file' :copy-root)
main-root' (ctf/get-ref-shape (ctf/file-data file') component' copy-root')]
; Check
;; Check
(t/is (= (count pages') 2))
(t/is (= (:name (first pages')) "Page 1"))
(t/is (= (:name (second pages')) "Main components"))
@ -104,10 +104,10 @@
(t/is (ctk/main-instance-of? (:id main-root') (:id (second pages')) component'))))
(t/deftest test-absorb-colors
(let [; Setup
(let [;; Setup
library (-> (thf/sample-file :library :is-shared true)
(thf/add-sample-color :color1 {:name "Test color"
:color "#abcdef"}))
(thf/add-sample-library-color :color1 {:name "Test color"
:color "#abcdef"}))
file (-> (thf/sample-file :file)
(thf/add-sample-shape :shape1
@ -118,19 +118,19 @@
:fill-color-ref-id (thi/id :color1)
:fill-color-ref-file (thi/id :library)}]))
; Action
;; Action
file' (ctf/update-file-data
file
#(ctf/absorb-assets % (:data library)))
_ (thf/validate-file! file')
; Get
;; Get
colors' (ctcl/colors-seq (ctf/file-data file'))
shape1' (thf/get-shape file' :shape1)
fill' (first (:fills shape1'))]
; Check
;; Check
(t/is (= (count colors') 1))
(t/is (= (:id (first colors')) (thi/id :color1)))
(t/is (= (:name (first colors')) "Test color"))
@ -141,7 +141,7 @@
(t/is (= (:fill-color-ref-file fill') (:id file')))))
(t/deftest test-absorb-typographies
(let [; Setup
(let [;; Setup
library (-> (thf/sample-file :library :is-shared true)
(thf/add-sample-typography :typography1 {:name "Test typography"}))
@ -169,18 +169,19 @@
:letter-spacing "0"
:fills [{:fill-color "#000000"
:fill-opacity 1}]}]}]}]}))
; Action
;; Action
file' (ctf/update-file-data
file
#(ctf/absorb-assets % (:data library)))
_ (thf/validate-file! file')
; Get
;; Get
typographies' (ctyl/typographies-seq (ctf/file-data file'))
shape1' (thf/get-shape file' :shape1)
text-node' (d/seek #(some? (:text %)) (txt/node-seq (:content shape1')))]
;; Check
(t/is (= (count typographies') 1))
(t/is (= (:id (first typographies')) (thi/id :typography1)))
(t/is (= (:name (first typographies')) "Test typography"))

View file

@ -16,7 +16,6 @@
[app.common.logging :as log]
[app.common.schema :as sm]
[app.common.types.shape-tree :as ctst]
[app.common.types.shape.layout :as ctl]
[app.common.uuid :as uuid]
[app.main.data.workspace.state-helpers :as wsh]
[app.main.data.workspace.undo :as dwu]

View file

@ -7,7 +7,6 @@
(ns frontend-tests.state-components-sync-test
(:require
[app.common.colors :as clr]
[app.common.types.file :as ctf]
[app.main.data.workspace :as dw]
[app.main.data.workspace.changes :as dch]
[app.main.data.workspace.libraries :as dwl]
@ -24,69 +23,6 @@
;; === Test touched ======================
(t/deftest test-touched
(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)))
[_group1 shape1']
(thl/resolve-instance state (thp/id :instance1))
store (the/prepare-store state done
(fn [new-state]
;; Uncomment to debug
;; (ctf/dump-tree (get new-state :workspace-data)
;; (get new-state :current-page-id)
;; (get new-state :workspace-libraries)
;; false true)
;; Expected shape tree:
;;;
;; [Page]
;; Root Frame
;; Rect 1
;; Rect 1
;; Rect 1 #--> Rect 1
;; Rect 1* ---> Rect 1
;; #{:fill-group}
;;;
;; [Rect 1]
;; page1 / Rect 1
;;;
(let [[[group shape1] [c-group c-shape1] _component]
(thl/resolve-instance-and-main
new-state
(thp/id :instance1))]
(t/is (= (:name group) "Rect 1"))
(t/is (= (:touched group) nil))
(t/is (= (:name shape1) "Rect 1"))
(t/is (= (:touched shape1) #{:fill-group}))
(t/is (= (:fill-color shape1) clr/test))
(t/is (= (:fill-opacity shape1) 0.5))
(t/is (= (:name c-group) "Rect 1"))
(t/is (= (:touched c-group) nil))
(t/is (= (:name c-shape1) "Rect 1"))
(t/is (= (:touched c-shape1) nil))
(t/is (= (:fill-color c-shape1) clr/white))
(t/is (= (:fill-opacity c-shape1) 1)))))]
(ptk/emit!
store
(dch/update-shapes [(:id shape1')]
(fn [shape]
(merge shape {:fill-color clr/test
:fill-opacity 0.5})))
:the/end))))
(t/deftest test-touched-children-add
(t/async done
(let [state (-> thp/initial-state