forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_module.py
221 lines (152 loc) · 6.66 KB
/
test_module.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
from pathlib import Path
import pytest
from sanic import Sanic
import reactpy
from reactpy.backend import sanic as sanic_implementation
from reactpy.testing import (
BackendFixture,
DisplayFixture,
assert_reactpy_did_log,
assert_reactpy_did_not_log,
poll,
)
from reactpy.web.module import NAME_SOURCE, WebModule
JS_FIXTURES_DIR = Path(__file__).parent / "js_fixtures"
async def test_that_js_module_unmount_is_called(display: DisplayFixture):
SomeComponent = reactpy.web.export(
reactpy.web.module_from_file(
"set-flag-when-unmount-is-called",
JS_FIXTURES_DIR / "set-flag-when-unmount-is-called.js",
),
"SomeComponent",
)
set_current_component = reactpy.Ref(None)
@reactpy.component
def ShowCurrentComponent():
current_component, set_current_component.current = reactpy.hooks.use_state(
lambda: SomeComponent({"id": "some-component", "text": "initial component"})
)
return current_component
await display.show(ShowCurrentComponent)
await display.page.wait_for_selector("#some-component", state="attached")
set_current_component.current(
reactpy.html.h1({"id": "some-other-component"}, "some other component")
)
# the new component has been displayed
await display.page.wait_for_selector("#some-other-component", state="attached")
# the unmount callback for the old component was called
await display.page.wait_for_selector("#unmount-flag", state="attached")
@pytest.mark.flaky(reruns=3)
async def test_module_from_url(browser):
app = Sanic("test_module_from_url")
# instead of directing the URL to a CDN, we just point it to this static file
app.static(
"/simple-button.js",
str(JS_FIXTURES_DIR / "simple-button.js"),
content_type="text/javascript",
)
SimpleButton = reactpy.web.export(
reactpy.web.module_from_url("/simple-button.js", resolve_exports=False),
"SimpleButton",
)
@reactpy.component
def ShowSimpleButton():
return SimpleButton({"id": "my-button"})
async with BackendFixture(app=app, implementation=sanic_implementation) as server:
async with DisplayFixture(server, browser) as display:
await display.show(ShowSimpleButton)
await display.page.wait_for_selector("#my-button")
async def test_module_from_file(display: DisplayFixture):
SimpleButton = reactpy.web.export(
reactpy.web.module_from_file(
"simple-button", JS_FIXTURES_DIR / "simple-button.js"
),
"SimpleButton",
)
is_clicked = reactpy.Ref(False)
@reactpy.component
def ShowSimpleButton():
return SimpleButton(
{"id": "my-button", "onClick": lambda event: is_clicked.set_current(True)}
)
await display.show(ShowSimpleButton)
button = await display.page.wait_for_selector("#my-button")
await button.click()
await poll(lambda: is_clicked.current).until_is(True)
def test_module_from_file_source_conflict(tmp_path):
first_file = tmp_path / "first.js"
with pytest.raises(FileNotFoundError, match="does not exist"):
reactpy.web.module_from_file("temp", first_file)
first_file.touch()
reactpy.web.module_from_file("temp", first_file)
second_file = tmp_path / "second.js"
second_file.touch()
# ok, same content
reactpy.web.module_from_file("temp", second_file)
third_file = tmp_path / "third.js"
third_file.write_text("something-different")
with assert_reactpy_did_log(r"Existing web module .* will be replaced with"):
reactpy.web.module_from_file("temp", third_file)
def test_web_module_from_file_symlink(tmp_path):
file = tmp_path / "temp.js"
file.touch()
module = reactpy.web.module_from_file("temp", file, symlink=True)
assert module.file.resolve().read_text() == ""
file.write_text("hello world!")
assert module.file.resolve().read_text() == "hello world!"
def test_web_module_from_file_symlink_twice(tmp_path):
file_1 = tmp_path / "temp_1.js"
file_1.touch()
reactpy.web.module_from_file("temp", file_1, symlink=True)
with assert_reactpy_did_not_log(r"Existing web module .* will be replaced with"):
reactpy.web.module_from_file("temp", file_1, symlink=True)
file_2 = tmp_path / "temp_2.js"
file_2.write_text("something")
with assert_reactpy_did_log(r"Existing web module .* will be replaced with"):
reactpy.web.module_from_file("temp", file_2, symlink=True)
def test_web_module_from_file_replace_existing(tmp_path):
file1 = tmp_path / "temp1.js"
file1.touch()
reactpy.web.module_from_file("temp", file1)
file2 = tmp_path / "temp2.js"
file2.write_text("something")
with assert_reactpy_did_log(r"Existing web module .* will be replaced with"):
reactpy.web.module_from_file("temp", file2)
def test_module_missing_exports():
module = WebModule("test", NAME_SOURCE, None, {"a", "b", "c"}, None, False)
with pytest.raises(ValueError, match="does not export 'x'"):
reactpy.web.export(module, "x")
with pytest.raises(ValueError, match=r"does not export \['x', 'y'\]"):
reactpy.web.export(module, ["x", "y"])
async def test_module_exports_multiple_components(display: DisplayFixture):
Header1, Header2 = reactpy.web.export(
reactpy.web.module_from_file(
"exports-two-components", JS_FIXTURES_DIR / "exports-two-components.js"
),
["Header1", "Header2"],
)
await display.show(lambda: Header1({"id": "my-h1"}, "My Header 1"))
await display.page.wait_for_selector("#my-h1", state="attached")
await display.show(lambda: Header2({"id": "my-h2"}, "My Header 2"))
await display.page.wait_for_selector("#my-h2", state="attached")
async def test_imported_components_can_render_children(display: DisplayFixture):
module = reactpy.web.module_from_file(
"component-can-have-child", JS_FIXTURES_DIR / "component-can-have-child.js"
)
Parent, Child = reactpy.web.export(module, ["Parent", "Child"])
await display.show(
lambda: Parent(
Child({"index": 1}),
Child({"index": 2}),
Child({"index": 3}),
)
)
parent = await display.page.wait_for_selector("#the-parent", state="attached")
children = await parent.query_selector_all("li")
assert len(children) == 3
for index, child in enumerate(children):
assert (await child.get_attribute("id")) == f"child-{index + 1}"
def test_module_from_string():
reactpy.web.module_from_string("temp", "old")
with assert_reactpy_did_log(r"Existing web module .* will be replaced with"):
reactpy.web.module_from_string("temp", "new")