Skip to content

Commit cc9d99d

Browse files
committed
add tests and fix types
1 parent ded495d commit cc9d99d

File tree

3 files changed

+151
-52
lines changed

3 files changed

+151
-52
lines changed

src/renderer/document/events.cljs

+2-3
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,12 @@
138138

139139
(m/=> create [:function
140140
[:-> map? uuid? App]
141-
[:-> map? uuid? Vec2D App]])
141+
[:-> map? uuid? [:maybe Vec2D] App]])
142142
(defn create
143143
([db guid]
144144
(create db guid [595 842]))
145145
([db guid size]
146-
(-> db
147-
(h/create-tab (assoc db/default :id guid))
146+
(-> (h/create-tab db (assoc db/default :id guid))
148147
(element.h/create-default-canvas size)
149148
(h/center))))
150149

src/renderer/element/handlers.cljs

+4-1
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@
461461
(update-in (path db) dissoc id)
462462
(expand id))))))
463463

464+
(m/=> update-index [:function
465+
[:-> App fn? App]
466+
[:-> App uuid? fn? App]])
464467
(defn update-index
465468
([db f]
466469
(reduce (partial-right update-index f) db (selected-sorted-ids db)))
@@ -632,7 +635,7 @@
632635
child-els
633636
(add-children child-els)))))
634637

635-
(m/=> create-default-canvas [:-> App Vec2D App])
638+
(m/=> create-default-canvas [:-> App [:maybe Vec2D] App])
636639
(defn create-default-canvas
637640
[db size]
638641
(cond-> db

test/element_test.cljs

+145-48
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,122 @@
4444

4545
(testing "select same tags"
4646
(rf/dispatch [::e/add {:tag :rect
47-
:attrs {:width 100
48-
:height 100}}])
47+
:attrs {:width "100"
48+
:height "100"}}])
4949
(is (= :rect (-> @selected first :tag)))
5050

5151
(rf/dispatch [::e/add {:tag :rect
52-
:attrs {:width 100
53-
:height 100}}])
52+
:attrs {:width "100"
53+
:height "100"}}])
5454
(rf/dispatch [::e/select-same-tags])
5555
(is (= (count @selected) 2))))))
5656

57+
(deftest index
58+
(rf-test/run-test-sync
59+
(rf/dispatch [::app.e/initialize-db])
60+
(rf/dispatch [::document.e/new-from-template nil])
61+
62+
(rf/dispatch [::e/add {:tag :rect
63+
:attrs {:width "100"
64+
:height "100"}}])
65+
(rf/dispatch [::e/add {:tag :circle
66+
:attrs {:r "100"
67+
:cx "100"
68+
:cy "100"}}])
69+
70+
(rf/dispatch [::e/add {:tag :line
71+
:attrs {:x1 "0"
72+
:y1 "0"
73+
:x2 "50"
74+
:y2 "50"}}])
75+
76+
(let [elements (rf/subscribe [::s/root-children])]
77+
(testing "initial order"
78+
(is (= (mapv :tag @elements) [:rect :circle :line])))
79+
80+
(testing "lower"
81+
(rf/dispatch [::e/lower])
82+
(is (= (mapv :tag @elements) [:rect :line :circle])))
83+
84+
(testing "raise"
85+
(rf/dispatch [::e/raise])
86+
(is (= (mapv :tag @elements) [:rect :circle :line])))
87+
88+
(testing "raise when already on top"
89+
(rf/dispatch [::e/raise])
90+
(is (= (mapv :tag @elements) [:rect :circle :line])))
91+
92+
(testing "lower to bottom"
93+
(rf/dispatch [::e/lower-to-bottom])
94+
(is (= (mapv :tag @elements) [:line :rect :circle])))
95+
96+
(testing "raise to top"
97+
(rf/dispatch [::e/raise-to-top])
98+
(is (= (mapv :tag @elements) [:rect :circle :line]))))))
99+
100+
(deftest align
101+
(rf-test/run-test-sync
102+
(rf/dispatch [::app.e/initialize-db])
103+
(rf/dispatch [::document.e/new-from-template [800 600]])
104+
105+
(rf/dispatch [::e/add {:tag :rect
106+
:attrs {:x "50"
107+
:y "50"
108+
:width "100"
109+
:height "100"}}])
110+
111+
(let [elements (rf/subscribe [::s/selected])]
112+
(testing "align left"
113+
(rf/dispatch [::e/align :left])
114+
(is (= (-> @elements first :attrs) {:x "0"
115+
:y "50"
116+
:width "100"
117+
:height "100"})))
118+
119+
(testing "align top"
120+
(rf/dispatch [::e/align :top])
121+
(is (= (-> @elements first :attrs) {:x "0"
122+
:y "0"
123+
:width "100"
124+
:height "100"})))
125+
126+
(testing "align right"
127+
(rf/dispatch [::e/align :right])
128+
(is (= (-> @elements first :attrs) {:x "700"
129+
:y "0"
130+
:width "100"
131+
:height "100"})))
132+
133+
(testing "align bottom"
134+
(rf/dispatch [::e/align :bottom])
135+
(is (= (-> @elements first :attrs) {:x "700"
136+
:y "500"
137+
:width "100"
138+
:height "100"})))
139+
140+
(testing "align center vertical"
141+
(rf/dispatch [::e/align :center-vertical])
142+
(is (= (-> @elements first :attrs) {:x "700"
143+
:y "250"
144+
:width "100"
145+
:height "100"})))
146+
147+
(testing "align center horizontal"
148+
(rf/dispatch [::e/align :center-horizontal])
149+
(is (= (-> @elements first :attrs) {:x "350"
150+
:y "250"
151+
:width "100"
152+
:height "100"}))))))
153+
57154
(deftest visibility
58155
(rf-test/run-test-sync
59156
(rf/dispatch [::app.e/initialize-db])
60157
(rf/dispatch [::document.e/init])
61158

62159
(let [selected (rf/subscribe [::s/selected])]
63160
(rf/dispatch [::e/add {:tag :rect
64-
:attrs {:width 100
65-
:height 100}}])
161+
:attrs {:width "100"
162+
:height "100"}}])
66163
(testing "default state"
67164
(is (-> @selected first :visible)))
68165

