-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathtest_vdom.py
344 lines (307 loc) · 10 KB
/
test_vdom.py
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import sys
import pytest
from fastjsonschema import JsonSchemaException
import idom
from idom.config import IDOM_DEBUG_MODE
from idom.core.events import EventHandler
from idom.core.types import VdomDict
from idom.core.vdom import is_vdom, make_vdom_constructor, validate_vdom_json
FAKE_EVENT_HANDLER = EventHandler(lambda data: None)
FAKE_EVENT_HANDLER_DICT = {"onEvent": FAKE_EVENT_HANDLER}
@pytest.mark.parametrize(
"result, value",
[
(False, {}),
(False, {"tagName": None}),
(False, VdomDict()),
(True, {"tagName": ""}),
(True, VdomDict(tagName="")),
],
)
def test_is_vdom(result, value):
assert is_vdom(value) == result
@pytest.mark.parametrize(
"actual, expected",
[
(
idom.vdom("div", [idom.vdom("div")]),
{"tagName": "div", "children": [{"tagName": "div"}]},
),
(
idom.vdom("div", {"style": {"backgroundColor": "red"}}),
{"tagName": "div", "attributes": {"style": {"backgroundColor": "red"}}},
),
(
# multiple iterables of children are merged
idom.vdom("div", [idom.vdom("div"), 1], (idom.vdom("div"), 2)),
{
"tagName": "div",
"children": [{"tagName": "div"}, 1, {"tagName": "div"}, 2],
},
),
(
idom.vdom("div", event_handlers=FAKE_EVENT_HANDLER_DICT),
{"tagName": "div", "eventHandlers": FAKE_EVENT_HANDLER_DICT},
),
(
idom.vdom("div", {"onEvent": FAKE_EVENT_HANDLER}),
{"tagName": "div", "eventHandlers": FAKE_EVENT_HANDLER_DICT},
),
(
idom.vdom("div", idom.html.h1("hello"), idom.html.h2("world")),
{
"tagName": "div",
"children": [
{"tagName": "h1", "children": ["hello"]},
{"tagName": "h2", "children": ["world"]},
],
},
),
(
idom.vdom("div", {"tagName": "div"}),
{"tagName": "div", "children": [{"tagName": "div"}]},
),
(
idom.vdom("div", (i for i in range(3))),
{"tagName": "div", "children": [0, 1, 2]},
),
(
idom.vdom("div", map(lambda x: x**2, [1, 2, 3])),
{"tagName": "div", "children": [1, 4, 9]},
),
(
idom.vdom(
"MyComponent",
import_source={"source": "./some-script.js", "fallback": "loading..."},
),
{
"tagName": "MyComponent",
"importSource": {
"source": "./some-script.js",
"fallback": "loading...",
},
},
),
],
)
def test_simple_node_construction(actual, expected):
assert actual == expected
async def test_callable_attributes_are_cast_to_event_handlers():
params_from_calls = []
node = idom.vdom("div", {"onEvent": lambda *args: params_from_calls.append(args)})
event_handlers = node.pop("eventHandlers")
assert node == {"tagName": "div"}
handler = event_handlers["onEvent"]
assert event_handlers == {"onEvent": EventHandler(handler.function)}
await handler.function([1, 2])
await handler.function([3, 4, 5])
assert params_from_calls == [(1, 2), (3, 4, 5)]
async def test_event_handlers_and_callable_attributes_are_automatically_merged():
calls = []
node = idom.vdom(
"div",
{"onEvent": lambda: calls.append("callable_attr")},
event_handlers={
"onEvent": EventHandler(lambda data: calls.append("normal_event_handler"))
},
)
event_handlers = node.pop("eventHandlers")
assert node == {"tagName": "div"}
handler = event_handlers["onEvent"]
assert event_handlers == {"onEvent": EventHandler(handler.function)}
await handler.function([])
assert calls == ["normal_event_handler", "callable_attr"]
def test_make_vdom_constructor():
elmt = make_vdom_constructor("some-tag")
assert elmt({"data": 1}, [elmt()]) == {
"tagName": "some-tag",
"children": [{"tagName": "some-tag"}],
"attributes": {"data": 1},
}
no_children = make_vdom_constructor("no-children", allow_children=False)
with pytest.raises(TypeError, match="cannot have children"):
no_children([1, 2, 3])
assert no_children() == {"tagName": "no-children"}
@pytest.mark.parametrize(
"value",
[
{
"tagName": "div",
"children": [
"Some text",
{"tagName": "div"},
],
},
{
"tagName": "div",
"attributes": {"style": {"color": "blue"}},
},
{
"tagName": "div",
"eventHandler": {"target": "something"},
},
{
"tagName": "div",
"eventHandler": {
"target": "something",
"preventDefault": False,
"stopPropogation": True,
},
},
{
"tagName": "div",
"importSource": {"source": "something"},
},
{
"tagName": "div",
"importSource": {"source": "something", "fallback": None},
},
{
"tagName": "div",
"importSource": {"source": "something", "fallback": "loading..."},
},
{
"tagName": "div",
"importSource": {"source": "something", "fallback": {"tagName": "div"}},
},
{
"tagName": "div",
"children": [
"Some text",
{"tagName": "div"},
],
"attributes": {"style": {"color": "blue"}},
"eventHandler": {
"target": "something",
"preventDefault": False,
"stopPropogation": True,
},
"importSource": {
"source": "something",
"fallback": {"tagName": "div"},
},
},
],
)
def test_valid_vdom(value):
validate_vdom_json(value)
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="error messages are different in Python<3.10"
)
@pytest.mark.parametrize(
"value, error_message_pattern",
[
(
None,
r"data must be object",
),
(
{},
r"data must contain \['tagName'\] properties",
),
(
{"tagName": 0},
r"data\.tagName must be string",
),
(
{"tagName": "tag", "children": None},
r"data\.children must be array",
),
(
{"tagName": "tag", "children": [None]},
r"data\.children\[{data_x}\] must be object or string",
),
(
{"tagName": "tag", "children": [{"tagName": None}]},
r"data\.children\[{data_x}\]\.tagName must be string",
),
(
{"tagName": "tag", "attributes": None},
r"data\.attributes must be object",
),
(
{"tagName": "tag", "eventHandlers": None},
r"data\.eventHandlers must be object",
),
(
{"tagName": "tag", "eventHandlers": {"onEvent": None}},
r"data\.eventHandlers\.{data_key} must be object",
),
(
{
"tagName": "tag",
"eventHandlers": {"onEvent": {}},
},
r"data\.eventHandlers\.{data_key}\ must contain \['target'\] properties",
),
(
{
"tagName": "tag",
"eventHandlers": {
"onEvent": {
"target": "something",
"preventDefault": None,
}
},
},
r"data\.eventHandlers\.{data_key}\.preventDefault must be boolean",
),
(
{
"tagName": "tag",
"eventHandlers": {
"onEvent": {
"target": "something",
"stopPropagation": None,
}
},
},
r"data\.eventHandlers\.{data_key}\.stopPropagation must be boolean",
),
(
{"tagName": "tag", "importSource": None},
r"data\.importSource must be object",
),
(
{"tagName": "tag", "importSource": {}},
r"data\.importSource must contain \['source'\] properties",
),
(
{
"tagName": "tag",
"importSource": {"source": "something", "fallback": 0},
},
r"data\.importSource\.fallback must be object or string or null",
),
(
{
"tagName": "tag",
"importSource": {"source": "something", "fallback": {"tagName": None}},
},
r"data\.importSource\.fallback\.tagName must be string",
),
],
)
def test_invalid_vdom(value, error_message_pattern):
with pytest.raises(JsonSchemaException, match=error_message_pattern):
validate_vdom_json(value)
@pytest.mark.skipif(not IDOM_DEBUG_MODE.current, reason="Only logs in debug mode")
def test_debug_log_cannot_verify_keypath_for_genereators(caplog):
idom.vdom("div", (1 for i in range(10)))
assert len(caplog.records) == 1
assert caplog.records[0].message.startswith(
"Did not verify key-path integrity of children in generator"
)
caplog.records.clear()
@pytest.mark.skipif(not IDOM_DEBUG_MODE.current, reason="Only logs in debug mode")
def test_debug_log_dynamic_children_must_have_keys(caplog):
idom.vdom("div", [idom.vdom("div")])
assert len(caplog.records) == 1
assert caplog.records[0].message.startswith("Key not specified for child")
caplog.records.clear()
@idom.component
def MyComponent():
return idom.vdom("div")
idom.vdom("div", [MyComponent()])
assert len(caplog.records) == 1
assert caplog.records[0].message.startswith("Key not specified for child")