33from  reactpy  import  component , config , html 
44from  reactpy .testing  import  DisplayFixture , poll 
55from  reactpy .utils  import  Ref 
6- from  tests .tooling .hooks  import  use_counter , use_toggle 
7- 
8- 
9- async  def  test_script_mount_unmount (display : DisplayFixture ):
10-     toggle_is_mounted  =  Ref ()
11- 
12-     @component  
13-     def  Root ():
14-         is_mounted , toggle_is_mounted .current  =  use_toggle (True )
15-         return  html .div (
16-             html .div ({"id" : "mount-state" , "data_value" : False }),
17-             HasScript () if  is_mounted  else  html .div (),
18-         )
19- 
20-     @component  
21-     def  HasScript ():
22-         return  html .script (
23-             """() => { 
24-                 const mapping = {"false": false, "true": true}; 
25-                 const mountStateEl = document.getElementById("mount-state"); 
26-                 mountStateEl.setAttribute( 
27-                     "data-value", !mapping[mountStateEl.getAttribute("data-value")]); 
28-                 return () => mountStateEl.setAttribute( 
29-                     "data-value", !mapping[mountStateEl.getAttribute("data-value")]); 
30-             }""" 
31-         )
32- 
33-     await  display .show (Root )
34- 
35-     mount_state  =  await  display .page .wait_for_selector ("#mount-state" , state = "attached" )
36-     poll_mount_state  =  poll (mount_state .get_attribute , "data-value" )
37- 
38-     await  poll_mount_state .until_equals ("true" )
39- 
40-     toggle_is_mounted .current ()
41- 
42-     await  poll_mount_state .until_equals ("false" )
43- 
44-     toggle_is_mounted .current ()
45- 
46-     await  poll_mount_state .until_equals ("true" )
6+ from  tests .tooling .hooks  import  use_counter 
477
488
499async  def  test_script_re_run_on_content_change (display : DisplayFixture ):
@@ -54,14 +14,9 @@ def HasScript():
5414        count , incr_count .current  =  use_counter (1 )
5515        return  html .div (
5616            html .div ({"id" : "mount-count" , "data_value" : 0 }),
57-             html .div ({"id" : "unmount-count" , "data_value" : 0 }),
5817            html .script (
59-                 f"""() => {{ 
60-                     const mountCountEl = document.getElementById("mount-count"); 
61-                     const unmountCountEl = document.getElementById("unmount-count"); 
62-                     mountCountEl.setAttribute("data-value", { count }  
63-                     return () => unmountCountEl.setAttribute("data-value", { count }  
64-                 }}""" 
18+                 'const mountCountEl = document.getElementById("mount-count");' 
19+                 f'mountCountEl.setAttribute("data-value", { count }  
6520            ),
6621        )
6722
@@ -70,23 +25,11 @@ def HasScript():
7025    mount_count  =  await  display .page .wait_for_selector ("#mount-count" , state = "attached" )
7126    poll_mount_count  =  poll (mount_count .get_attribute , "data-value" )
7227
73-     unmount_count  =  await  display .page .wait_for_selector (
74-         "#unmount-count" , state = "attached" 
75-     )
76-     poll_unmount_count  =  poll (unmount_count .get_attribute , "data-value" )
77- 
7828    await  poll_mount_count .until_equals ("1" )
79-     await  poll_unmount_count .until_equals ("0" )
80- 
8129    incr_count .current ()
82- 
8330    await  poll_mount_count .until_equals ("2" )
84-     await  poll_unmount_count .until_equals ("1" )
85- 
8631    incr_count .current ()
87- 
8832    await  poll_mount_count .until_equals ("3" )
89-     await  poll_unmount_count .until_equals ("2" )
9033
9134
9235async  def  test_script_from_src (display : DisplayFixture ):
0 commit comments