blob: da9b7dbba75a4c4b39e27de69cceb32ccc9d243c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
(ns dompa.nodes-test
#?(:clj
(:require
[clojure.test :refer [deftest is testing]]
[clojure.zip :as zip]
[dompa.html :as html]
[dompa.nodes :as nodes :refer [$ defhtml]]))
#?(:cljs
(:require
[cljs.test :refer-macros [deftest testing is]]
[clojure.zip :as zip]
[dompa.nodes :as nodes :refer [$] :refer-macros [defhtml]]
[dompa.html :as html])))
(defhtml hello [who]
($ :div
($ "hello " who)))
(deftest defhtml-test
(is (= "<div>hello world</div>"
(hello "world"))))
(defhtml list-items [items]
($ :ul
(->> items
(map (fn [item]
($ :li item)))
(into []))))
(deftest list-items-test
(is (= "<ul><li>one</li><li>two</li><li>three</li></ul>"
(list-items ["one" "two" "three"]))))
(defhtml empty-list-items [items]
($ :ul
(map (fn [item]
($ :li item))
items)))
(deftest empty-list-items-test
(testing "map over empty vector should not produce LazySeq string"
(is (= "<ul></ul>"
(empty-list-items []))))
(testing "map over non-empty vector should work"
(is (= "<ul><li>one</li><li>two</li></ul>"
(empty-list-items ["one" "two"])))))
(deftest $-test
(testing "a simple node"
(is (= {:node/name :div
:node/children [{:node/name :dompa/text
:node/value "hello world"}]}
($ :div "hello world"))))
(testing "a fragment node"
(is (= {:node/name :<>
:node/children [{:node/name :span
:node/children [{:node/name :dompa/text
:node/value "hello"}]}
{:node/name :span
:node/children [{:node/name :dompa/text
:node/value "world"}]}]}
($ :<>
($ :span
($ "hello"))
($ :span
($ "world"))))))
(testing "a nil node"
(is (= nil
($ nil))))
(testing "a string node"
(is (= {:node/name :dompa/text
:node/value "hello world"}
($ "hello world"))))
(testing "a node of multiple sub-nodes"
(is (= {:node/name :<>
:node/children [{:node/name :dompa/text
:node/value "hello"}
{:node/name :dompa/text
:node/value "12345"}
{:node/name :dompa/text
:node/value "123.3"}
{:node/name :dompa/text
:node/value "world"}]}
($ "hello" 12345 nil 123.3 "world")))
(is (= {:node/name :<>
:node/children [{:node/name :dompa/text
:node/value "hello"}
{:node/name :dompa/text
:node/value "12345"}
{:node/name :<>
:node/children [{:node/name :dompa/text
:node/value "w"}
{:node/name :dompa/text
:node/value "o"}
{:node/name :dompa/text
:node/value "r"}
{:node/name :dompa/text
:node/value "l"}
{:node/name :dompa/text
:node/value "d"}]}]}
($ "hello" 12345 (map #($ %) ["w" "o" "r" "l" "d"]))))
(is (= {:node/name :<>
:node/children [{:node/name :dompa/text
:node/value "hello"}
{:node/name :<>
:node/children [{:node/name :dompa/text
:node/value "w"}
{:node/name :<>
:node/children [{:node/name :dompa/text
:node/value "o"}
{:node/name :dompa/text
:node/value "r"}
{:node/name :<>
:node/children [{:node/name :dompa/text
:node/value "l"}
{:node/name :dompa/text
:node/value "d"}]}]}]}]}
($ "hello"
(map (fn [x]
($ x))
["w" (map (fn [x]
($ x))
["o" "r" (map (fn [x]
($ x))
["l" "d"])])]))))))
(deftest nil-in-hierarchy-does-not-remove-siblings-test
(testing "nil in a fragment should not remove sibling elements"
(is (= "<div>one</div><div>two</div>"
(nodes/->html
[($ :<>
($ :div "one")
($ :div "two")
nil)]))))
(testing "nil between siblings should not affect them"
(is (= "<div>one</div><div>two</div>"
(nodes/->html
[($ :<>
($ :div "one")
nil
($ :div "two"))]))))
(testing "multiple nils should not affect siblings"
(is (= "<div>one</div><div>two</div><div>three</div>"
(nodes/->html
[($ :<>
nil
($ :div "one")
nil
($ :div "two")
nil
($ :div "three")
nil)]))))
(testing "nil inside nested element should not affect parent siblings"
(is (= "<div><span>hello</span></div><div>world</div>"
(nodes/->html
[($ :<>
($ :div
($ :span "hello")
nil)
($ :div "world"))])))))
(deftest traverse-test
(let [traverser-fn (fn [node]
(if (= :dompa/text (:node/name node))
(assoc node :node/value "world hello")
node))]
(is (= "<div>world hello</div>"
(-> (html/->nodes "<div>hello world</div>")
(nodes/traverse traverser-fn)
nodes/->html)))))
(deftest zip-test
(let [nodes (html/->nodes "<div><p>hello</p><p>world</p></div>")
zipper (nodes/zip (first nodes))]
(is (= :div
(:node/name (zip/node zipper))))
(is (= "hello"
(-> zipper
zip/down
zip/down
zip/node
:node/value)))))
|