@@ -81,8 +178,8 @@
81178

82179
(let [selected (rf/subscribe [::s/selected])]
83180
(rf/dispatch [::e/add {:tag :rect
84-
:attrs {:width 100
85-
:height 100}}])
181+
:attrs {:width "100"
182+
:height "100"}}])
86183
(testing "default state"
87184
(is (not (-> @selected first :label))))
88185

@@ -101,8 +198,8 @@
101198

102199
(let [selected (rf/subscribe [::s/selected])]
103200
(rf/dispatch [::e/add {:tag :rect
104-
:attrs {:width 100
105-
:height 100}}])
201+
:attrs {:width "100"
202+
:height "100"}}])
106203
(testing "default state"
107204
(is (not (-> @selected first :locked))))
108205

@@ -121,8 +218,8 @@
121218

122219
(let [selected (rf/subscribe [::s/selected])]
123220
(rf/dispatch [::e/add {:tag :rect
124-
:attrs {:width 100
125-
:height 100
221+
:attrs {:width "100"
222+
:height "100"
126223
:fill "white"}}])
127224
(is (= (-> @selected first :attrs :fill) "white"))
128225

@@ -145,8 +242,8 @@
145242

146243
(let [selected (rf/subscribe [::s/selected])]
147244
(rf/dispatch [::e/add {:tag :rect
148-
:attrs {:width 100
149-
:height 100}}])
245+
:attrs {:width "100"
246+
:height "100"}}])
150247

