1
1
from pathlib import Path
2
+ from sys import implementation
2
3
3
4
import pytest
4
5
from sanic import Sanic
7
8
from selenium .webdriver .support .ui import WebDriverWait
8
9
9
10
import idom
10
- from idom .server .sanic import PerClientStateServer
11
- from idom .testing import ServerFixture , assert_idom_did_not_log , assert_idom_logged
11
+ from idom .server import sanic as sanic_implementation
12
+ from idom .testing import (
13
+ DisplayFixture ,
14
+ ServerFixture ,
15
+ assert_idom_did_not_log ,
16
+ assert_idom_logged ,
17
+ poll ,
18
+ )
12
19
from idom .web .module import NAME_SOURCE , WebModule
13
20
14
21
15
22
JS_FIXTURES_DIR = Path (__file__ ).parent / "js_fixtures"
16
23
17
24
18
- def test_that_js_module_unmount_is_called (driver , display ):
25
+ async def test_that_js_module_unmount_is_called (display : DisplayFixture ):
19
26
SomeComponent = idom .web .export (
20
27
idom .web .module_from_file (
21
28
"set-flag-when-unmount-is-called" ,
@@ -33,23 +40,23 @@ def ShowCurrentComponent():
33
40
)
34
41
return current_component
35
42
36
- display (ShowCurrentComponent )
43
+ page = await display . show (ShowCurrentComponent )
37
44
38
- driver . find_element ( "id " , "some-component " )
45
+ await page . wait_for_selector ( "#some-component " , state = "attached " )
39
46
40
47
set_current_component .current (
41
48
idom .html .h1 ({"id" : "some-other-component" }, "some other component" )
42
49
)
43
50
44
51
# the new component has been displayed
45
- driver . find_element ( "id" , " some-other-component" )
52
+ await page . wait_for_selector ( "# some-other-component" , state = "attached " )
46
53
47
54
# the unmount callback for the old component was called
48
- driver . find_element ( "id " , "unmount-flag " )
55
+ await page . wait_for_selector ( "#unmount-flag " , state = "attached " )
49
56
50
57
51
- def test_module_from_url (driver ):
52
- app = Sanic (__name__ )
58
+ async def test_module_from_url (browser ):
59
+ app = Sanic ("test_module_from_url" )
53
60
54
61
# instead of directing the URL to a CDN, we just point it to this static file
55
62
app .static (
@@ -67,30 +74,25 @@ def test_module_from_url(driver):
67
74
def ShowSimpleButton ():
68
75
return SimpleButton ({"id" : "my-button" })
69
76
70
- with ServerFixture (PerClientStateServer , app = app ) as mount_point :
71
- mount_point . mount ( ShowSimpleButton )
72
- driver . get ( mount_point . url () )
73
- driver . find_element ( "id" , " my-button" )
77
+ async with ServerFixture (app = app , implementation = sanic_implementation ) as server :
78
+ async with DisplayFixture ( server , browser ) as display :
79
+ page = await display . show ( ShowSimpleButton )
80
+ await page . wait_for_selector ( "# my-button" )
74
81
75
82
76
83
def test_module_from_template_where_template_does_not_exist ():
77
84
with pytest .raises (ValueError , match = "No template for 'does-not-exist.js'" ):
78
85
idom .web .module_from_template ("does-not-exist" , "something.js" )
79
86
80
87
81
- def test_module_from_template (driver , display ):
88
+ async def test_module_from_template (display : DisplayFixture ):
82
89
victory = idom .
web .
module_from_template (
"react" ,
"[email protected] " )
83
90
VictoryBar = idom .web .export (victory , "VictoryBar" )
84
- display (VictoryBar )
85
- wait = WebDriverWait (driver , 10 )
86
- wait .until (
87
- expected_conditions .visibility_of_element_located (
88
- (By .CLASS_NAME , "VictoryContainer" )
89
- )
90
- )
91
+ page = await display .show (VictoryBar )
92
+ await page .wait_for_selector (".VictoryContainer" )
91
93
92
94
93
- def test_module_from_file (driver , driver_wait , display ):
95
+ async def test_module_from_file (display : DisplayFixture ):
94
96
SimpleButton = idom .web .export (
95
97
idom .web .module_from_file (
96
98
"simple-button" , JS_FIXTURES_DIR / "simple-button.js"
@@ -106,11 +108,11 @@ def ShowSimpleButton():
106
108
{"id" : "my-button" , "onClick" : lambda event : is_clicked .set_current (True )}
107
109
)
108
110
109
- display (ShowSimpleButton )
111
+ page = await display . show (ShowSimpleButton )
110
112
111
- button = driver . find_element ( "id" , " my-button" )
112
- button .click ()
113
- driver_wait . until (lambda d : is_clicked .current )
113
+ button = await page . wait_for_selector ( "# my-button" )
114
+ await button .click ()
115
+ poll (lambda : is_clicked .current ). until_is ( True )
114
116
115
117
116
118
def test_module_from_file_source_conflict (tmp_path ):
@@ -188,44 +190,44 @@ def test_module_missing_exports():
188
190
idom .web .export (module , ["x" , "y" ])
189
191
190
192
191
- def test_module_exports_multiple_components (driver , display ):
193
+ async def test_module_exports_multiple_components (display : DisplayFixture ):
192
194
Header1 , Header2 = idom .web .export (
193
195
idom .web .module_from_file (
194
196
"exports-two-components" , JS_FIXTURES_DIR / "exports-two-components.js"
195
197
),
196
198
["Header1" , "Header2" ],
197
199
)
198
200
199
- display (lambda : Header1 ({"id" : "my-h1" }, "My Header 1" ))
201
+ page = await display . show (lambda : Header1 ({"id" : "my-h1" }, "My Header 1" ))
200
202
201
- driver . find_element ( "id " , "my-h1 " )
203
+ await page . wait_for_selector ( "#my-h1 " , state = "attached " )
202
204
203
- display (lambda : Header2 ({"id" : "my-h2" }, "My Header 2" ))
205
+ page = await display . show (lambda : Header2 ({"id" : "my-h2" }, "My Header 2" ))
204
206
205
- driver . find_element ( "id " , "my-h2 " )
207
+ await page . wait_for_selector ( "#my-h2 " , state = "attached " )
206
208
207
209
208
- def test_imported_components_can_render_children (driver , display ):
210
+ async def test_imported_components_can_render_children (display : DisplayFixture ):
209
211
module = idom .web .module_from_file (
210
212
"component-can-have-child" , JS_FIXTURES_DIR / "component-can-have-child.js"
211
213
)
212
214
Parent , Child = idom .web .export (module , ["Parent" , "Child" ])
213
215
214
- display (
216
+ page = await display . show (
215
217
lambda : Parent (
216
218
Child ({"index" : 1 }),
217
219
Child ({"index" : 2 }),
218
220
Child ({"index" : 3 }),
219
221
)
220
222
)
221
223
222
- parent = driver . find_element ( "id " , "the-parent " )
223
- children = parent .find_elements ( "tag name" , "li" )
224
+ parent = await page . wait_for_selector ( "#the-parent " , state = "attached " )
225
+ children = await parent .query_selector_all ( "li" )
224
226
225
227
assert len (children ) == 3
226
228
227
229
for index , child in enumerate (children ):
228
- assert child .get_attribute ("id" ) == f"child-{ index + 1 } "
230
+ assert ( await child .get_attribute ("id" ) ) == f"child-{ index + 1 } "
229
231
230
232
231
233
def test_module_from_string ():
0 commit comments