0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 00:40:30 -05:00
penpot/frontend/test/frontend_tests/helpers/libraries.cljs

178 lines
6 KiB
Text
Raw Normal View History

;; 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 frontend-tests.helpers.libraries
(:require
[app.common.files.helpers :as cfh]
[app.common.types.container :as ctn]
2023-03-07 06:03:56 -05:00
[app.common.types.file :as ctf]
[app.main.data.workspace.state-helpers :as wsh]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.pages :as thp]))
;; ---- Helpers to manage libraries and synchronization
2023-03-07 06:03:56 -05:00
(defn is-main-instance-root
[shape]
(t/is (nil? (:shape-ref shape)))
(t/is (some? (:component-id shape)))
(t/is (= (:component-root shape) true)))
2023-03-07 06:03:56 -05:00
(defn is-main-instance-subroot
[shape]
(t/is (some? (:component-id shape))) ; shape-ref may or may be not nil
(t/is (nil? (:component-root shape))))
2023-03-07 06:03:56 -05:00
(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))))
2023-03-07 06:03:56 -05:00
(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)))
(t/is (some? (:component-id shape)))
(t/is (= (:component-root shape) true)))
(defn is-instance-subroot
[shape]
(t/is (some? (:shape-ref shape)))
(t/is (some? (:component-id shape)))
(t/is (nil? (:component-root shape))))
(defn is-instance-child
[shape]
(t/is (some? (:shape-ref shape)))
(t/is (nil? (:component-id shape)))
(t/is (nil? (:component-file shape)))
(t/is (nil? (:component-root shape))))
(defn is-instance-inner
[shape]
(if (some? (:component-id shape))
(is-instance-subroot shape)
(is-instance-child shape)))
(defn is-noninstance
[shape]
(t/is (nil? (:shape-ref shape)))
(t/is (nil? (:component-id shape)))
(t/is (nil? (:component-file shape)))
(t/is (nil? (:component-root shape)))
(t/is (nil? (:remote-synced shape)))
(t/is (nil? (:touched shape))))
(defn is-from-file
[shape file]
(t/is (= (:component-file shape)
(:id file))))
2021-02-04 11:29:12 -05:00
(defn resolve-instance
"Get the shape with the given id and all its children, and
verify that they are a well constructed instance tree."
2023-03-07 06:03:56 -05:00
[state root-id]
(let [page (thp/current-page state)
shapes (cfh/get-children-with-self (:objects page)
2023-03-07 06:03:56 -05:00
root-id)]
(is-instance-root (first shapes))
(run! is-instance-inner (rest shapes))
2021-02-04 11:29:12 -05:00
2023-03-07 06:03:56 -05:00
shapes))
2021-02-04 11:29:12 -05:00
(defn resolve-noninstance
"Get the shape with the given id and all its children, and
verify that they are not a component instance."
2023-03-07 06:03:56 -05:00
[state root-id]
(let [page (thp/current-page state)
shapes (cfh/get-children-with-self (:objects page)
2023-03-07 06:03:56 -05:00
root-id)]
(run! is-noninstance shapes)
2021-02-04 11:29:12 -05:00
2023-03-07 06:03:56 -05:00
shapes))
2021-02-04 11:29:12 -05:00
(defn resolve-instance-and-main
"Get the shape with the given id and all its children, and also
the main component and all its shapes."
([state root-inst-id]
(resolve-instance-and-main state root-inst-id false))
([state root-inst-id subinstance?]
2023-03-07 06:03:56 -05:00
(let [page (thp/current-page state)
root-inst (ctn/get-shape page root-inst-id)
main-instance? (:main-instance root-inst)
2023-03-07 06:03:56 -05:00
libs (wsh/get-libraries state)
component (ctf/get-component libs (:component-file root-inst) (:component-id root-inst))
2023-03-07 06:03:56 -05:00
library (ctf/get-component-library libs root-inst)
shapes-inst (cfh/get-children-with-self (:objects page) root-inst-id)
2023-03-07 06:03:56 -05:00
shapes-main (ctf/get-component-shapes (:data library) component)
unique-refs (into #{} (map :shape-ref) shapes-inst)
main-exists? (fn [shape]
2023-03-07 06:03:56 -05:00
(let [main-shape (ctf/get-ref-shape (:data library) component shape)]
(t/is (some? main-shape))))]
;; Validate that the instance tree is well constructed
2023-03-07 06:03:56 -05:00
(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])))
(defn resolve-instance-and-main-allow-dangling
"Get the shape with the given id and all its children, and also
the main component and all its shapes. Allows shapes with the
corresponding component shape missing."
[state root-inst-id]
(let [page (thp/current-page state)
root-inst (ctn/get-shape page root-inst-id)
libs (wsh/get-libraries state)
component (ctf/get-component libs (:component-file root-inst) (:component-id root-inst))
2023-03-07 06:03:56 -05:00
library (ctf/get-component-library libs root-inst)
shapes-inst (cfh/get-children-with-self (:objects page) root-inst-id)
2023-03-07 06:03:56 -05:00
shapes-main (ctf/get-component-shapes (:data library) component)]
;; Validate that the instance tree is well constructed
(is-instance-root (first shapes-inst))
[shapes-inst shapes-main component]))
2021-02-03 07:09:38 -05:00
(defn resolve-component
"Get the component with the given id and all its shapes."
[state component-file component-id]
2023-03-07 06:03:56 -05:00
(let [libs (wsh/get-libraries state)
component (ctf/get-component libs component-file component-id)
2023-03-07 06:03:56 -05:00
library (ctf/get-component-library libs component)
shapes-main (ctf/get-component-shapes (:data library) component)]
2021-02-03 07:09:38 -05:00
;; Validate that the component tree is well constructed
(run! is-noninstance shapes-main)
2021-02-03 07:09:38 -05:00
[shapes-main component]))