mirror of
https://github.com/penpot/penpot.git
synced 2025-12-11 22:14:05 +01:00
🎉 Update tests
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.types.component :as ctk]
|
||||
[app.common.types.container :as ctn]
|
||||
[app.common.types.file :as ctf]
|
||||
[app.main.data.workspace.state-helpers :as wsh]
|
||||
[cljs.pprint :refer [pprint]]
|
||||
[cljs.test :as t :include-macros true]
|
||||
@@ -16,6 +17,29 @@
|
||||
|
||||
;; ---- Helpers to manage libraries and synchronization
|
||||
|
||||
(defn is-main-instance-root
|
||||
[shape]
|
||||
(t/is (nil? (:shape-ref shape)))
|
||||
(t/is (some? (:component-id shape)))
|
||||
(t/is (= (:component-root? shape) true)))
|
||||
|
||||
(defn is-main-instance-subroot
|
||||
[shape]
|
||||
(t/is (some? (:component-id shape))) ; shape-ref may or may be not nil
|
||||
(t/is (= (:component-root? shape) true)))
|
||||
|
||||
(defn is-main-instance-child
|
||||
[shape]
|
||||
(t/is (nil? (:component-id shape))) ; shape-ref may or may be not nil
|
||||
(t/is (nil? (:component-file shape)))
|
||||
(t/is (nil? (:component-root? shape))))
|
||||
|
||||
(defn is-main-instance-inner
|
||||
[shape]
|
||||
(if (some? (:component-id shape))
|
||||
(is-main-instance-subroot shape)
|
||||
(is-main-instance-child shape)))
|
||||
|
||||
(defn is-instance-root
|
||||
[shape]
|
||||
(t/is (some? (:shape-ref shape)))
|
||||
@@ -58,27 +82,25 @@
|
||||
(defn resolve-instance
|
||||
"Get the shape with the given id and all its children, and
|
||||
verify that they are a well constructed instance tree."
|
||||
[state root-inst-id]
|
||||
(let [page (thp/current-page state)
|
||||
root-inst (ctn/get-shape page root-inst-id)
|
||||
shapes-inst (cph/get-children-with-self (:objects page)
|
||||
root-inst-id)]
|
||||
(is-instance-root (first shapes-inst))
|
||||
(run! is-instance-inner (rest shapes-inst))
|
||||
[state root-id]
|
||||
(let [page (thp/current-page state)
|
||||
shapes (cph/get-children-with-self (:objects page)
|
||||
root-id)]
|
||||
(is-instance-root (first shapes))
|
||||
(run! is-instance-inner (rest shapes))
|
||||
|
||||
shapes-inst))
|
||||
shapes))
|
||||
|
||||
(defn resolve-noninstance
|
||||
"Get the shape with the given id and all its children, and
|
||||
verify that they are not a component instance."
|
||||
[state root-inst-id]
|
||||
(let [page (thp/current-page state)
|
||||
root-inst (ctn/get-shape page root-inst-id)
|
||||
shapes-inst (cph/get-children-with-self (:objects page)
|
||||
root-inst-id)]
|
||||
(run! is-noninstance shapes-inst)
|
||||
[state root-id]
|
||||
(let [page (thp/current-page state)
|
||||
shapes (cph/get-children-with-self (:objects page)
|
||||
root-id)]
|
||||
(run! is-noninstance shapes)
|
||||
|
||||
shapes-inst))
|
||||
shapes))
|
||||
|
||||
(defn resolve-instance-and-main
|
||||
"Get the shape with the given id and all its children, and also
|
||||
@@ -87,38 +109,39 @@
|
||||
(resolve-instance-and-main state root-inst-id false))
|
||||
|
||||
([state root-inst-id subinstance?]
|
||||
(let [page (thp/current-page state)
|
||||
root-inst (ctn/get-shape page root-inst-id)
|
||||
(let [page (thp/current-page state)
|
||||
root-inst (ctn/get-shape page root-inst-id)
|
||||
main-instance? (:main-instance? root-inst)
|
||||
|
||||
libs (wsh/get-libraries state)
|
||||
component (cph/get-component libs (:component-id root-inst))
|
||||
libs (wsh/get-libraries state)
|
||||
component (ctf/get-component libs (:component-id root-inst))
|
||||
library (ctf/get-component-library libs root-inst)
|
||||
|
||||
shapes-inst (cph/get-children-with-self (:objects page) root-inst-id)
|
||||
shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst))
|
||||
|
||||
unique-refs (into #{} (map :shape-ref) shapes-inst)
|
||||
shapes-inst (cph/get-children-with-self (:objects page) root-inst-id)
|
||||
shapes-main (ctf/get-component-shapes (:data library) component)
|
||||
unique-refs (into #{} (map :shape-ref) shapes-inst)
|
||||
|
||||
main-exists? (fn [shape]
|
||||
(let [component-shape
|
||||
(cph/get-component-shape (:objects page) shape)
|
||||
|
||||
component
|
||||
(cph/get-component libs (:component-id component-shape))
|
||||
|
||||
main-shape
|
||||
(ctn/get-shape component (:shape-ref shape))]
|
||||
|
||||
(let [main-shape (ctf/get-ref-shape (:data library) component shape)]
|
||||
(t/is (some? main-shape))))]
|
||||
|
||||
;; Validate that the instance tree is well constructed
|
||||
(if subinstance?
|
||||
(is-instance-subroot (first shapes-inst))
|
||||
(is-instance-root (first shapes-inst)))
|
||||
(run! is-instance-inner (rest shapes-inst))
|
||||
(t/is (= (count shapes-inst)
|
||||
(count shapes-main)
|
||||
(count unique-refs)))
|
||||
(run! main-exists? shapes-inst)
|
||||
(if main-instance?
|
||||
(do
|
||||
(if subinstance?
|
||||
(is-main-instance-subroot (first shapes-inst))
|
||||
(is-main-instance-root (first shapes-inst)))
|
||||
(run! is-main-instance-inner (rest shapes-inst)))
|
||||
(do
|
||||
(if subinstance?
|
||||
(is-instance-subroot (first shapes-inst))
|
||||
(is-instance-root (first shapes-inst)))
|
||||
(run! is-instance-inner (rest shapes-inst))))
|
||||
|
||||
(t/is (= (count shapes-inst) (count shapes-main)))
|
||||
(when-not main-instance?
|
||||
(t/is (= (count shapes-inst) (count unique-refs)))
|
||||
(run! main-exists? shapes-inst))
|
||||
|
||||
[shapes-inst shapes-main component])))
|
||||
|
||||
@@ -131,24 +154,11 @@
|
||||
root-inst (ctn/get-shape page root-inst-id)
|
||||
|
||||
libs (wsh/get-libraries state)
|
||||
component (cph/get-component libs (:component-id root-inst))
|
||||
component (ctf/get-component libs (:component-id root-inst))
|
||||
library (ctf/get-component-library libs root-inst)
|
||||
|
||||
shapes-inst (cph/get-children-with-self (:objects page) root-inst-id)
|
||||
shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst))
|
||||
|
||||
unique-refs (into #{} (map :shape-ref) shapes-inst)
|
||||
|
||||
main-exists? (fn [shape]
|
||||
(let [component-shape
|
||||
(cph/get-component-shape (:objects page) shape)
|
||||
|
||||
component
|
||||
(cph/get-component libs (:component-id component-shape))
|
||||
|
||||
main-shape
|
||||
(ctn/get-shape component (:shape-ref shape))]
|
||||
|
||||
(t/is (some? main-shape))))]
|
||||
shapes-main (ctf/get-component-shapes (:data library) component)]
|
||||
|
||||
;; Validate that the instance tree is well constructed
|
||||
(is-instance-root (first shapes-inst))
|
||||
@@ -158,14 +168,12 @@
|
||||
(defn resolve-component
|
||||
"Get the component with the given id and all its shapes."
|
||||
[state component-id]
|
||||
(let [page (thp/current-page state)
|
||||
libs (wsh/get-libraries state)
|
||||
component (cph/get-component libs component-id)
|
||||
root-main (ctk/get-component-root component)
|
||||
shapes-main (cph/get-children-with-self (:objects component) (:id root-main))]
|
||||
(let [libs (wsh/get-libraries state)
|
||||
component (ctf/get-component libs component-id)
|
||||
library (ctf/get-component-library libs component)
|
||||
shapes-main (ctf/get-component-shapes (:data library) component)]
|
||||
|
||||
;; Validate that the component tree is well constructed
|
||||
(run! is-noninstance shapes-main)
|
||||
|
||||
[shapes-main component]))
|
||||
|
||||
|
||||
@@ -7,20 +7,14 @@
|
||||
(ns frontend-tests.helpers.pages
|
||||
(:require
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.geom.shapes :as gsh]
|
||||
[app.common.pages :as cp]
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.types.shape :as cts]
|
||||
[app.common.uuid :as uuid]
|
||||
[app.main.data.workspace :as dw]
|
||||
[app.main.data.workspace.groups :as dwg]
|
||||
[app.main.data.workspace.layout :as layout]
|
||||
[app.main.data.workspace.libraries-helpers :as dwlh]
|
||||
[app.main.data.workspace.state-helpers :as wsh]
|
||||
[beicon.core :as rx]
|
||||
[cljs.pprint :refer [pprint]]
|
||||
[cljs.test :as t :include-macros true]
|
||||
[potok.core :as ptk]))
|
||||
[app.main.data.workspace.state-helpers :as wsh]))
|
||||
|
||||
;; ---- Helpers to manage pages and objects
|
||||
|
||||
@@ -32,10 +26,12 @@
|
||||
:workspace-layout layout/default-layout
|
||||
:workspace-global layout/default-global
|
||||
:workspace-data {:id current-file-id
|
||||
:options {:components-v2 true}
|
||||
:components {}
|
||||
:pages []
|
||||
:pages-index {}}
|
||||
:workspace-libraries {}})
|
||||
:workspace-libraries {}
|
||||
:features {:components-v2 true}})
|
||||
|
||||
(def ^:private idmap (atom {}))
|
||||
|
||||
@@ -56,6 +52,11 @@
|
||||
(let [page (current-page state)]
|
||||
(get-in page [:objects (id label)])))
|
||||
|
||||
(defn get-children
|
||||
[state label]
|
||||
(let [page (current-page state)]
|
||||
(cph/get-children (:objects page) (id label))))
|
||||
|
||||
(defn sample-page
|
||||
([state] (sample-page state {}))
|
||||
([state {:keys [id name] :as props
|
||||
@@ -106,7 +107,7 @@
|
||||
objects (wsh/lookup-page-objects state (:id page))
|
||||
shapes (dwg/shapes-for-grouping objects shape-ids)
|
||||
|
||||
[group component-root changes]
|
||||
[group component-id changes]
|
||||
(dwlh/generate-add-component nil
|
||||
shapes
|
||||
(:objects page)
|
||||
@@ -115,7 +116,7 @@
|
||||
true)]
|
||||
|
||||
(swap! idmap assoc instance-label (:id group)
|
||||
component-label (:id component-root))
|
||||
component-label component-id)
|
||||
(update state :workspace-data
|
||||
cp/process-changes (:redo-changes changes))))
|
||||
|
||||
@@ -148,7 +149,9 @@
|
||||
assoc library-id {:id library-id
|
||||
:name name
|
||||
:data {:id library-id
|
||||
:options (:options data)
|
||||
:pages (:pages data)
|
||||
:pages-index (:pages-index data)
|
||||
:components (:components data)}})
|
||||
(update :workspace-data
|
||||
assoc :components {} :pages [] :pages-index {}))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user