1- from idom import component , html , use_state
1+ import pytest
2+
3+ from idom import component , config , html , use_state
24from idom .utils import Ref
35
46
@@ -7,8 +9,8 @@ def use_toggle():
79 return state , lambda : set_state (not state )
810
911
10- def use_counter ():
11- state , set_state = use_state (1 )
12+ def use_counter (initial_value ):
13+ state , set_state = use_state (initial_value )
1214 return state , lambda : set_state (state + 1 )
1315
1416
@@ -61,7 +63,7 @@ def test_script_re_run_on_content_change(driver, driver_wait, display):
6163
6264 @component
6365 def HasScript ():
64- count , incr_count .current = use_counter ()
66+ count , incr_count .current = use_counter (1 )
6567 return html .div (
6668 html .div ({"id" : "mount-count" , "data-value" : 0 }),
6769 html .div ({"id" : "unmount-count" , "data-value" : 0 }),
@@ -92,3 +94,51 @@ def HasScript():
9294
9395 driver_wait .until (lambda d : mount_count .get_attribute ("data-value" ) == "3" )
9496 driver_wait .until (lambda d : unmount_count .get_attribute ("data-value" ) == "2" )
97+
98+
99+ def test_script_from_src (driver , driver_wait , display ):
100+ incr_src_id = Ref ()
101+ file_name_template = "__some_js_script_{src_id}__.js"
102+
103+ @component
104+ def HasScript ():
105+ src_id , incr_src_id .current = use_counter (0 )
106+ if src_id == 0 :
107+ # on initial display we haven't added the file yet.
108+ return html .div ()
109+ else :
110+ return html .div (
111+ html .div ({"id" : "run-count" , "data-value" : 0 }),
112+ html .script (
113+ {"src" : f"/modules/{ file_name_template .format (src_id = src_id )} " }
114+ ),
115+ )
116+
117+ display (HasScript )
118+
119+ for i in range (1 , 4 ):
120+ script_file = config .IDOM_WED_MODULES_DIR .current / file_name_template .format (
121+ src_id = i
122+ )
123+ script_file .write_text (
124+ f"""
125+ let runCountEl = document.getElementById("run-count");
126+ runCountEl.setAttribute("data-value", { i } );
127+ """
128+ )
129+
130+ incr_src_id .current ()
131+
132+ run_count = driver .find_element ("id" , "run-count" )
133+
134+ driver_wait .until (lambda d : (run_count .get_attribute ("data-value" ) == "1" ))
135+
136+
137+ def test_script_may_only_have_one_child ():
138+ with pytest .raises (ValueError , match = "'script' nodes may have, at most, one child" ):
139+ html .script ("one child" , "two child" )
140+
141+
142+ def test_child_of_script_must_be_string ():
143+ with pytest .raises (ValueError , match = "The child of a 'script' must be a string" ):
144+ html .script (1 )
0 commit comments