forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_events.py
More file actions
410 lines (311 loc) · 11.7 KB
/
test_events.py
File metadata and controls
410 lines (311 loc) · 11.7 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import pytest
import reactpy
from reactpy import component, html
from reactpy.core.events import (
EventHandler,
merge_event_handler_funcs,
merge_event_handlers,
to_event_handler_function,
)
from reactpy.core.layout import Layout
from reactpy.testing import DisplayFixture, poll
from reactpy.types import Event
from tests.tooling.common import DEFAULT_TYPE_DELAY
from .. import pytestmark # noqa: F401
def test_event_handler_repr():
handler = EventHandler(lambda: None)
assert repr(handler) == (
f"EventHandler(function={handler.function}, prevent_default=False, "
f"stop_propagation=False, target={handler.target!r})"
)
def test_event_handler_props():
handler_0 = EventHandler(lambda data: None)
assert handler_0.stop_propagation is False
assert handler_0.prevent_default is False
assert handler_0.target is None
handler_1 = EventHandler(lambda data: None, prevent_default=True)
assert handler_1.stop_propagation is False
assert handler_1.prevent_default is True
assert handler_1.target is None
handler_2 = EventHandler(lambda data: None, stop_propagation=True)
assert handler_2.stop_propagation is True
assert handler_2.prevent_default is False
assert handler_2.target is None
handler_3 = EventHandler(lambda data: None, target="123")
assert handler_3.stop_propagation is False
assert handler_3.prevent_default is False
assert handler_3.target == "123"
def test_event_handler_equivalence():
async def func(data):
return None
assert EventHandler(func) == EventHandler(func)
assert EventHandler(lambda data: None) != EventHandler(lambda data: None)
assert EventHandler(func, stop_propagation=True) != EventHandler(
func, stop_propagation=False
)
assert EventHandler(func, prevent_default=True) != EventHandler(
func, prevent_default=False
)
assert EventHandler(func, target="123") != EventHandler(func, target="456")
async def test_to_event_handler_function():
call_args = reactpy.Ref(None)
async def coro(*args):
call_args.current = args
def func(*args):
call_args.current = args
await to_event_handler_function(coro, positional_args=True)([1, 2, 3])
assert call_args.current == (1, 2, 3)
await to_event_handler_function(func, positional_args=True)([1, 2, 3])
assert call_args.current == (1, 2, 3)
await to_event_handler_function(coro, positional_args=False)([1, 2, 3])
assert call_args.current == ([1, 2, 3],)
await to_event_handler_function(func, positional_args=False)([1, 2, 3])
assert call_args.current == ([1, 2, 3],)
async def test_merge_event_handler_empty_list():
with pytest.raises(ValueError, match=r"No event handlers to merge"):
merge_event_handlers([])
@pytest.mark.parametrize(
"kwargs_1, kwargs_2",
[
({"stop_propagation": True}, {"stop_propagation": False}),
({"prevent_default": True}, {"prevent_default": False}),
({"target": "this"}, {"target": "that"}),
],
)
async def test_merge_event_handlers_raises_on_mismatch(kwargs_1, kwargs_2):
def func(data):
return None
with pytest.raises(ValueError, match=r"Cannot merge handlers"):
merge_event_handlers(
[
EventHandler(func, **kwargs_1),
EventHandler(func, **kwargs_2),
]
)
async def test_merge_event_handlers():
handler = EventHandler(lambda data: None)
assert merge_event_handlers([handler]) is handler
calls = []
merged_handler = merge_event_handlers(
[
EventHandler(lambda data: calls.append("first")),
EventHandler(lambda data: calls.append("second")),
]
)
await merged_handler.function({})
assert calls == ["first", "second"]
def test_merge_event_handler_funcs_empty_list():
with pytest.raises(ValueError, match=r"No event handler functions to merge"):
merge_event_handler_funcs([])
async def test_merge_event_handler_funcs():
calls = []
async def some_func(data):
calls.append("some_func")
async def some_other_func(data):
calls.append("some_other_func")
assert merge_event_handler_funcs([some_func]) is some_func
merged_handler = merge_event_handler_funcs([some_func, some_other_func])
await merged_handler([])
assert calls == ["some_func", "some_other_func"]
async def test_can_prevent_event_default_operation(display: DisplayFixture):
@reactpy.component
def Input():
@reactpy.event(prevent_default=True)
async def on_key_down(value):
pass
return reactpy.html.input({"onKeyDown": on_key_down, "id": "input"})
await display.show(Input)
inp = await display.page.wait_for_selector("#input")
await inp.type("hello", delay=DEFAULT_TYPE_DELAY)
# the default action of updating the element's value did not take place
assert (await inp.evaluate("node => node.value")) == ""
async def test_simple_click_event(display: DisplayFixture):
@reactpy.component
def Button():
clicked, set_clicked = reactpy.hooks.use_state(False)
async def on_click(event):
set_clicked(True)
if not clicked:
return reactpy.html.button(
{"onClick": on_click, "id": "click"}, ["Click Me!"]
)
else:
return reactpy.html.p({"id": "complete"}, ["Complete"])
await display.show(Button)
button = await display.page.wait_for_selector("#click")
await button.click()
await display.page.wait_for_selector("#complete")
async def test_can_stop_event_propagation(display: DisplayFixture):
clicked = reactpy.Ref(False)
@reactpy.component
def DivInDiv():
@reactpy.event(stop_propagation=True)
def inner_click_no_op(event):
clicked.current = True
def outer_click_is_not_triggered(event):
raise AssertionError
outer = reactpy.html.div(
{
"style": {"height": "35px", "width": "35px", "backgroundColor": "red"},
"onClick": outer_click_is_not_triggered,
"id": "outer",
},
reactpy.html.div(
{
"style": {
"height": "30px",
"width": "30px",
"backgroundColor": "blue",
},
"onClick": inner_click_no_op,
"id": "inner",
}
),
)
return outer
await display.show(DivInDiv)
inner = await display.page.wait_for_selector("#inner")
await inner.click()
await poll(lambda: clicked.current).until_is(True)
async def test_javascript_event_as_arrow_function(display: DisplayFixture):
@reactpy.component
def App():
return reactpy.html.div(
reactpy.html.div(
reactpy.html.button(
{
"id": "the-button",
"onClick": '(e) => e.target.innerText = "Thank you!"',
},
"Click Me",
),
reactpy.html.div({"id": "the-parent"}),
)
)
await display.show(lambda: App())
button = await display.page.wait_for_selector("#the-button", state="attached")
assert await button.inner_text() == "Click Me"
await button.click()
assert await button.inner_text() == "Thank you!"
async def test_javascript_event_as_this_statement(display: DisplayFixture):
@reactpy.component
def App():
return reactpy.html.div(
reactpy.html.div(
reactpy.html.button(
{
"id": "the-button",
"onClick": 'this.innerText = "Thank you!"',
},
"Click Me",
),
reactpy.html.div({"id": "the-parent"}),
)
)
await display.show(lambda: App())
button = await display.page.wait_for_selector("#the-button", state="attached")
assert await button.inner_text() == "Click Me"
await button.click()
assert await button.inner_text() == "Thank you!"
async def test_javascript_event_after_state_update(display: DisplayFixture):
@reactpy.component
def App():
click_count, set_click_count = reactpy.hooks.use_state(0)
return reactpy.html.div(
{"id": "the-parent"},
reactpy.html.button(
{
"id": "button-with-reactpy-event",
"onClick": lambda _: set_click_count(click_count + 1),
},
"Click Me",
),
reactpy.html.button(
{
"id": "button-with-javascript-event",
"onClick": """javascript: () => {
let parent = document.getElementById("the-parent");
parent.appendChild(document.createElement("div"));
}""",
},
"No, Click Me",
),
*[reactpy.html.div("Clicked") for _ in range(click_count)],
)
await display.show(lambda: App())
button1 = await display.page.wait_for_selector(
"#button-with-reactpy-event", state="attached"
)
await button1.click()
await button1.click()
await button1.click()
button2 = await display.page.wait_for_selector(
"#button-with-javascript-event", state="attached"
)
await button2.click()
await button2.click()
await button2.click()
parent = await display.page.wait_for_selector("#the-parent", state="attached")
generated_divs = await parent.query_selector_all("div")
assert len(generated_divs) == 6
def test_detect_prevent_default():
def handler(event: Event):
event.preventDefault()
eh = EventHandler(handler)
assert eh.prevent_default is True
def test_detect_stop_propagation():
def handler(event: Event):
event.stopPropagation()
eh = EventHandler(handler)
assert eh.stop_propagation is True
def test_detect_both():
def handler(event: Event):
event.preventDefault()
event.stopPropagation()
eh = EventHandler(handler)
assert eh.prevent_default is True
assert eh.stop_propagation is True
def test_no_detect():
def handler(event: Event):
pass
eh = EventHandler(handler)
assert eh.prevent_default is False
assert eh.stop_propagation is False
def test_event_wrapper():
data = {"a": 1, "b": {"c": 2}}
event = Event(data)
assert event.a == 1
assert event.b.c == 2
assert event["a"] == 1
assert event["b"]["c"] == 2
async def test_vdom_has_prevent_default():
@component
def MyComponent():
def handler(event: Event):
event.preventDefault()
return html.button({"onClick": handler})
async with Layout(MyComponent()) as layout:
await layout.render()
# Check layout._event_handlers
# Find the handler
handler = next(iter(layout._event_handlers.values()))
assert handler.prevent_default is True
def test_event_export():
from reactpy.types import Event
assert Event is not None
def test_detect_false_positive():
def handler(event: Event):
# This should not trigger detection
other = Event()
other.preventDefault()
other.stopPropagation()
eh = EventHandler(handler)
assert eh.prevent_default is False
assert eh.stop_propagation is False
def test_detect_renamed_argument():
def handler(e: Event):
e.preventDefault()
e.stopPropagation()
eh = EventHandler(handler)
assert eh.prevent_default is True
assert eh.stop_propagation is True