-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathtest_module.py
234 lines (161 loc) · 6.82 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
222
223
224
225
226
227
228
229
230
231
232
233
234
from pathlib import Path
import pytest
from sanic import Sanic
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
import idom
from idom.server.sanic import PerClientStateServer
from idom.testing import ServerMountPoint, assert_idom_did_not_log, assert_idom_logged
from idom.web.module import NAME_SOURCE, WebModule
JS_FIXTURES_DIR = Path(__file__).parent / "js_fixtures"
def test_that_js_module_unmount_is_called(driver, display):
SomeComponent = idom.web.export(
idom.web.module_from_file(
"set-flag-when-unmount-is-called",
JS_FIXTURES_DIR / "set-flag-when-unmount-is-called.js",
),
"SomeComponent",
)
set_current_component = idom.Ref(None)
@idom.component
def ShowCurrentComponent():
current_component, set_current_component.current = idom.hooks.use_state(
lambda: SomeComponent({"id": "some-component", "text": "initial component"})
)
return current_component
display(ShowCurrentComponent)
driver.find_element("id", "some-component")
set_current_component.current(
idom.html.h1({"id": "some-other-component"}, "some other component")
)
# the new component has been displayed
driver.find_element("id", "some-other-component")
# the unmount callback for the old component was called
driver.find_element("id", "unmount-flag")
def test_module_from_url(driver):
app = Sanic(__name__)
# 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 = idom.web.export(
idom.web.module_from_url("/simple-button.js", resolve_exports=False),
"SimpleButton",
)
@idom.component
def ShowSimpleButton():
return SimpleButton({"id": "my-button"})
with ServerMountPoint(PerClientStateServer, app=app) as mount_point:
mount_point.mount(ShowSimpleButton)
driver.get(mount_point.url())
driver.find_element("id", "my-button")
def test_module_from_template_where_template_does_not_exist():
with pytest.raises(ValueError, match="No template for 'does-not-exist.js'"):
idom.web.module_from_template("does-not-exist", "something.js")
def test_module_from_template(driver, display):
victory = idom.web.module_from_template("react", "[email protected]")
VictoryBar = idom.web.export(victory, "VictoryBar")
display(VictoryBar)
wait = WebDriverWait(driver, 10)
wait.until(
expected_conditions.visibility_of_element_located(
(By.CLASS_NAME, "VictoryContainer")
)
)
def test_module_from_file(driver, driver_wait, display):
SimpleButton = idom.web.export(
idom.web.module_from_file(
"simple-button", JS_FIXTURES_DIR / "simple-button.js"
),
"SimpleButton",
)
is_clicked = idom.Ref(False)
@idom.component
def ShowSimpleButton():
return SimpleButton(
{"id": "my-button", "onClick": lambda event: is_clicked.set_current(True)}
)
display(ShowSimpleButton)
button = driver.find_element("id", "my-button")
button.click()
driver_wait.until(lambda d: is_clicked.current)
def test_module_from_file_source_conflict(tmp_path):
first_file = tmp_path / "first.js"
with pytest.raises(FileNotFoundError, match="does not exist"):
idom.web.module_from_file("temp", first_file)
first_file.touch()
idom.web.module_from_file("temp", first_file)
second_file = tmp_path / "second.js"
second_file.touch()
# ok, same content
idom.web.module_from_file("temp", second_file)
third_file = tmp_path / "third.js"
third_file.write_text("something-different")
with assert_idom_logged(r"Existing web module .* will be replaced with"):
idom.web.module_from_file("temp", third_file)
def test_web_module_from_file_symlink(tmp_path):
file = tmp_path / "temp.js"
file.touch()
module = idom.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()
idom.web.module_from_file("temp", file_1, symlink=True)
with assert_idom_did_not_log(r"Existing web module .* will be replaced with"):
idom.web.module_from_file("temp", file_1, symlink=True)
file_2 = tmp_path / "temp_2.js"
file_2.write_text("something")
with assert_idom_logged(r"Existing web module .* will be replaced with"):
idom.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()
idom.web.module_from_file("temp", file1)
file2 = tmp_path / "temp2.js"
file2.write_text("something")
with assert_idom_logged(r"Existing web module .* will be replaced with"):
idom.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'"):
idom.web.export(module, "x")
with pytest.raises(ValueError, match=r"does not export \['x', 'y'\]"):
idom.web.export(module, ["x", "y"])
def test_module_exports_multiple_components(driver, display):
Header1, Header2 = idom.web.export(
idom.web.module_from_file(
"exports-two-components", JS_FIXTURES_DIR / "exports-two-components.js"
),
["Header1", "Header2"],
)
display(lambda: Header1({"id": "my-h1"}, "My Header 1"))
driver.find_element("id", "my-h1")
display(lambda: Header2({"id": "my-h2"}, "My Header 2"))
driver.find_element("id", "my-h2")
def test_imported_components_can_render_children(driver, display):
module = idom.web.module_from_file(
"component-can-have-child", JS_FIXTURES_DIR / "component-can-have-child.js"
)
Parent, Child = idom.web.export(module, ["Parent", "Child"])
display(
lambda: Parent(
Child({"index": 1}),
Child({"index": 2}),
Child({"index": 3}),
)
)
parent = driver.find_element("id", "the-parent")
children = parent.find_elements("tag name", "li")
assert len(children) == 3
for index, child in enumerate(children):
assert child.get_attribute("id") == f"child-{index + 1}"
def test_module_from_string():
idom.web.module_from_string("temp", "old")
with assert_idom_logged(r"Existing web module .* will be replaced with"):
idom.web.module_from_string("temp", "new")