diff --git a/common/test/common_tests/helpers/components.cljc b/common/test/common_tests/helpers/components.cljc new file mode 100644 index 000000000..56ebf488d --- /dev/null +++ b/common/test/common_tests/helpers/components.cljc @@ -0,0 +1,153 @@ +;; 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.helpers.components + (:require + [app.common.data.macros :as dm] + [app.common.files.changes-builder :as pcb] + [app.common.files.helpers :as cfh] + [app.common.geom.point :as gpt] + [app.common.logic.libraries :as cll] + [app.common.types.component :as ctk] + [app.common.types.components-list :as ctkl] + [app.common.types.container :as ctn] + [app.common.types.file :as ctf] + [app.common.types.pages-list :as ctpl] + [app.common.types.shape-tree :as ctst] + [common-tests.helpers.files :as thf] + [common-tests.helpers.ids-map :as thi] + [common-tests.helpers.shapes :as ths])) + +(defn make-component + [file label root-label & {:keys [] :as params}] + (let [page (thf/current-page file) + root (ths/get-shape file root-label)] + + (dm/assert! + "Need that root is already a frame" + (cfh/frame-shape? root)) + + (let [[_new-root _new-shapes updated-shapes] + (ctn/convert-shape-in-component root (:objects page) (:id file)) + + updated-root (first updated-shapes)] ; Can't use new-root because it has a new id + + (thi/set-id! label (:component-id updated-root)) + + (ctf/update-file-data + file + (fn [file-data] + (as-> file-data $ + (reduce (fn [file-data shape] + (ctpl/update-page file-data + (:id page) + #(update % :objects assoc (:id shape) shape))) + $ + 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] + (ctkl/get-component (:data file) (thi/id label))) + +(defn get-component-by-id + [file id] + (ctkl/get-component (:data file) id)) + +(defn set-child-label + [file shape-label child-idx label] + (let [id (-> (ths/get-shape file shape-label) + :shapes + (nth child-idx))] + (when id + (thi/set-id! label id)))) + +(defn instantiate-component + [file component-label copy-root-label & {:keys [parent-label library children-labels] :as params}] + (let [page (thf/current-page file) + library (or library file) + component (get-component library component-label) + parent-id (when parent-label + (thi/id parent-label)) + parent (when parent-id + (ctst/get-shape page parent-id)) + frame-id (if (cfh/frame-shape? parent) + (:id parent) + (:frame-id parent)) + + [copy-root copy-shapes] + (ctn/make-component-instance page + component + (:data library) + (gpt/point 100 100) + true + {:force-id (thi/new-id! copy-root-label) + :force-frame-id frame-id}) + + copy-root' (cond-> copy-root + (some? parent) + (assoc :parent-id parent-id) + + (some? frame-id) + (assoc :frame-id frame-id) + + (and (some? parent) (ctn/in-any-component? (:objects page) parent)) + (dissoc :component-root)) + file' (ctf/update-file-data + file + (fn [file-data] + (as-> file-data $ + (ctpl/update-page $ + (:id page) + #(ctst/add-shape (:id copy-root') + copy-root' + % + frame-id + parent-id + nil + true)) + (reduce (fn [file-data shape] + (ctpl/update-page file-data + (:id page) + #(ctst/add-shape (:id shape) + shape + % + (:parent-id shape) + (:frame-id shape) + nil + true))) + $ + (remove #(= (:id %) (:did copy-root')) copy-shapes)))))] + (when children-labels + (dotimes [idx (count children-labels)] + (set-child-label file' copy-root-label idx (nth children-labels idx)))) + file')) + +(defn component-swap + [file shape-label new-component-label new-shape-label & {:keys [library] :as params}] + (let [shape (ths/get-shape file shape-label) + library (or library file) + libraries {(:id library) library} + page (thf/current-page file) + objects (:objects page) + id-new-component (-> (get-component library new-component-label) + :id) + + ;; Store the properties that need to be maintained when the component is swapped + keep-props-values (select-keys shape ctk/swap-keep-attrs) + + + [new_shape _ changes] + (-> (pcb/empty-changes nil (:id page)) + (cll/generate-component-swap objects shape (:data file) page libraries id-new-component 0 nil keep-props-values))] + + (thi/set-id! new-shape-label (:id new_shape)) + (thf/apply-changes file changes))) diff --git a/common/test/common_tests/helpers/compositions.cljc b/common/test/common_tests/helpers/compositions.cljc index d8b9cbe8b..cf1a02d9a 100644 --- a/common/test/common_tests/helpers/compositions.cljc +++ b/common/test/common_tests/helpers/compositions.cljc @@ -7,27 +7,35 @@ (ns common-tests.helpers.compositions (:require [app.common.data :as d] - [common-tests.helpers.files :as thf])) + [common-tests.helpers.components :as thc] + [common-tests.helpers.shapes :as ths])) (defn add-rect [file rect-label & {:keys [] :as params}] - (thf/add-sample-shape file rect-label + ;; Generated shape tree: + ;; :rect-label [:type :rect :name: Rect1] + (ths/add-sample-shape file rect-label (merge {:type :rect :name "Rect1"} params))) (defn add-frame [file frame-label & {:keys [] :as params}] - (thf/add-sample-shape file frame-label + ;; Generated shape tree: + ;; :frame-label [:type :frame :name: Frame1] + (ths/add-sample-shape file frame-label (merge {:type :frame :name "Frame1"} params))) (defn add-frame-with-child [file frame-label child-label & {:keys [frame-params child-params]}] + ;; Generated shape tree: + ;; :frame-label [:name: Frame1] + ;; :child-label [:name: Rect1] (-> file (add-frame frame-label frame-params) - (thf/add-sample-shape child-label + (ths/add-sample-shape child-label (merge {:type :rect :name "Rect1" :parent-label frame-label} @@ -36,13 +44,22 @@ (defn add-simple-component [file component-label root-label child-label & {:keys [component-params root-params child-params]}] + ;; Generated shape tree: + ;; {:root-label} [:name: Frame1] # [Component :component-label] + ;; :child-label [:name: Rect1] (-> file (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))) + (thc/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 & {:keys [component-params main-root-params main-child-params copy-root-params]}] + ;; Generated shape tree: + ;; {:main-root-label} [:name: Frame1] # [Component :component-label] + ;; :main-child-label [:name: Rect1] + ;; + ;; :copy-root-label [:name: Frame1] #--> [Component :component-label] :main-root-label + ;; [:name: Rect1] ---> :main-child-label (-> file (add-simple-component component-label main-root-label @@ -50,32 +67,103 @@ :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))) + (thc/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]}] + ;; Generated shape tree: + ;; {:root-label} [:name: Frame1] # [Component :component-label] + ;; :child1-label [:name: Rect1] + ;; :child2-label [:name: Rect2] + ;; :child3-label [:name: Rect3] (as-> file $ (add-frame $ root-label root-params) - (reduce (fn [file [label params]] - (thf/add-sample-shape file + (reduce (fn [file [index [label params]]] + (ths/add-sample-shape file label (merge {:type :rect - :name "Rect1" + :name (str "Rect" (inc index)) :parent-label root-label} params))) $ - (d/zip-all child-labels child-params-list)) - (thf/make-component $ component-label root-label component-params))) + (d/enumerate (d/zip-all child-labels child-params-list))) + (thc/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 component-label main-root-label main-child-labels copy-root-label + & {:keys [component-params main-root-params main-child-params-list copy-root-params]}] + ;; Generated shape tree: + ;; {:root-label} [:name: Frame1] # [Component :component-label] + ;; :child1-label [:name: Rect1] + ;; :child2-label [:name: Rect2] + ;; :child3-label [:name: Rect3] + ;; + ;; :copy-root-label [:name: Frame1] #--> [Component :component-label] :root-label + ;; [:name: Rect1] ---> :child1-label + ;; [:name: Rect2] ---> :child2-label + ;; [:name: Rect3] ---> :child3-label (-> file (add-component-with-many-children component-label - root-label - child-labels + main-root-label + main-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))) + :root-params main-root-params + :child-params-list main-child-params-list) + (thc/instantiate-component component-label copy-root-label copy-root-params))) + +(defn add-nested-component + [file component1-label main1-root-label main1-child-label component2-label main2-root-label nested-head-label + & {:keys [component1-params root1-params main1-child-params component2-params main2-root-params nested-head-params]}] + ;; Generated shape tree: + ;; {:main1-root-label} [:name: Frame1] # [Component :component1-label] + ;; :main1-child-label [:name: Rect1] + ;; + ;; {:main2-root-label} [:name: Frame2] # [Component :component2-label] + ;; :nested-head-label [:name: Frame1] @--> [Component :component1-label] :main1-root-label + ;; [:name: Rect1] ---> :main1-child-label + (-> file + (add-simple-component component1-label + main1-root-label + main1-child-label + :component-params component1-params + :root-params root1-params + :child-params main1-child-params) + (add-frame main2-root-label (merge {:name "Frame2"} + main2-root-params)) + (thc/instantiate-component component1-label + nested-head-label + (assoc nested-head-params + :parent-label main2-root-label)) + (thc/make-component component2-label + main2-root-label + component2-params))) + +(defn add-nested-component-with-copy + [file component1-label main1-root-label main1-child-label component2-label main2-root-label nested-head-label copy2-label + & {:keys [component1-params root1-params main1-child-params component2-params main2-root-params nested-head-params copy2-params]}] + ;; Generated shape tree: + ;; {:main1-root-label} [:name: Frame1] # [Component :component1-label] + ;; :main1-child-label [:name: Rect1] + ;; + ;; {:main2-root-label} [:name: Frame2] # [Component :component2-label] + ;; :nested-head-label [:name: Frame1] @--> [Component :component1-label] :main1-root-label + ;; [:name: Rect1] ---> :main1-child-label + ;; + ;; :copy2-label [:name: Frame2] #--> [Component :component2-label] :main2-root-label + ;; [:name: Frame1] @--> [Component :component1-label] :nested-head-label + ;; [:name: Rect1] ---> + (-> file + (add-nested-component component1-label + main1-root-label + main1-child-label + component2-label + main2-root-label + nested-head-label + :component1-params component1-params + :root1-params root1-params + :main1-child-params main1-child-params + :component2-params component2-params + :main2-root-params main2-root-params + :nested-head-params nested-head-params) + (thc/instantiate-component component2-label copy2-label copy2-params))) \ No newline at end of file diff --git a/common/test/common_tests/helpers/debug.cljc b/common/test/common_tests/helpers/debug.cljc deleted file mode 100644 index 984e47159..000000000 --- a/common/test/common_tests/helpers/debug.cljc +++ /dev/null @@ -1,33 +0,0 @@ -(ns common-tests.helpers.debug - (:require - [app.common.uuid :as uuid] - [common-tests.helpers.ids-map :as thi])) - -(defn dump-shape - "Dumps a shape, with each attribute in a line" - [shape] - (println "{") - (doseq [[k v] (sort shape)] - (when (some? v) - (println (str " " k " : " v)))) - (println "}")) - -(defn- stringify-keys [m keys] - (apply str (interpose ", " (map #(str % ": " (get m %)) keys)))) - -(defn dump-page - "Dumps the layer tree of the page. Prints the label of each shape, and the specified keys. - Example: (thd/dump-page (thf/current-page file) [:id :touched])" - ([page keys] - (dump-page page uuid/zero "" keys)) - ([page id padding keys] - (let [objects (vals (:objects page)) - root-objects (filter #(and - (= (:parent-id %) id) - (not= (:id %) id)) - objects)] - (doseq [val root-objects] - (println padding (thi/label (:id val)) - (when keys - (str "[" (stringify-keys val keys) "]"))) - (dump-page page (:id val) (str padding " ") keys))))) diff --git a/common/test/common_tests/helpers/files.cljc b/common/test/common_tests/helpers/files.cljc index c7de66880..2d893104f 100644 --- a/common/test/common_tests/helpers/files.cljc +++ b/common/test/common_tests/helpers/files.cljc @@ -6,29 +6,17 @@ (ns common-tests.helpers.files (:require - [app.common.colors :as clr] - [app.common.data.macros :as dm] + [app.common.data :as d] [app.common.features :as ffeat] [app.common.files.changes :as cfc] - [app.common.files.changes-builder :as pcb] - [app.common.files.helpers :as cfh] [app.common.files.validate :as cfv] - [app.common.geom.point :as gpt] - [app.common.logic.libraries :as cll] [app.common.pprint :refer [pprint]] - [app.common.types.color :as ctc] - [app.common.types.colors-list :as ctcl] - [app.common.types.component :as ctk] - [app.common.types.components-list :as ctkl] - [app.common.types.container :as ctn] [app.common.types.file :as ctf] [app.common.types.page :as ctp] [app.common.types.pages-list :as ctpl] - [app.common.types.shape :as cts] - [app.common.types.shape-tree :as ctst] - [app.common.types.typographies-list :as cttl] - [app.common.types.typography :as ctt] - [common-tests.helpers.ids-map :as thi])) + [app.common.uuid :as uuid] + [common-tests.helpers.ids-map :as thi] + [cuerdas.core :as str])) ;; ----- Files @@ -68,25 +56,6 @@ (validate-file! file') file')) -(declare current-page-id) -(declare get-page) - -(defn dump-file - [file & {:keys [page-label libraries] :as params}] - (let [params (-> params - (or {:show-ids true :show-touched true}) - (dissoc page-label libraries)) - page (if (some? page-label) - (:id (get-page file page-label)) - (current-page-id file)) - libraries (or libraries {})] - - (ctf/dump-tree file page libraries params))) - -(defn pprint-file - [file & {:keys [level length] :or {level 10 length 1000}}] - (pprint file {:level level :length length})) - ;; ----- Pages (defn sample-page @@ -116,221 +85,74 @@ [file label] (vary-meta file assoc :current-page-id (thi/id label))) -;; ----- Shapes +;; ----- Debug -(defn sample-shape - [label & {:keys [type] :as params}] - (let [params (cond-> params - label - (assoc :id (thi/new-id! label)) +(defn dump-file-type + "Dump a file using dump-tree function in common.types.file." + [file & {:keys [page-label libraries] :as params}] + (let [params (-> params + (or {:show-ids true :show-touched true}) + (dissoc page-label libraries)) + page (if (some? page-label) + (:id (get-page file page-label)) + (current-page-id file)) + libraries (or libraries {})] - (nil? type) - (assoc :type :rect))] + (ctf/dump-tree file page libraries params))) - (cts/setup-shape params))) +(defn pprint-file + "Pretry print a file trying to limit the quantity of info shown." + [file & {:keys [level length] :or {level 10 length 1000}}] + (pprint file {:level level :length length})) -(defn add-sample-shape - [file label & {:keys [parent-label] :as params}] - (let [page (current-page file) - shape (sample-shape label (dissoc params :parent-label)) - parent-id (when parent-label - (thi/id parent-label)) - parent (when parent-id - (ctst/get-shape page parent-id)) - frame-id (if (cfh/frame-shape? parent) - (:id parent) - (:frame-id parent))] - (ctf/update-file-data - file - (fn [file-data] - (ctpl/update-page file-data - (:id page) - #(ctst/add-shape (:id shape) - shape - % - frame-id - parent-id - nil - true)))))) +(defn dump-shape + "Dump a shape, with each attribute in a line." + [shape] + (println "{") + (doseq [[k v] (sort shape)] + (when (some? v) + (println (str " " k " : " v)))) + (println "}")) -(defn get-shape - [file label & {:keys [page-label]}] - (let [page (if page-label - (get-page file page-label) - (current-page file))] - (ctst/get-shape page (thi/id label)))) +(defn- stringify-keys [m keys] + (apply str (interpose ", " (map #(str % ": " (get m %)) keys)))) -(defn get-shape-by-id - [file id & {:keys [page-label]}] - (let [page (if page-label - (get-page file page-label) - (current-page file))] - (ctst/get-shape page id))) +(defn- dump-page-shape + [shape keys padding] + (println (str/pad (str padding + (when (:main-instance shape) "{") + (or (thi/label (:id shape)) "") + (when (:main-instance shape) "}") + (when keys + (str " [" (stringify-keys shape keys) "]"))) + {:length 40 :type :right}) + (if (nil? (:shape-ref shape)) + (if (:component-root shape) + (str "# [Component " (or (thi/label (:component-id shape)) "") "]") + "") + (str/format "%s--> %s%s" + (cond (:component-root shape) "#" + (:component-id shape) "@" + :else "-") + (if (:component-root shape) + (str "[Component " (or (thi/label (:component-id shape)) "") "] ") + "") + (or (thi/label (:shape-ref shape)) ""))))) -;; ----- Components +(defn dump-page + "Dump the layer tree of the page. Print the label of each shape, and the specified keys." + ([page keys] + (dump-page page uuid/zero "" keys)) + ([page root-id padding keys] + (let [lookupf (d/getf (:objects page)) + root-shape (lookupf root-id) + shapes (map lookupf (:shapes root-shape))] + (doseq [shape shapes] + (dump-page-shape shape keys padding) + (dump-page page (:id shape) (str padding " ") keys))))) -(defn make-component - [file label root-label & {:keys [] :as params}] - (let [page (current-page file) - root (get-shape file root-label)] - - (dm/assert! - "Need that root is already a frame" - (cfh/frame-shape? root)) - - (let [[_new-root _new-shapes updated-shapes] - (ctn/convert-shape-in-component root (:objects page) (:id file)) - - updated-root (first updated-shapes)] ; Can't use new-root because it has a new id - - (thi/set-id! label (:component-id updated-root)) - - (ctf/update-file-data - file - (fn [file-data] - (as-> file-data $ - (reduce (fn [file-data shape] - (ctpl/update-page file-data - (:id page) - #(update % :objects assoc (:id shape) shape))) - $ - 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] - (ctkl/get-component (:data file) (thi/id label))) - -(defn get-component-by-id - [file id] - (ctkl/get-component (:data file) id)) - -(defn set-child-label - [file shape-label child-idx label] - (let [id (-> (get-shape file shape-label) - :shapes - (nth child-idx))] - (when id - (thi/set-id! label id)))) - -(defn instantiate-component - [file component-label copy-root-label & {:keys [parent-label library children-labels] :as params}] - (let [page (current-page file) - library (or library file) - component (get-component library component-label) - parent-id (when parent-label - (thi/id parent-label)) - parent (when parent-id - (ctst/get-shape page parent-id)) - frame-id (if (cfh/frame-shape? parent) - (:id parent) - (:frame-id parent)) - - [copy-root copy-shapes] - (ctn/make-component-instance page - component - (:data library) - (gpt/point 100 100) - true - {:force-id (thi/new-id! copy-root-label) - :force-frame-id frame-id}) - - copy-root' (cond-> copy-root - (some? parent) - (assoc :parent-id parent-id) - - (some? frame-id) - (assoc :frame-id frame-id) - - (and (some? parent) (ctn/in-any-component? (:objects page) parent)) - (dissoc :component-root)) - file' (ctf/update-file-data - file - (fn [file-data] - (as-> file-data $ - (ctpl/update-page $ - (:id page) - #(ctst/add-shape (:id copy-root') - copy-root' - % - frame-id - parent-id - nil - true)) - (reduce (fn [file-data shape] - (ctpl/update-page file-data - (:id page) - #(ctst/add-shape (:id shape) - shape - % - (:parent-id shape) - (:frame-id shape) - nil - true))) - $ - (remove #(= (:id %) (:did copy-root')) copy-shapes)))))] - (when children-labels - (dotimes [idx (count children-labels)] - (set-child-label file' copy-root-label idx (nth children-labels idx)))) - file')) - - - -(defn component-swap - [file shape-label new-component-label new-shape-label & {:keys [library] :as params}] - (let [shape (get-shape file shape-label) - library (or library file) - libraries {(:id library) library} - page (current-page file) - objects (:objects page) - id-new-component (-> (get-component library new-component-label) - :id) - - ;; Store the properties that need to be maintained when the component is swapped - keep-props-values (select-keys shape ctk/swap-keep-attrs) - - - [new_shape _ changes] - (-> (pcb/empty-changes nil (:id page)) - (cll/generate-component-swap objects shape (:data file) page libraries id-new-component 0 nil keep-props-values))] - - (thi/set-id! new-shape-label (:id new_shape)) - (apply-changes file changes))) - - -(defn sample-color - [label & {:keys [] :as params}] - (ctc/make-color (assoc params :id (thi/new-id! label)))) - -(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)))) - -(defn sample-typography - [label & {:keys [] :as params}] - (ctt/make-typography (assoc params :id (thi/new-id! label)))) - -(defn add-sample-typography - [file label & {:keys [] :as params}] - (let [typography (sample-typography label params)] - (ctf/update-file-data file #(cttl/add-typography % typography)))) +(defn dump-file + "Dump the current page of the file, using dump-page above. + Example: (thf/dump-file file [:id :touched])" + ([file] (dump-file file [])) + ([file keys] (dump-page (current-page file) keys))) diff --git a/common/test/common_tests/helpers/shapes.cljc b/common/test/common_tests/helpers/shapes.cljc new file mode 100644 index 000000000..53521cbe5 --- /dev/null +++ b/common/test/common_tests/helpers/shapes.cljc @@ -0,0 +1,101 @@ +;; 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.helpers.shapes + (:require + [app.common.colors :as clr] + [app.common.files.helpers :as cfh] + [app.common.types.color :as ctc] + [app.common.types.colors-list :as ctcl] + [app.common.types.file :as ctf] + [app.common.types.pages-list :as ctpl] + [app.common.types.shape :as cts] + [app.common.types.shape-tree :as ctst] + [app.common.types.typographies-list :as cttl] + [app.common.types.typography :as ctt] + [common-tests.helpers.files :as thf] + [common-tests.helpers.ids-map :as thi])) + +(defn sample-shape + [label & {:keys [type] :as params}] + (let [params (cond-> params + label + (assoc :id (thi/new-id! label)) + + (nil? type) + (assoc :type :rect))] + + (cts/setup-shape params))) + +(defn add-sample-shape + [file label & {:keys [parent-label] :as params}] + (let [page (thf/current-page file) + shape (sample-shape label (dissoc params :parent-label)) + parent-id (when parent-label + (thi/id parent-label)) + parent (when parent-id + (ctst/get-shape page parent-id)) + frame-id (if (cfh/frame-shape? parent) + (:id parent) + (:frame-id parent))] + (ctf/update-file-data + file + (fn [file-data] + (ctpl/update-page file-data + (:id page) + #(ctst/add-shape (:id shape) + shape + % + frame-id + parent-id + nil + true)))))) + +(defn get-shape + [file label & {:keys [page-label]}] + (let [page (if page-label + (thf/get-page file page-label) + (thf/current-page file))] + (ctst/get-shape page (thi/id label)))) + +(defn get-shape-by-id + [file id & {:keys [page-label]}] + (let [page (if page-label + (thf/get-page file page-label) + (thf/current-page file))] + (ctst/get-shape page id))) + +(defn sample-color + [label & {:keys [] :as params}] + (ctc/make-color (assoc params :id (thi/new-id! label)))) + +(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)))) + +(defn sample-typography + [label & {:keys [] :as params}] + (ctt/make-typography (assoc params :id (thi/new-id! label)))) + +(defn add-sample-typography + [file label & {:keys [] :as params}] + (let [typography (sample-typography label params)] + (ctf/update-file-data file #(cttl/add-typography % typography)))) diff --git a/common/test/common_tests/logic/comp_remove_swap_slots_test.cljc b/common/test/common_tests/logic/comp_remove_swap_slots_test.cljc index 2fd559e0c..331ee0e8a 100644 --- a/common/test/common_tests/logic/comp_remove_swap_slots_test.cljc +++ b/common/test/common_tests/logic/comp_remove_swap_slots_test.cljc @@ -11,9 +11,11 @@ [app.common.types.component :as ctk] [app.common.uuid :as uuid] [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.ids-map :as thi] + [common-tests.helpers.shapes :as ths])) (t/use-fixtures :each thi/test-fixture) @@ -34,29 +36,30 @@ (-> (thf/sample-file :file1) (tho/add-frame :frame-red) - (thf/make-component :red :frame-red) + (thc/make-component :red :frame-red) (tho/add-frame :frame-blue) - (thf/make-component :blue :frame-blue) + (thc/make-component :blue :frame-blue) (tho/add-frame :frame-green) - (thf/make-component :green :frame-green) - (thf/instantiate-component :red :red-copy-green :parent-label :frame-green) + (thc/make-component :green :frame-green) + (thc/instantiate-component :red :red-copy-green :parent-label :frame-green) (tho/add-frame :frame-b1) - (thf/make-component :b1 :frame-b1) + (thc/make-component :b1 :frame-b1) (tho/add-frame :frame-yellow :parent-label :frame-b1) - (thf/instantiate-component :red :red-copy :parent-label :frame-b1) - (thf/component-swap :red-copy :blue :blue1) - (thf/instantiate-component :green :green-copy :parent-label :frame-b1 :children-labels [:red-copy-in-green-copy]) - (thf/component-swap :red-copy-in-green-copy :blue :blue-copy-in-green-copy) + (thc/instantiate-component :red :red-copy :parent-label :frame-b1) + (thc/component-swap :red-copy :blue :blue1) + (thc/instantiate-component :green :green-copy :parent-label :frame-b1 :children-labels [:red-copy-in-green-copy]) + (thc/component-swap :red-copy-in-green-copy :blue :blue-copy-in-green-copy) (tho/add-frame :frame-b2) - (thf/make-component :b2 :frame-b2))) + (thc/make-component :b2 :frame-b2))) (t/deftest test-keep-swap-slot-relocating-blue1-to-root - (let [;; ============================== Setup =============================== + (let [;; ==== Setup file (setup-file) - page (thf/current-page file) - blue1 (thf/get-shape file :blue1) - ;; ============================== Action ============================== + page (thf/current-page file) + blue1 (ths/get-shape file :blue1) + + ;; ==== Action changes (cls/generate-relocate-shapes (pcb/empty-changes nil) (:objects page) #{(:parent-id blue1)} ;; parents @@ -66,10 +69,11 @@ #{(:id blue1)}) ;; ids file' (thf/apply-changes file changes) - ;; ============================== Get ================================= - blue1' (thf/get-shape file' :blue1)] + ;; ==== Get + blue1' (ths/get-shape file' :blue1)] + + ;; ==== Check - ;; ================================== Check =============================== ;; blue1 had swap-id before move (t/is (some? (ctk/get-swap-slot blue1))) @@ -78,12 +82,12 @@ (t/is (nil? (ctk/get-swap-slot blue1'))))) (t/deftest test-keep-swap-slot-move-blue1-to-root - (let [;; ============================== Setup =============================== + (let [;; ==== Setup file (setup-file) page (thf/current-page file) - blue1 (thf/get-shape file :blue1) + blue1 (ths/get-shape file :blue1) - ;; ============================== Action ============================== + ;; ==== Action changes (cls/generate-move-shapes-to-frame (pcb/empty-changes nil) #{(:id blue1)} ;; ids uuid/zero ;; frame-id @@ -94,10 +98,11 @@ file' (thf/apply-changes file changes) - ;; ============================== Get ================================= - blue1' (thf/get-shape file' :blue1)] + ;; ==== Get + blue1' (ths/get-shape file' :blue1)] + + ;; ==== Check - ;; ================================== Check =============================== ;; blue1 had swap-id before move (t/is (some? (ctk/get-swap-slot blue1))) @@ -107,14 +112,14 @@ (t/deftest test-keep-swap-slot-relocating-blue1-to-b2 - (let [;; ============================== Setup =============================== + (let [;; ==== Setup file (setup-file) page (thf/current-page file) - blue1 (thf/get-shape file :blue1) - b2 (thf/get-shape file :frame-b2) + blue1 (ths/get-shape file :blue1) + b2 (ths/get-shape file :frame-b2) - ;; ============================== Action ============================== + ;; ==== Action changes (cls/generate-relocate-shapes (pcb/empty-changes nil) (:objects page) #{(:parent-id blue1)} ;; parents @@ -124,10 +129,11 @@ #{(:id blue1)}) ;; ids file' (thf/apply-changes file changes) - ;; ============================== Get ================================= - blue1' (thf/get-shape file' :blue1)] + ;; ==== Get + blue1' (ths/get-shape file' :blue1)] + + ;; ==== Check - ;; ================================== Check =============================== ;; blue1 had swap-id before move (t/is (some? (ctk/get-swap-slot blue1))) @@ -136,14 +142,14 @@ (t/is (nil? (ctk/get-swap-slot blue1'))))) (t/deftest test-keep-swap-slot-move-blue1-to-b2 - (let [;; ============================== Setup =============================== + (let [;; ==== Setup file (setup-file) page (thf/current-page file) - blue1 (thf/get-shape file :blue1) - b2 (thf/get-shape file :frame-b2) + blue1 (ths/get-shape file :blue1) + b2 (ths/get-shape file :frame-b2) - ;; ============================== Action ============================== + ;; ==== Action changes (cls/generate-move-shapes-to-frame (pcb/empty-changes nil) #{(:id blue1)} ;; ids (:id b2) ;; frame-id @@ -154,10 +160,11 @@ file' (thf/apply-changes file changes) - ;; ============================== Get ================================= - blue1' (thf/get-shape file' :blue1)] + ;; ==== Get + blue1' (ths/get-shape file' :blue1)] + + ;; ==== Check - ;; ================================== Check =============================== ;; blue1 had swap-id before move (t/is (some? (ctk/get-swap-slot blue1))) @@ -166,13 +173,13 @@ (t/is (nil? (ctk/get-swap-slot blue1'))))) (t/deftest test-keep-swap-slot-relocating-yellow-to-root - (let [;; ============================== Setup =============================== + (let [;; ==== Setup file (setup-file) page (thf/current-page file) - blue1 (thf/get-shape file :blue1) - yellow (thf/get-shape file :frame-yellow) + blue1 (ths/get-shape file :blue1) + yellow (ths/get-shape file :frame-yellow) - ;; ============================== Action ============================== + ;; ==== Action ;; Move blue1 into yellow changes (cls/generate-relocate-shapes (pcb/empty-changes nil) (:objects page) @@ -184,7 +191,7 @@ file' (thf/apply-changes file changes) page' (thf/current-page file') - yellow' (thf/get-shape file' :frame-yellow) + yellow' (ths/get-shape file' :frame-yellow) ;; Move yellow into root changes' (cls/generate-relocate-shapes (pcb/empty-changes nil) @@ -196,10 +203,11 @@ #{(:id yellow')}) ;; ids file'' (thf/apply-changes file' changes') - ;; ============================== Get ================================= - blue1'' (thf/get-shape file'' :blue1)] + ;; ==== Get + blue1'' (ths/get-shape file'' :blue1)] + + ;; ==== Check - ;; ================================== Check =============================== ;; blue1 had swap-id before move (t/is (some? (ctk/get-swap-slot blue1))) @@ -208,13 +216,13 @@ (t/is (nil? (ctk/get-swap-slot blue1''))))) (t/deftest test-keep-swap-slot-move-yellow-to-root - (let [;; ============================== Setup =============================== + (let [;; ==== Setup file (setup-file) page (thf/current-page file) - blue1 (thf/get-shape file :blue1) - yellow (thf/get-shape file :frame-yellow) + blue1 (ths/get-shape file :blue1) + yellow (ths/get-shape file :frame-yellow) - ;; ============================== Action ============================== + ;; ==== Action ;; Move blue1 into yellow changes (cls/generate-move-shapes-to-frame (pcb/empty-changes nil) #{(:id blue1)} ;; ids @@ -226,7 +234,7 @@ file' (thf/apply-changes file changes) page' (thf/current-page file') - yellow' (thf/get-shape file' :frame-yellow) + yellow' (ths/get-shape file' :frame-yellow) ;; Move yellow into root changes' (cls/generate-move-shapes-to-frame (pcb/empty-changes nil) @@ -238,10 +246,11 @@ nil) ;; cell file'' (thf/apply-changes file' changes') - ;; ============================== Get ================================= - blue1'' (thf/get-shape file'' :blue1)] + ;; ==== Get + blue1'' (ths/get-shape file'' :blue1)] + + ;; ==== Check - ;; ================================== Check =============================== ;; blue1 had swap-id before move (t/is (some? (ctk/get-swap-slot blue1))) @@ -251,13 +260,13 @@ (t/deftest test-keep-swap-slot-relocating-yellow-to-b2 - (let [;; ============================== Setup =============================== + (let [;; ==== Setup file (setup-file) page (thf/current-page file) - blue1 (thf/get-shape file :blue1) - yellow (thf/get-shape file :frame-yellow) + blue1 (ths/get-shape file :blue1) + yellow (ths/get-shape file :frame-yellow) - ;; ============================== Action ============================== + ;; ==== Action ;; Move blue1 into yellow changes (cls/generate-relocate-shapes (pcb/empty-changes nil) (:objects page) @@ -269,8 +278,8 @@ file' (thf/apply-changes file changes) page' (thf/current-page file') - yellow' (thf/get-shape file' :frame-yellow) - b2' (thf/get-shape file' :frame-b2) + yellow' (ths/get-shape file' :frame-yellow) + b2' (ths/get-shape file' :frame-b2) ;; Move yellow into b2 changes' (cls/generate-relocate-shapes (pcb/empty-changes nil) @@ -282,10 +291,11 @@ #{(:id yellow')}) ;; ids file'' (thf/apply-changes file' changes') - ;; ============================== Get ================================= - blue1'' (thf/get-shape file'' :blue1)] + ;; ==== Get + blue1'' (ths/get-shape file'' :blue1)] + + ;; ==== Check - ;; ================================== Check =============================== ;; blue1 had swap-id before move (t/is (some? (ctk/get-swap-slot blue1))) @@ -294,13 +304,13 @@ (t/is (nil? (ctk/get-swap-slot blue1''))))) (t/deftest test-keep-swap-slot-move-yellow-to-b2 - (let [;; ============================== Setup =============================== + (let [;; ==== Setup file (setup-file) page (thf/current-page file) - blue1 (thf/get-shape file :blue1) - yellow (thf/get-shape file :frame-yellow) + blue1 (ths/get-shape file :blue1) + yellow (ths/get-shape file :frame-yellow) - ;; ============================== Action ============================== + ;; ==== Action ;; Move blue1 into yellow changes (cls/generate-move-shapes-to-frame (pcb/empty-changes nil) #{(:id blue1)} ;; ids @@ -312,8 +322,8 @@ file' (thf/apply-changes file changes) page' (thf/current-page file') - yellow' (thf/get-shape file' :frame-yellow) - b2' (thf/get-shape file' :frame-b2) + yellow' (ths/get-shape file' :frame-yellow) + b2' (ths/get-shape file' :frame-b2) ;; Move yellow into b2 changes' (cls/generate-move-shapes-to-frame (pcb/empty-changes nil) @@ -326,10 +336,11 @@ file'' (thf/apply-changes file' changes') - ;; ============================== Get ================================= - blue1'' (thf/get-shape file'' :blue1)] + ;; ==== Get + blue1'' (ths/get-shape file'' :blue1)] + + ;; ==== Check - ;; ================================== Check =============================== ;; blue1 had swap-id before move (t/is (some? (ctk/get-swap-slot blue1))) diff --git a/common/test/common_tests/logic/component_creation_test.cljc b/common/test/common_tests/logic/component_creation_test.cljc index 59ec29806..13a7533b8 100644 --- a/common/test/common_tests/logic/component_creation_test.cljc +++ b/common/test/common_tests/logic/component_creation_test.cljc @@ -9,20 +9,22 @@ [app.common.files.changes-builder :as pcb] [app.common.logic.libraries :as cll] [clojure.test :as t] + [common-tests.helpers.components :as thc] [common-tests.helpers.files :as thf] - [common-tests.helpers.ids-map :as thi])) + [common-tests.helpers.ids-map :as thi] + [common-tests.helpers.shapes :as ths])) (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)) + (ths/add-sample-shape :shape1 :type :frame)) page (thf/current-page file) - shape1 (thf/get-shape file :shape1) + shape1 (ths/get-shape file :shape1) - ;; Action + ;; ==== Action [_ component-id changes] (cll/generate-add-component (pcb/empty-changes) [shape1] @@ -35,11 +37,11 @@ file' (thf/apply-changes file changes) - ;; Get - component (thf/get-component-by-id file' component-id) - root (thf/get-shape-by-id file' (:main-instance-id component))] + ;; ==== Get + component (thc/get-component-by-id file' component-id) + root (ths/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))))) diff --git a/common/test/common_tests/logic/components_touched_test.cljc b/common/test/common_tests/logic/components_touched_test.cljc index 0aa14ca29..c7bc5bd62 100644 --- a/common/test/common_tests/logic/components_touched_test.cljc +++ b/common/test/common_tests/logic/components_touched_test.cljc @@ -7,30 +7,30 @@ (ns common-tests.logic.components-touched-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.compositions :as tho] [common-tests.helpers.files :as thf] - [common-tests.helpers.ids-map :as thi])) + [common-tests.helpers.ids-map :as thi] + [common-tests.helpers.shapes :as ths])) (t/use-fixtures :each thi/test-fixture) (t/deftest test-touched-when-changing-attribute - (let [;; Setup + (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 + :main-child-params {:fills (ths/sample-fills-color :fill-color "#abcdef")})) page (thf/current-page file) - copy-root (thf/get-shape file :copy-root) + copy-root (ths/get-shape file :copy-root) - ;; Action + ;; ==== Action update-fn (fn [shape] - (assoc shape :fills (thf/sample-fills-color :fill-color "#fabada"))) + (assoc shape :fills (ths/sample-fills-color :fill-color "#fabada"))) changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) (:shapes copy-root) @@ -40,13 +40,13 @@ 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'))) + ;; ==== 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 + ;; ==== Check (t/is (= (count fills') 1)) (t/is (= (:fill-color fill') "#fabada")) (t/is (= (:fill-opacity fill') 1)) @@ -54,18 +54,19 @@ (t/is (= (:touched copy-child') #{:fill-group})))) (t/deftest test-not-touched-when-adding-shape - (let [;; Setup + (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)) + (ths/add-sample-shape :free-shape)) page (thf/current-page file) - copy-root (thf/get-shape file :copy-root) + copy-root (ths/get-shape file :copy-root) + + ;; ==== Action - ;; 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) @@ -78,16 +79,16 @@ 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')))] + ;; ==== Get + copy-root' (ths/get-shape file' :copy-root) + copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root')))] - ;; Check + ;; ==== Check (t/is (= (:touched copy-root') nil)) (t/is (= (:touched copy-child') nil)))) (t/deftest test-touched-when-deleting-shape - (let [;; Setup + (let [;; ==== Setup file (-> (thf/sample-file :file1) (tho/add-simple-component-with-copy :component1 :main-root @@ -95,9 +96,10 @@ :copy-root)) page (thf/current-page file) - copy-root (thf/get-shape file :copy-root) + copy-root (ths/get-shape file :copy-root) + + ;; ==== Action - ;; 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] @@ -110,28 +112,29 @@ 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')))] + ;; ==== Get + copy-root' (ths/get-shape file' :copy-root) + copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root')))] - ;; Check + ;; ==== Check (t/is (= (:touched copy-root') nil)) (t/is (= (:touched copy-child') #{:visibility-group})))) (t/deftest test-not-touched-when-moving-shape - (let [;; Setup + (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)) + (ths/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))) + copy-root (ths/get-shape file :copy-root) + copy-child1 (ths/get-shape-by-id file (first (:shapes copy-root))) + + ;; ==== Action - ;; 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) @@ -144,10 +147,88 @@ 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')))] + ;; ==== Get + copy-root' (ths/get-shape file' :copy-root) + copy-child' (ths/get-shape-by-id file' (first (:shapes copy-root')))] - ;; Check + ;; ==== Check (t/is (= (:touched copy-root') nil)) (t/is (= (:touched copy-child') nil)))) + +(t/deftest test-touched-when-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 + :root2-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' (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') "#fabada")) + (t/is (= (:fill-opacity fill') 1)) + (t/is (= (:touched copy2-root') #{:fill-group})))) + +(t/deftest test-touched-when-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 + :nested-head-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)) + (:shapes copy2-root) + update-fn + (:objects page) + {}) + + 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') "#fabada")) + (t/is (= (:fill-opacity fill') 1)) + (t/is (= (:touched copy2-root') nil)) + (t/is (= (:touched copy2-child') #{:fill-group})))) \ No newline at end of file diff --git a/common/test/common_tests/logic/swap_and_reset_test.cljc b/common/test/common_tests/logic/swap_and_reset_test.cljc index 535315297..40d3c7ef8 100644 --- a/common/test/common_tests/logic/swap_and_reset_test.cljc +++ b/common/test/common_tests/logic/swap_and_reset_test.cljc @@ -11,27 +11,32 @@ [app.common.types.component :as ctk] [app.common.types.file :as ctf] [clojure.test :as t] - [common-tests.helpers.compositions :as thc] + [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.ids-map :as thi] + [common-tests.helpers.shapes :as ths])) (t/use-fixtures :each thi/test-fixture) ;; Related .penpot file: common/test/cases/swap-and-reset.penpot (t/deftest test-simple-swap - (let [;; Setup - file - (-> (thf/sample-file :file1) - (thc/add-simple-component-with-copy :component-1 :component-1-main-root :component-1-main-child :component-1-copy-root) - (thc/add-simple-component :component-2 :component-2-root :component-2-child)) + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (tho/add-simple-component-with-copy :component-1 + :component-1-main-root + :component-1-main-child + :component-1-copy-root) + (tho/add-simple-component :component-2 + :component-2-root + :component-2-child)) - component-1-copy-root (thf/get-shape file :component-1-copy-root) - component-1 (thf/get-component file :component-1) - component-2 (thf/get-component file :component-2) + component-1-copy-root (ths/get-shape file :component-1-copy-root) + component-2 (thc/get-component file :component-2) page (thf/current-page file) - ;; Action - [new-shape all-parents changes] + ;; ==== Action + [new-shape _all-parents changes] (cll/generate-component-swap (pcb/empty-changes) (:objects page) component-1-copy-root @@ -45,41 +50,39 @@ file' (thf/apply-changes file changes) - ;; Get - swapped (thf/get-shape-by-id file' (:id new-shape))] + ;; ==== Get + swapped (ths/get-shape-by-id file' (:id new-shape))] - ;; Check + ;; ==== Check (t/is (not= (:component-id component-1-copy-root) (:component-id swapped))) (t/is (= (:id component-2) (:component-id swapped))) (t/is (= (:id file) (:component-file swapped))))) (t/deftest test-swap-nested - (let [;; Setup + (let [;; ==== Setup file (-> (thf/sample-file :file1) - (thc/add-simple-component :component-1 :component-1-main-root :component-1-main-child) - (thc/add-frame :component-container) - (thf/instantiate-component :component-1 :component-1-copy-root :parent-label :component-container) - (thf/make-component :component-container-main :component-container) - (thf/instantiate-component :component-container-main :component-container-instance) - (thc/add-simple-component :component-2 :component-2-main-root :component-2-main-child)) + (tho/add-simple-component :component-1 :component-1-main-root :component-1-main-child) + (tho/add-frame :component-container) + (thc/instantiate-component :component-1 :component-1-copy-root :parent-label :component-container) + (thc/make-component :component-container-main :component-container) + (thc/instantiate-component :component-container-main :component-container-instance) + (tho/add-simple-component :component-2 :component-2-main-root :component-2-main-child)) page (thf/current-page file) - component-1 (thf/get-component file :component-1) - component-2 (thf/get-component file :component-2) - component-3 (thf/get-component file :component-3) + component-2 (thc/get-component file :component-2) copy (->> - (thf/get-shape file :component-container-instance) + (ths/get-shape file :component-container-instance) :shapes first - (thf/get-shape-by-id file)) + (ths/get-shape-by-id file)) libraries {(:id file) file} - ;; Action - [new-shape all-parents changes] + ;; ==== Action + [new-shape _all-parents changes] (cll/generate-component-swap (pcb/empty-changes) (:objects page) copy @@ -95,16 +98,16 @@ libraries' {(:id file') file'} page' (thf/current-page file') - ;; Get - swapped (thf/get-shape-by-id file' (:id new-shape)) - component-1-copy-root (thf/get-shape file' :component-1-copy-root) + ;; ==== Get + swapped (ths/get-shape-by-id file' (:id new-shape)) + component-1-copy-root (ths/get-shape file' :component-1-copy-root) slot (-> (ctf/find-swap-slot swapped page' file' libraries') (ctk/build-swap-slot-group))] - ;; Check + ;; ==== Check (t/is (not= (:component-id copy) (:component-id swapped))) (t/is (= (:id component-2) (:component-id swapped))) (t/is (= (:id file) (:component-file swapped))) @@ -112,29 +115,29 @@ (t/is (= (ctk/get-swap-slot swapped) (:id component-1-copy-root))))) (t/deftest test-swap-and-reset-override - (let [;; Setup + (let [;; ==== Setup file (-> (thf/sample-file :file1) - (thc/add-simple-component :component-1 :component-1-main-root :component-1-main-child) - (thc/add-frame :component-container) - (thf/instantiate-component :component-1 :component-1-copy-root :parent-label :component-container) - (thf/make-component :component-container-main :component-container) - (thf/instantiate-component :component-container-main :component-container-instance) - (thc/add-simple-component :component-2 :component-2-main-root :component-2-main-child)) + (tho/add-simple-component :component-1 :component-1-main-root :component-1-main-child) + (tho/add-frame :component-container) + (thc/instantiate-component :component-1 :component-1-copy-root :parent-label :component-container) + (thc/make-component :component-container-main :component-container) + (thc/instantiate-component :component-container-main :component-container-instance) + (tho/add-simple-component :component-2 :component-2-main-root :component-2-main-child)) page (thf/current-page file) - component-1 (thf/get-component file :component-1) - component-2 (thf/get-component file :component-2) + component-1 (thc/get-component file :component-1) + component-2 (thc/get-component file :component-2) copy (->> - (thf/get-shape file :component-container-instance) + (ths/get-shape file :component-container-instance) :shapes first - (thf/get-shape-by-id file)) + (ths/get-shape-by-id file)) - ;; Action - [new-shape all-parents changes-swap] + ;; ==== Action + [new-shape _all-parents changes-swap] (cll/generate-component-swap (pcb/empty-changes) (:objects page) copy @@ -158,17 +161,15 @@ true) file' (thf/apply-changes file changes) - page' (thf/current-page file') - ;; Get + + ;; ==== Get reset (->> - (thf/get-shape file' :component-container-instance) + (ths/get-shape file' :component-container-instance) :shapes first - (thf/get-shape-by-id file')) + (ths/get-shape-by-id file'))] - component-1-copy-root (thf/get-shape file' :component-1-copy-root)] - - ;; Check + ;; ==== Check (t/is (= (:id component-1) (:component-id reset))) (t/is (nil? (ctk/get-swap-slot reset))))) diff --git a/common/test/common_tests/types/types_libraries_test.cljc b/common/test/common_tests/types/types_libraries_test.cljc index d95232824..744a39518 100644 --- a/common/test/common_tests/types/types_libraries_test.cljc +++ b/common/test/common_tests/types/types_libraries_test.cljc @@ -15,9 +15,11 @@ [app.common.types.pages-list :as ctpl] [app.common.types.typographies-list :as ctyl] [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.ids-map :as thi] + [common-tests.helpers.shapes :as ths])) (t/use-fixtures :each thi/test-fixture) @@ -27,14 +29,14 @@ f3 (thf/sample-file :file3 :name "testing file") f4 (-> (thf/sample-file :file4 :page-label :page2) (thf/add-sample-page :page3 :name "testing page") - (thf/add-sample-shape :shape1)) + (ths/add-sample-shape :shape1)) f5 (-> f4 - (thf/add-sample-shape :shape2) + (ths/add-sample-shape :shape2) (thf/switch-to-page :page2) - (thf/add-sample-shape :shape3 :name "testing shape" :width 100)) - s1 (thf/get-shape f4 :shape1) - s2 (thf/get-shape f5 :shape2 :page-label :page3) - s3 (thf/get-shape f5 :shape3)] + (ths/add-sample-shape :shape3 :name "testing shape" :width 100)) + s1 (ths/get-shape f4 :shape1) + s2 (ths/get-shape f5 :shape2 :page-label :page3) + s3 (ths/get-shape f5 :shape3)] ;; (thf/pprint-file f4) @@ -75,7 +77,7 @@ (tho/add-simple-component :component1 :main-root :rect1)) file (-> (thf/sample-file :file) - (thf/instantiate-component :component1 :copy-root :library library)) + (thc/instantiate-component :component1 :copy-root :library library)) ;; Action file' (ctf/update-file-data @@ -89,7 +91,7 @@ components' (ctkl/components-seq (ctf/file-data file')) component' (first components') - copy-root' (thf/get-shape file' :copy-root) + copy-root' (ths/get-shape file' :copy-root) main-root' (ctf/get-ref-shape (ctf/file-data file') component' copy-root')] ;; Check @@ -106,11 +108,11 @@ (t/deftest test-absorb-colors (let [;; Setup library (-> (thf/sample-file :library :is-shared true) - (thf/add-sample-library-color :color1 {:name "Test color" + (ths/add-sample-library-color :color1 {:name "Test color" :color "#abcdef"})) file (-> (thf/sample-file :file) - (thf/add-sample-shape :shape1 + (ths/add-sample-shape :shape1 :type :rect :name "Rect1" :fills [{:fill-color "#abcdef" @@ -127,7 +129,7 @@ ;; Get colors' (ctcl/colors-seq (ctf/file-data file')) - shape1' (thf/get-shape file' :shape1) + shape1' (ths/get-shape file' :shape1) fill' (first (:fills shape1'))] ;; Check @@ -143,10 +145,10 @@ (t/deftest test-absorb-typographies (let [;; Setup library (-> (thf/sample-file :library :is-shared true) - (thf/add-sample-typography :typography1 {:name "Test typography"})) + (ths/add-sample-typography :typography1 {:name "Test typography"})) file (-> (thf/sample-file :file) - (thf/add-sample-shape :shape1 + (ths/add-sample-shape :shape1 :type :text :name "Text1" :content {:type "root" @@ -178,7 +180,7 @@ ;; Get typographies' (ctyl/typographies-seq (ctf/file-data file')) - shape1' (thf/get-shape file' :shape1) + shape1' (ths/get-shape file' :shape1) text-node' (d/seek #(some? (:text %)) (txt/node-seq (:content shape1')))] ;; Check diff --git a/frontend/test/frontend_tests/state_components_sync_test.cljs b/frontend/test/frontend_tests/state_components_sync_test.cljs index 8ae88562a..7907f4ddd 100644 --- a/frontend/test/frontend_tests/state_components_sync_test.cljs +++ b/frontend/test/frontend_tests/state_components_sync_test.cljs @@ -21,337 +21,6 @@ (t/use-fixtures :each {:before thp/reset-idmap!}) -;; === Test touched ====================== - -(t/deftest test-touched-from-lib - (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/move-to-library :lib1 "Library 1") - (thp/sample-page) - (thp/instantiate-component :instance1 - (thp/id :component1) - (thp/id :lib1))) - - [_group1 shape1'] - (thl/resolve-instance state (thp/id :instance1)) - - store (the/prepare-store state done - (fn [new-state] - ;; Expected shape tree: - ;; - ;; [Page] - ;; Root Frame - ;; Rect 1 #--> Rect 1 - ;; Rect 1* ---> Rect 1 - ;; #{:fill-group} - ;; - (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-nested-upper - (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" - :fill-color clr/black - :fill-opacity 0}) - (thp/frame-shapes :frame1 - [(thp/id :instance1) - (thp/id :shape2)]) - (thp/make-component :main2 :component2 - [(thp/id :frame1)]) - (thp/instantiate-component :instance2 - (thp/id :component2))) - - [_instance2 _instance1 shape1' _shape2'] - (thl/resolve-instance state (thp/id :instance2)) - - store (the/prepare-store state done - (fn [new-state] - ;; Expected shape tree: - ;; - ;; [Page] - ;; Root Frame - ;; Rect 1 - ;; Rect 1 - ;; Group - ;; Rect 1 #--> Rect 1 - ;; Rect 1 ---> Rect 1 - ;; Circle 1 - ;; Group #--> Group - ;; Rect 1 @--> Rect 1 - ;; Rect 1 ---> Rect 1 - ;; Circle 1* ---> Circle 1 - ;; #{:fill-group} - ;; - ;; [Rect 1] - ;; page1 / Rect 1 - ;; - ;; [Group] - ;; page1 / Group - ;; - (let [[[instance2 instance1 shape1 shape2] - [c-instance2 c-instance1 c-shape1 c-shape2] _component] - (thl/resolve-instance-and-main - new-state - (thp/id :instance2))] - - (t/is (= (:name instance2) "Board")) - (t/is (= (:touched instance2) nil)) - (t/is (= (:name instance1) "Rect 1")) - (t/is (= (:touched instance1) nil)) - (t/is (= (:name shape1) "Circle 1")) - (t/is (= (:touched shape1) #{:fill-group})) - (t/is (= (:fill-color shape1) clr/test)) - (t/is (= (:fill-opacity shape1) 0.5)) - (t/is (= (:name shape2) "Rect 1")) - (t/is (= (:touched shape2) nil)) - (t/is (= (:fill-color shape2) clr/white)) - (t/is (= (:fill-opacity shape2) 1)) - - (t/is (= (:name c-instance2) "Board")) - (t/is (= (:touched c-instance2) nil)) - (t/is (= (:name c-instance1) "Rect 1")) - (t/is (= (:touched c-instance1) nil)) - (t/is (= (:name c-shape1) "Circle 1")) - (t/is (= (:touched c-shape1) nil)) - (t/is (= (:fill-color c-shape1) clr/black)) - (t/is (= (:fill-opacity c-shape1) 0)) - (t/is (= (:name c-shape2) "Rect 1")) - (t/is (= (:touched c-shape2) nil)) - (t/is (= (:fill-color c-shape2) clr/white)) - (t/is (= (:fill-opacity c-shape2) 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-nested-lower-near - (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" - :fill-color clr/black - :fill-opacity 0}) - (thp/frame-shapes :frame1 - [(thp/id :instance1) - (thp/id :shape2)]) - (thp/make-component :instance2 :component2 - [(thp/id :frame1)]) - (thp/instantiate-component :instance2 - (thp/id :component2))) - - [_instance2 _instance1 _shape1' shape2'] - (thl/resolve-instance state (thp/id :instance2)) - - store (the/prepare-store state done - (fn [new-state] - ;; Expected shape tree: - ;; - ;; [Page] - ;; Root Frame - ;; Rect 1 - ;; Rect 1 - ;; Group - ;; Rect 1 #--> Rect 1 - ;; Rect 1 ---> Rect 1 - ;; Circle 1 - ;; Group #--> Group - ;; Rect 1 @--> Rect 1 - ;; Rect 1* ---> Rect 1 - ;; #{:fill-group} - ;; Circle 1 ---> Circle 1 - ;; - ;; [Rect 1] - ;; page1 / Rect 1 - ;; - ;; [Group] - ;; page1 / Group - ;; - (let [[[instance2 instance1 shape1 shape2] - [c-instance2 c-instance1 c-shape1 c-shape2] _component] - (thl/resolve-instance-and-main - new-state - (thp/id :instance2))] - - (t/is (= (:name instance2) "Board")) - (t/is (= (:touched instance2) nil)) - (t/is (= (:name instance1) "Rect 1")) - (t/is (= (:touched instance1) nil)) - (t/is (= (:name shape1) "Circle 1")) - (t/is (= (:touched shape1) nil)) - (t/is (= (:fill-color shape1) clr/black)) - (t/is (= (:fill-opacity shape1) 0)) - (t/is (= (:name shape2) "Rect 1")) - (t/is (= (:touched shape2) #{:fill-group})) - (t/is (= (:fill-color shape2) clr/test)) - (t/is (= (:fill-opacity shape2) 0.5)) - - (t/is (= (:name c-instance2) "Board")) - (t/is (= (:touched c-instance2) nil)) - (t/is (= (:name c-instance1) "Rect 1")) - (t/is (= (:touched c-instance1) nil)) - (t/is (= (:name c-shape1) "Circle 1")) - (t/is (= (:touched c-shape1) nil)) - (t/is (= (:fill-color c-shape1) clr/black)) - (t/is (= (:fill-opacity c-shape1) 0)) - (t/is (= (:name c-shape2) "Rect 1")) - (t/is (= (:touched c-shape2) nil)) - (t/is (= (:fill-color c-shape2) clr/white)) - (t/is (= (:fill-opacity c-shape2) 1)))))] - - (ptk/emit! - store - (dch/update-shapes [(:id shape2')] - (fn [shape] - (merge shape {:fill-color clr/test - :fill-opacity 0.5}))) - :the/end)))) - -(t/deftest test-touched-nested-lower-remote - (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" - :fill-color clr/black - :fill-opacity 0}) - (thp/frame-shapes :frame1 - [(thp/id :instance1) - (thp/id :shape2)]) - (thp/make-component :instance2 :component2 - [(thp/id :frame1)]) - (thp/instantiate-component :instance2 - (thp/id :component2))) - - [instance2 _instance1 _shape1' shape2'] - (thl/resolve-instance state (thp/id :instance2)) - - store (the/prepare-store state done - (fn [new-state] - ;; Expected shape tree: - ;; - ;; [Page] - ;; Root Frame - ;; Rect 1 - ;; Rect 1 - ;; Group - ;; Rect 1 #--> Rect 1 - ;; Rect 1* ---> Rect 1 - ;; #{:fill-group} - ;; Circle 1 - ;; Group #--> Group - ;; Rect 1 @--> Rect 1 - ;; Rect 1 ---> Rect 1 - ;; Circle 1 ---> Circle 1 - ;; - ;; [Rect 1] - ;; page1 / Rect 1 - ;; - ;; [Group] - ;; page1 / Group - ;; - (let [[[instance2 instance1 shape1 shape2] - [c-instance2 c-instance1 c-shape1 c-shape2] _component] - (thl/resolve-instance-and-main - new-state - (thp/id :instance2))] - - (t/is (= (:name instance2) "Board")) - (t/is (= (:touched instance2) nil)) - (t/is (= (:name instance1) "Rect 1")) - (t/is (= (:touched instance1) nil)) - (t/is (= (:name shape1) "Circle 1")) - (t/is (= (:touched shape1) nil)) - (t/is (= (:fill-color shape1) clr/black)) - (t/is (= (:fill-opacity shape1) 0)) - (t/is (= (:name shape2) "Rect 1")) - (t/is (= (:touched shape2) #{:fill-group})) - (t/is (= (:fill-color shape2) clr/test)) - (t/is (= (:fill-opacity shape2) 0.5)) - (t/is (= (:name c-instance2) "Board")) - (t/is (= (:touched c-instance2) nil)) - (t/is (= (:name c-instance1) "Rect 1")) - (t/is (= (:touched c-instance1) nil)) - (t/is (= (:name c-shape1) "Circle 1")) - (t/is (= (:touched c-shape1) nil)) - (t/is (= (:fill-color c-shape1) clr/black)) - (t/is (= (:fill-opacity c-shape1) 0)) - (t/is (= (:name c-shape2) "Rect 1")) - (t/is (= (:touched c-shape2) #{:fill-group})))))] - - (ptk/emit! - store - (dch/update-shapes [(:id shape2')] - (fn [shape] - (merge shape {:fill-color clr/test - :fill-opacity 0.5}))) - (dwl/update-component (:id instance2)) - :the/end)))) - ;; === Test reset changes ====================== (t/deftest test-reset-changes