Skip to content

Commit

Permalink
Merge pull request #4535 from penpot/hiru-move-front-tests
Browse files Browse the repository at this point in the history
Move frontend tests to common
  • Loading branch information
pabloalba committed Apr 30, 2024
2 parents bca8180 + de6d8cc commit 5e39601
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 324 deletions.
17 changes: 17 additions & 0 deletions common/src/app/common/files/libraries_helpers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@
;; Change this to :info :debug or :trace to debug this module, or :warn to reset to default
(log/set-level! :warn)

(defn generate-update-shapes
[changes ids update-fn objects {:keys [attrs ignore-tree ignore-touched with-objects?]}]
(let [changes (reduce
(fn [changes id]
(let [opts {:attrs attrs
:ignore-geometry? (get ignore-tree id)
:ignore-touched ignore-touched
:with-objects? with-objects?}]
(pcb/update-shapes changes [id] update-fn (d/without-nils opts))))
(-> changes
(pcb/with-objects objects))
ids)
grid-ids (->> ids (filter (partial ctl/grid-layout? objects)))
changes (pcb/update-shapes changes grid-ids ctl/assign-cell-positions {:with-objects? true})
changes (pcb/reorder-grid-children changes ids)]
changes))

(declare generate-sync-container)
(declare generate-sync-shape)
(declare generate-sync-text-shape)
Expand Down
79 changes: 58 additions & 21 deletions common/test/common_tests/helpers/compositions.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,76 @@

(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]
[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)))

(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)))
31 changes: 23 additions & 8 deletions common/test/common_tests/helpers/files.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)]

Expand All @@ -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]
Expand Down Expand Up @@ -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))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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)))))
152 changes: 152 additions & 0 deletions common/test/common_tests/logic/components_touched_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
;; 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
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)

;; 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}))))

(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)
copy-root (thf/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 (cflh/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' (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)
#{(:parent-id copy-child1)} ; 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 5e39601

Please sign in to comment.