11import pytest
22
33from idom import component , config , html , use_state
4+ from idom .testing import DisplayFixture , poll
45from idom .utils import Ref
56
67
7- def use_toggle ():
8- state , set_state = use_state (True )
8+ def use_toggle (initial = True ):
9+ state , set_state = use_state (initial )
910 return state , lambda : set_state (not state )
1011
1112
@@ -14,20 +15,15 @@ def use_counter(initial_value):
1415 return state , lambda : set_state (state + 1 )
1516
1617
17- def test_script_mount_unmount (driver , driver_wait , display ):
18+ async def test_script_mount_unmount (display : DisplayFixture ):
1819 toggle_is_mounted = Ref ()
1920
2021 @component
2122 def Root ():
2223 is_mounted , toggle_is_mounted .current = use_toggle ()
23- if is_mounted :
24- el = HasScript ()
25- else :
26- el = html .div ()
27-
2824 return html .div (
2925 html .div ({"id" : "mount-state" , "data-value" : False }),
30- el ,
26+ HasScript () if is_mounted else html . div () ,
3127 )
3228
3329 @component
@@ -43,22 +39,23 @@ def HasScript():
4339 }"""
4440 )
4541
46- display (Root )
42+ page = await display . show (Root )
4743
48- mount_state = driver .find_element ("id" , "mount-state" )
44+ mount_state = await page .wait_for_selector ("#mount-state" , state = "attached" )
45+ poll_mount_state = poll (mount_state .get_attribute , "data-value" )
4946
50- driver_wait . until ( lambda d : mount_state . get_attribute ( "data-value" ) == "true" )
47+ await poll_mount_state . until_equals ( "true" )
5148
5249 toggle_is_mounted .current ()
5350
54- driver_wait . until ( lambda d : mount_state . get_attribute ( "data-value" ) == "false" )
51+ await poll_mount_state . until_equals ( "false" )
5552
5653 toggle_is_mounted .current ()
5754
58- driver_wait . until ( lambda d : mount_state . get_attribute ( "data-value" ) == "true" )
55+ await poll_mount_state . until_equals ( "true" )
5956
6057
61- def test_script_re_run_on_content_change (driver , driver_wait , display ):
58+ async def test_script_re_run_on_content_change (display : DisplayFixture ):
6259 incr_count = Ref ()
6360
6461 @component
@@ -77,26 +74,29 @@ def HasScript():
7774 ),
7875 )
7976
80- display (HasScript )
77+ page = await display . show (HasScript )
8178
82- mount_count = driver . find_element ( "id " , "mount-count " )
83- unmount_count = driver . find_element ( "id" , "unmount-count " )
79+ mount_count = await page . wait_for_selector ( "#mount-count " , state = "attached " )
80+ poll_mount_count = poll ( mount_count . get_attribute , "data-value " )
8481
85- driver_wait .until (lambda d : mount_count .get_attribute ("data-value" ) == "1" )
86- driver_wait .until (lambda d : unmount_count .get_attribute ("data-value" ) == "0" )
82+ unmount_count = await page .wait_for_selector ("#unmount-count" , state = "attached" )
83+ poll_unmount_count = poll (unmount_count .get_attribute , "data-value" )
84+
85+ await poll_mount_count .until_equals ("1" )
86+ await poll_unmount_count .until_equals ("0" )
8787
8888 incr_count .current ()
8989
90- driver_wait . until ( lambda d : mount_count . get_attribute ( "data-value" ) == "2" )
91- driver_wait . until ( lambda d : unmount_count . get_attribute ( "data-value" ) == "1" )
90+ await poll_mount_count . until_equals ( "2" )
91+ await poll_unmount_count . until_equals ( "1" )
9292
9393 incr_count .current ()
9494
95- driver_wait . until ( lambda d : mount_count . get_attribute ( "data-value" ) == "3" )
96- driver_wait . until ( lambda d : unmount_count . get_attribute ( "data-value" ) == "2" )
95+ await poll_mount_count . until_equals ( "3" )
96+ await poll_unmount_count . until_equals ( "2" )
9797
9898
99- def test_script_from_src (driver , driver_wait , display ):
99+ async def test_script_from_src (display : DisplayFixture ):
100100 incr_src_id = Ref ()
101101 file_name_template = "__some_js_script_{src_id}__.js"
102102
@@ -114,7 +114,7 @@ def HasScript():
114114 ),
115115 )
116116
117- display (HasScript )
117+ page = await display . show (HasScript )
118118
119119 for i in range (1 , 4 ):
120120 script_file = config .IDOM_WEB_MODULES_DIR .current / file_name_template .format (
@@ -129,9 +129,9 @@ def HasScript():
129129
130130 incr_src_id .current ()
131131
132- run_count = driver . find_element ( "id " , "run-count " )
133-
134- driver_wait . until ( lambda d : ( run_count . get_attribute ( "data-value" ) == "1" ) )
132+ run_count = await page . wait_for_selector ( "#run-count " , state = "attached " )
133+ poll_run_count = poll ( run_count . get_attribute , "data-value" )
134+ await poll_run_count . until_equals ( "1" )
135135
136136
137137def test_script_may_only_have_one_child ():
0 commit comments