summaryrefslogtreecommitdiff
path: root/test/dompa/nodes_test.cljc
blob: 0340abb10fca0716534f620c62bd0e846cde2d25 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
(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"])))))

(defhtml greeting [who]
  ($ :span who))

(defhtml page [who]
  ($ :div
     (greeting who)))

(defhtml badge [text]
  ($ :span {:class "badge"} text))

(defhtml card [title]
  ($ :div {:class "card"}
     (badge title)))

(defhtml list-item [text]
  ($ :li text))

(defhtml my-list [items]
  ($ :ul
     (->> items
          (map list-item)
          (into []))))

(defhtml header [title]
  ($ :h1 title))

(defhtml nav [items]
  ($ :nav
     ($ :ul
        (->> items
             (map list-item)
             (into [])))))

(defhtml layout [title nav-items body]
  ($ :div
     (header title)
     (nav nav-items)
     ($ :main body)))

(deftest nested-defhtml-test
  (testing "simple nesting of defhtml"
    (is (= "<div><span>world</span></div>"
           (page "world"))))

  (testing "nested defhtml with attributes"
    (is (= "<div class=\"card\"><span class=\"badge\">Hello</span></div>"
           (card "Hello"))))

  (testing "nested defhtml via map/collection"
    (is (= "<ul><li>a</li><li>b</li><li>c</li></ul>"
           (my-list ["a" "b" "c"]))))

  (testing "deeply nested defhtml (three levels)"
    (is (= "<div><h1>Title</h1><nav><ul><li>a</li><li>b</li></ul></nav><main>content</main></div>"
           (layout "Title" ["a" "b"] "content"))))

  (testing "mixing defhtml and $ in the same parent"
    (is (= "<div><span>hi</span><p>bye</p></div>"
           (nodes/->html [($ :div
                             (greeting "hi")
                             ($ :p "bye"))])))))

(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)))))