From d6f3efb3588d9dfc67214cb246c24abe43c8387a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Fri, 5 Feb 2021 09:32:37 +0100 Subject: [PATCH] :tada: Add more tests for components --- ...y_sync.cljs => test_components_basic.cljs} | 46 +------ frontend/tests/app/test_components_sync.cljs | 123 ++++++++++++++++++ frontend/tests/app/test_shapes.cljs | 61 +++++++++ 3 files changed, 185 insertions(+), 45 deletions(-) rename frontend/tests/app/{test_library_sync.cljs => test_components_basic.cljs} (89%) create mode 100644 frontend/tests/app/test_components_sync.cljs create mode 100644 frontend/tests/app/test_shapes.cljs diff --git a/frontend/tests/app/test_library_sync.cljs b/frontend/tests/app/test_components_basic.cljs similarity index 89% rename from frontend/tests/app/test_library_sync.cljs rename to frontend/tests/app/test_components_basic.cljs index edee2cf46..5f46a3366 100644 --- a/frontend/tests/app/test_library_sync.cljs +++ b/frontend/tests/app/test_components_basic.cljs @@ -1,4 +1,4 @@ -(ns app.test-library-sync +(ns app.test-components-basic (:require [cljs.test :as t :include-macros true] [cljs.pprint :refer [pprint]] [clojure.stacktrace :as stk] @@ -17,50 +17,6 @@ (t/use-fixtures :each {:before thp/reset-idmap!}) -(t/deftest test-create-page - (t/testing "create page" - (let [state (-> thp/initial-state - (thp/sample-page)) - page (thp/current-page state)] - (t/is (= (:name page) "page1"))))) - -(t/deftest test-create-shape - (t/testing "create shape" - (let [state (-> thp/initial-state - (thp/sample-page) - (thp/sample-shape :shape1 :rect - {:name "Rect 1"})) - shape (thp/get-shape state :shape1)] - (t/is (= (:name shape) "Rect 1"))))) - -(t/deftest synctest - (t/testing "synctest" - (let [state {:workspace-local {:color-for-rename "something"}} - new-state (->> state - (the/do-update - dwl/clear-color-for-rename))] - (t/is (= (get-in new-state [:workspace-local :color-for-rename]) - nil))))) - -(t/deftest asynctest - (t/testing "asynctest" - (t/async done - (let [state {} - color {:color "#ffffff"}] - (->> state - (the/do-watch-update - (dwl/add-recent-color color)) - (rx/map - (fn [new-state] - (t/is (= (get-in new-state [:workspace-file - :data - :recent-colors]) - [color])) - (t/is (= (get-in new-state [:workspace-data - :recent-colors]) - [color])))) - (rx/subs done done)))))) - (t/deftest test-add-component-from-single-shape (t/async done (try diff --git a/frontend/tests/app/test_components_sync.cljs b/frontend/tests/app/test_components_sync.cljs new file mode 100644 index 000000000..a7ae79ce3 --- /dev/null +++ b/frontend/tests/app/test_components_sync.cljs @@ -0,0 +1,123 @@ +(ns app.test-components-sync + (:require [cljs.test :as t :include-macros true] + [cljs.pprint :refer [pprint]] + [clojure.stacktrace :as stk] + [beicon.core :as rx] + [linked.core :as lks] + [app.test-helpers.events :as the] + [app.test-helpers.pages :as thp] + [app.test-helpers.libraries :as thl] + [app.common.geom.point :as gpt] + [app.common.data :as d] + [app.common.pages.helpers :as cph] + [app.main.data.workspace.common :as dwc] + [app.main.data.workspace.libraries :as dwl] + [app.main.data.workspace.libraries-helpers :as dwlh])) + +(t/use-fixtures :each + {:before thp/reset-idmap!}) + +(t/deftest test-touched + (t/async done + (try + (let [state (-> thp/initial-state + (thp/sample-page) + (thp/sample-shape :shape1 :rect + {:name "Rect 1" + :fill-color "#ffffff" + :fill-opacity 1}) + (thp/make-component :instance1 + [(thp/id :shape1)])) + + shape1 (thp/get-shape state :shape1) + instance1 (thp/get-shape state :instance1) + + update-shape (fn [shape] + (merge shape {:fill-color "#fabada" + :fill-opacity 0.5}))] + + (->> state + (the/do-watch-update (dwc/update-shapes [(:id shape1)] + update-shape)) + (rx/do + (fn [new-state] + (let [shape1 (thp/get-shape new-state :shape1) + + [[group shape1] [c-group c-shape1] component] + (thl/resolve-instance-and-master + new-state + (:id instance1)) + + file (dwlh/get-local-file new-state)] + + (t/is (= (:fill-color shape1) "#fabada")) + (t/is (= (:fill-opacity shape1) 0.5)) + (t/is (= (:touched shape1) #{:fill-group})) + (t/is (= (:fill-color c-shape1) "#ffffff")) + (t/is (= (:fill-opacity c-shape1) 1)) + (t/is (= (:touched c-shape1) nil))))) + + (rx/subs + done + #(do + (println (.-stack %)) + (done))))) + + (catch :default e + (println (.-stack e)) + (done))))) + +(t/deftest test-reset-changes + (t/async done + (try + (let [state (-> thp/initial-state + (thp/sample-page) + (thp/sample-shape :shape1 :rect + {:name "Rect 1" + :fill-color "#ffffff" + :fill-opacity 1}) + (thp/make-component :instance1 + [(thp/id :shape1)])) + + shape1 (thp/get-shape state :shape1) + instance1 (thp/get-shape state :instance1) + + update-shape (fn [shape] + (merge shape {:fill-color "#fabada" + :fill-opacity 0.5}))] + + (->> state + (the/do-watch-update (dwc/update-shapes [(:id shape1)] + update-shape)) + + (rx/mapcat #(the/do-watch-update + (dwl/reset-component (:id instance1)) %)) + + (rx/do + (fn [new-state] + (let [shape1 (thp/get-shape new-state :shape1) + + [[group shape1] [c-group c-shape1] component] + (thl/resolve-instance-and-master + new-state + (:id instance1)) + + file (dwlh/get-local-file new-state)] + + (t/is (= (:fill-color shape1) "#ffffff")) + (t/is (= (:fill-opacity shape1) 1)) + (t/is (= (:touched shape1) nil)) + (t/is (= (:fill-color c-shape1) "#ffffff")) + (t/is (= (:fill-opacity c-shape1) 1)) + (t/is (= (:touched c-shape1) nil))))) + + (rx/subs + done + #(do + (println (.-stack %)) + (done))))) + + (catch :default e + (println (.-stack e)) + (done))))) + diff --git a/frontend/tests/app/test_shapes.cljs b/frontend/tests/app/test_shapes.cljs new file mode 100644 index 000000000..157780071 --- /dev/null +++ b/frontend/tests/app/test_shapes.cljs @@ -0,0 +1,61 @@ +(ns app.test-shapes + (:require [cljs.test :as t :include-macros true] + [cljs.pprint :refer [pprint]] + [clojure.stacktrace :as stk] + [beicon.core :as rx] + [linked.core :as lks] + [app.test-helpers.events :as the] + [app.test-helpers.pages :as thp] + [app.test-helpers.libraries :as thl] + [app.common.geom.point :as gpt] + [app.common.data :as d] + [app.common.pages.helpers :as cph] + [app.main.data.workspace.libraries :as dwl])) + +(t/use-fixtures :each + {:before thp/reset-idmap!}) + +(t/deftest test-create-page + (t/testing "create page" + (let [state (-> thp/initial-state + (thp/sample-page)) + page (thp/current-page state)] + (t/is (= (:name page) "page1"))))) + +(t/deftest test-create-shape + (t/testing "create shape" + (let [state (-> thp/initial-state + (thp/sample-page) + (thp/sample-shape :shape1 :rect + {:name "Rect 1"})) + shape (thp/get-shape state :shape1)] + (t/is (= (:name shape) "Rect 1"))))) + +(t/deftest synctest + (t/testing "synctest" + (let [state {:workspace-local {:color-for-rename "something"}} + new-state (->> state + (the/do-update + dwl/clear-color-for-rename))] + (t/is (= (get-in new-state [:workspace-local :color-for-rename]) + nil))))) + +(t/deftest asynctest + (t/testing "asynctest" + (t/async done + (let [state {} + color {:color "#ffffff"}] + (->> state + (the/do-watch-update + (dwl/add-recent-color color)) + (rx/map + (fn [new-state] + (t/is (= (get-in new-state [:workspace-file + :data + :recent-colors]) + [color])) + (t/is (= (get-in new-state [:workspace-data + :recent-colors]) + [color])))) + (rx/subs done done)))))) +