summaryrefslogtreecommitdiff
path: root/test/dompa/coordinates_test.cljc
blob: 85a45180ef61a6f20d985a538d3695de6be73962 (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
(ns dompa.coordinates-test
  #?(:clj (:require [clojure.test :refer [deftest testing is]]
                    [dompa.coordinates :as coordinates]))
  #?(:cljs (:require [cljs.test :refer-macros [deftest testing is]]
                     [dompa.coordinates :as coordinates])))

(deftest compose-test
  (testing "Create coordinates"
    (let [html "<div>hello</div>"]
      (is (= {:html        html
              :coordinates [[0 4] [5 9] [10 15]]}
             (coordinates/compose html)))))

  (testing "Create coordinates with invalid HTML"
    (let [html "<div>hello"]
      (is (= {:html        html
              :coordinates [[0 4] [5 9]]}
             (coordinates/compose html))))

    (let [html "<div>hello</span>"]
      (is (= {:html        html
              :coordinates [[0 4] [5 9] [10 16]]}
             (coordinates/compose html))))

    (let [html "<div"]
      (is (= {:html        html
              :coordinates []}
             (coordinates/compose html))))

    (let [html "div>"]
      (is (= {:html        html
              :coordinates [[0 3]]}
             (coordinates/compose html))))

    (let [html "<>"]
      (is (= {:html        html
              :coordinates [[0 1]]}
             (coordinates/compose html)))))

  (testing "Create coordinates with just text"
    (let [html "hello"]
      (is (= {:html        html
              :coordinates [[0 4]]}
             (coordinates/compose html)))))

  (testing "Create coordinates with text starting"
    (let [html "hello<div></div>"]
      (is (= {:html        html
              :coordinates [[0 4] [5 9] [10 15]]}
             (coordinates/compose html)))))

  (testing "Create coordinates with text ending"
    (let [html "<div></div>hello"]
      (is (= {:html        html
              :coordinates [[0 4] [5 10] [11 15]]}
             (coordinates/compose html))))))

(deftest unify-test
  (testing "Unify coordinates"
    (let [html "<div>hello</div>"]
      (is (= {:html        html
              :coordinates [[0 15] [5 9]]}
             (-> (coordinates/compose html)
                 coordinates/unify)))))

  (testing "Unify coordinates with invalid HTML"
    (let [html "<div>hello"]
      (is (= {:html        html
              :coordinates [[0 4] [5 9]]}
             (-> (coordinates/compose html)
                 coordinates/unify))))

    (let [html "<div>hello</span>"]
      (is (= {:html        html
              :coordinates [[0 4] [5 9]]}
             (-> (coordinates/compose html)
                 coordinates/unify)))))

  (testing "Unify coordinates with just text"
    (let [html "hello"]
      (is (= {:html        html
              :coordinates [[0 4]]}
             (-> (coordinates/compose html)
                 coordinates/unify)))))

  (testing "Unify coordinates with text starting"
    (let [html "hello<div></div>"]
      (is (= {:html        html
              :coordinates [[0 4] [5 15]]}
             (-> (coordinates/compose html)
                 coordinates/unify)))))

  (testing "Unify coordinates with text ending"
    (let [html "<div></div>hello"]
      (is (= {:html        html
              :coordinates [[0 10] [11 15]]}
             (-> (coordinates/compose html)
                 coordinates/unify))))))

(deftest nodes-test
  (testing "Create nodes"
    (is (= [{:node/name     :div
             :node/attrs    {}
             :node/children [{:node/name  :dompa/text
                              :node/value "hello"}]}]
           (-> "<div>hello</div>"
               coordinates/compose
               coordinates/unify
               coordinates/->nodes))))

  (testing "Create nodes with attributes"
    (is (= [{:node/name     :div
             :node/attrs    {:class     "some test classes"
                             :data-attr "something"
                             :checked   true}
             :node/children [{:node/name  :dompa/text
                              :node/value "hello"}]}]
           (-> "<div class=\"some test classes\" data-attr=\"something\" checked>hello</div>"
               coordinates/compose
               coordinates/unify
               coordinates/->nodes))))

  (testing "Create nested nodes"
    (is (= [{:node/name     :b
             :node/attrs    {}
             :node/children [{:node/name  :dompa/text
                              :node/value "bold"}]}
            {:node/name  :img
             :node/attrs {:src "img.png"}}
            {:node/name  :dompa/text
             :node/value "Hello, "}
            {:node/name     :span
             :node/attrs    {}
             :node/children [{:node/name  :dompa/text
                              :node/value "wor"}
                             {:node/name     :i
                              :node/attrs    {}
                              :node/children [{:node/name  :dompa/text
                                               :node/value "l"}
                                              {:node/name     :b
                                               :node/attrs    {}
                                               :node/children [{:node/name  :dompa/text
                                                                :node/value "d"}]}]}]}]
           (-> "<b>bold</b><img src=\"img.png\" />Hello, <span>wor<i>l<b>d</b></i></span>"
               coordinates/compose
               coordinates/unify
               coordinates/->nodes)))))