1
1
import os
2
2
from time import sleep
3
3
4
+ from dash .testing .wait import until
4
5
import dash_html_components as html
5
6
import dash
6
7
from dash .dependencies import Input , Output
13
14
}
14
15
"""
15
16
17
+ GOUDA = """
18
+ window.cheese = 'gouda';
19
+ """
20
+
21
+
22
+ def replace_file (filename , new_content ):
23
+ path = os .path .join (
24
+ os .path .dirname (__file__ ), "hr_assets" , filename
25
+ )
26
+ with open (path , "r+" ) as fp :
27
+ sleep (1 ) # ensure a new mod time
28
+ old_content = fp .read ()
29
+ fp .truncate (0 )
30
+ fp .seek (0 )
31
+ fp .write (new_content )
32
+
33
+ return path , old_content
34
+
16
35
17
36
def test_dvhr001_hot_reload (dash_duo ):
18
37
app = dash .Dash (__name__ , assets_folder = "hr_assets" )
@@ -42,15 +61,12 @@ def new_text(n):
42
61
"#hot-reload-content" , "background-color" , "rgba(0, 0, 255, 1)"
43
62
)
44
63
45
- hot_reload_file = os .path .join (
46
- os .path .dirname (__file__ ), "hr_assets" , "hot_reload.css"
47
- )
48
- with open (hot_reload_file , "r+" ) as fp :
49
- sleep (1 ) # ensure a new mod time
50
- old_content = fp .read ()
51
- fp .truncate (0 )
52
- fp .seek (0 )
53
- fp .write (RED_BG )
64
+ # set a global var - if we soft reload it should still be there,
65
+ # hard reload will delete it
66
+ dash_duo .driver .execute_script ("window.someVar = 42;" )
67
+ assert dash_duo .driver .execute_script ("return window.someVar" ) == 42
68
+
69
+ soft_reload_file , old_soft = replace_file ("hot_reload.css" , RED_BG )
54
70
55
71
try :
56
72
# red is live changed during the test execution
@@ -59,13 +75,38 @@ def new_text(n):
59
75
)
60
76
finally :
61
77
sleep (1 ) # ensure a new mod time
62
- with open (hot_reload_file , "w" ) as f :
63
- f .write (old_content )
78
+ with open (soft_reload_file , "w" ) as f :
79
+ f .write (old_soft )
64
80
65
81
dash_duo .wait_for_style_to_equal (
66
82
"#hot-reload-content" , "background-color" , "rgba(0, 0, 255, 1)"
67
83
)
68
84
85
+ # only soft reload, someVar is still there
86
+ assert dash_duo .driver .execute_script ("return window.someVar" ) == 42
87
+
88
+ assert dash_duo .driver .execute_script ("return window.cheese" ) == "roquefort"
89
+
90
+ hard_reload_file , old_hard = replace_file ("hot_reload.js" , GOUDA )
91
+
92
+ try :
93
+ until (
94
+ lambda : dash_duo .driver .execute_script ("return window.cheese" ) == "gouda" ,
95
+ timeout = 3
96
+ )
97
+ finally :
98
+ sleep (1 ) # ensure a new mod time
99
+ with open (hard_reload_file , "w" ) as f :
100
+ f .write (old_hard )
101
+
102
+ until (
103
+ lambda : dash_duo .driver .execute_script ("return window.cheese" ) == "roquefort" ,
104
+ timeout = 3
105
+ )
106
+
107
+ # we've done a hard reload so someVar is gone
108
+ assert dash_duo .driver .execute_script ("return window.someVar" ) is None
109
+
69
110
# Now check the server status indicator functionality
70
111
71
112
dash_duo .find_element (".dash-debug-menu" ).click ()
0 commit comments