151248
(testing "delete selection"
152249
(let [id (-> @selected first :id)]
@@ -161,8 +258,8 @@
161258
(rf/dispatch [::document.e/init])
162259
(let [selected (rf/subscribe [::s/selected])]
163260
(rf/dispatch [::e/add {:tag :rect
164-
:attrs {:width 100
165-
:height 100}}])
261+
:attrs {:width "100"
262+
:height "100"}}])
166263
(rf/dispatch [::e/scale [2 4]])
167264
(is (= (-> @selected first :attrs :width) "200"))
168265
(is (= (-> @selected first :attrs :height) "400")))))
@@ -173,10 +270,10 @@
173270
(rf/dispatch [::document.e/init])
174271
(let [selected (rf/subscribe [::s/selected])]
175272
(rf/dispatch [::e/add {:tag :rect
176-
:attrs {:x 100
177-
:y 100
178-
:width 100
179-
:height 100}}])
273+
:attrs {:x "100"
274+
:y "100"
275+
:width "100"
276+
:height "100"}}])
180277
(rf/dispatch [::e/translate [50 100]])
181278
(is (= (-> @selected first :attrs :x) "150"))
182279
(is (= (-> @selected first :attrs :y) "200")))))
@@ -187,10 +284,10 @@
187284
(rf/dispatch [::document.e/init])
188285
(let [selected (rf/subscribe [::s/selected])]
189286
(rf/dispatch [::e/add {:tag :rect
190-
:attrs {:x 100
191-
:y 100
192-
:width 100
193-
:height 100}}])
287+
:attrs {:x "100"
288+
:y "100"
289+
:width "100"
290+
:height "100"}}])
194291
(rf/dispatch [::e/place [100 100]])
195292
(is (= (-> @selected first :attrs :x) "50"))
196293
(is (= (-> @selected first :attrs :y) "50")))))
@@ -201,10 +298,10 @@
201298
(rf/dispatch [::document.e/init])
202299
(let [selected (rf/subscribe [::s/selected])]
203300
(rf/dispatch [::e/add {:tag :rect
204-
:attrs {:x 100
205-
:y 100
206-
:width 100
207-
:height 100
301+
:attrs {:x "100"
302+
:y "100"
303+
:width "100"
304+
:height "100"
208305
:fill "red"}}])
209306
(rf/dispatch [::e/->path])
210307
(is (= (-> @selected first :tag) :path))
@@ -217,10 +314,10 @@
217314
(rf/dispatch [::document.e/init])
218315
(let [selected (rf/subscribe [::s/selected])]
219316
(rf/dispatch [::e/add {:tag :rect
220-
:attrs {:x 100
221-
:y 100
222-
:width 100
223-
:height 100
317+
:attrs {:x "100"
318+
:y "100"
319+
:width "100"
320+
:height "100"
224321
:fill "red"
225322
:stroke "black"}}])
226323
(rf/dispatch [::e/stroke->path])
@@ -234,17 +331,17 @@
234331
(rf/dispatch [::document.e/init])
235332
(let [selected (rf/subscribe [::s/selected])]
236333
(rf/dispatch [::e/add {:tag :rect
237-
:attrs {:x 100
238-
:y 100
239-
:width 100
240-
:height 100
334+
:attrs {:x "100"
335+
:y "100"
336+
:width "100"
337+
:height "100"
241338
:fill "red"
242339
:stroke "black"}}])
243340
(rf/dispatch [::e/add {:tag :rect
244-
:attrs {:x 100
245-
:y 100
246-
:width 100
247-
:height 100}}])
341+
:attrs {:x "100"
342+
:y "100"
343+
:width "100"
344+
:height "100"}}])
248345
(rf/dispatch [::e/select-all])
249346
(rf/dispatch [::e/boolean-operation :unite])
250347
(is (= (-> @selected first :tag) :path))
@@ -268,10 +365,10 @@
268365
(rf/dispatch [::app.e/initialize-db])
269366
(rf/dispatch [::document.e/init])
270367
(rf/dispatch [::e/add {:tag :rect
271-
:attrs {:x 100
272-
:y 100
273-
:width 100
274-
:height 100}}])
368+
:attrs {:x "100"
369+
:y "100"
370+
:width "100"
371+
:height "100"}}])
275372
(let [selected (rf/subscribe [::s/selected])
276373
id (-> @selected first :id)]
277374
(rf/dispatch [::e/animate :animate {}])
@@ -283,10 +380,10 @@
283380
(rf/dispatch [::app.e/initialize-db])
284381
(rf/dispatch [::document.e/init])
285382
(rf/dispatch [::e/add {:tag :rect
286-
:attrs {:x 100
287-
:y 100
288-
:width 100
289-
:height 100}}])
383+
:attrs {:x "100"
384+
:y "100"
385+
:width "100"
386+
:height "100"}}])
290387
(let [selected (rf/subscribe [::s/selected])
291388
id (-> @selected first :id)]
292389
(testing "group"

0 commit comments

Comments
 (0)