From 884410c0d801bbfb3cf4a2e3a55185e15ebadb69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Thu, 4 Feb 2021 17:29:12 +0100 Subject: [PATCH] :tada: Add more tests for components --- .../tests/app/test_helpers/libraries.cljs | 27 +++++++ frontend/tests/app/test_library_sync.cljs | 81 ++++++++++++++++++- 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/frontend/tests/app/test_helpers/libraries.cljs b/frontend/tests/app/test_helpers/libraries.cljs index 77f2259e9..7e275f100 100644 --- a/frontend/tests/app/test_helpers/libraries.cljs +++ b/frontend/tests/app/test_helpers/libraries.cljs @@ -52,6 +52,33 @@ (t/is (= (:component-file shape) (:id file)))) +(defn resolve-instance + [state root-inst-id] + (let [page (thp/current-page state) + root-inst (cph/get-shape page root-inst-id) + shapes-inst (cph/get-object-with-children + root-inst-id + (:objects page))] + + ;; Validate that the instance tree is well constructed + (t/is (is-instance-root (first shapes-inst))) + (run! is-instance-child (rest shapes-inst)) + + shapes-inst)) + +(defn resolve-noninstance + [state root-inst-id] + (let [page (thp/current-page state) + root-inst (cph/get-shape page root-inst-id) + shapes-inst (cph/get-object-with-children + root-inst-id + (:objects page))] + + ;; Validate that the tree is not an instance + (run! is-noninstance shapes-inst) + + shapes-inst)) + (defn resolve-instance-and-master [state root-inst-id] (let [page (thp/current-page state) diff --git a/frontend/tests/app/test_library_sync.cljs b/frontend/tests/app/test_library_sync.cljs index b1712b533..edee2cf46 100644 --- a/frontend/tests/app/test_library_sync.cljs +++ b/frontend/tests/app/test_library_sync.cljs @@ -288,6 +288,48 @@ (println (.-stack e)) (done))))) +(t/deftest test-delete-component + (t/async done + (try + (let [state (-> thp/initial-state + (thp/sample-page) + (thp/sample-shape :shape1 :rect + {:name "Rect 1"}) + (thp/make-component :instance1 + [(thp/id :shape1)])) + + instance1 (thp/get-shape state :instance1) + component-id (:component-id instance1)] + + (->> state + (the/do-watch-update (dwl/delete-component + {:id component-id})) + (rx/do + (fn [new-state] + (let [[instance1 shape1] + (thl/resolve-instance + new-state + (:id instance1)) + + file (dwlh/get-local-file new-state) + component (cph/get-component + (:component-id instance1) + (:component-file instance1) + file + {})] + + (t/is (nil? component))))) + + (rx/subs + done + #(do + (println (.-stack %)) + (done))))) + + (catch :default e + (println (.-stack e)) + (done))))) + (t/deftest test-instantiate-component (t/async done (try @@ -322,9 +364,9 @@ (t/is (not= (:id instance1) (:id instance2))) (t/is (= (:id component) component-id)) - (t/is (= (:name instance2) "Component-7")) + (t/is (= (:name instance2) "Component-8")) (t/is (= (:name shape2) "Rect 1")) - (t/is (= (:name c-instance2) "Component-6")) + (t/is (= (:name c-instance2) "Component-7")) (t/is (= (:name c-shape2) "Rect 1"))))) (rx/subs @@ -337,3 +379,38 @@ (println (.-stack e)) (done))))) +(t/deftest test-detach-component + (t/async done + (try + (let [state (-> thp/initial-state + (thp/sample-page) + (thp/sample-shape :shape1 :rect + {:name "Rect 1"}) + (thp/make-component :instance1 + [(thp/id :shape1)])) + + instance1 (thp/get-shape state :instance1) + component-id (:component-id instance1)] + + (->> state + (the/do-watch-update (dwl/detach-component + (:id instance1))) + (rx/do + (fn [new-state] + (let [[instance1 shape1] + (thl/resolve-noninstance + new-state + (:id instance1))] + + (t/is (= (:name "Rect 1")))))) + + (rx/subs + done + #(do + (println (.-stack %)) + (done))))) + + (catch :default e + (println (.-stack e)) + (done))))) +