1
1
import pytest
2
2
import dash
3
- from dash .dependencies import Input , Output
3
+ from dash .dependencies import Input , Output , State
4
4
import dash_core_components as dcc
5
5
import dash_html_components as html
6
6
7
+ from dash .testing .wait import until
8
+
7
9
8
10
@pytest .mark .DCC774
9
11
def test_loca001_callbacks (dash_dcc ):
@@ -23,3 +25,116 @@ def update_path(path):
23
25
dash_dcc .start_server (app )
24
26
25
27
dash_dcc .wait_for_text_to_equal ("#div" , "/" )
28
+
29
+
30
+ def test_loca002_location_link (dash_dcc ):
31
+ app = dash .Dash (__name__ )
32
+
33
+ app .layout = html .Div (
34
+ [
35
+ html .Div (id = "waitfor" ),
36
+ dcc .Location (id = "test-location" , refresh = False ),
37
+ dcc .Link (
38
+ html .Button ("I am a clickable button" ),
39
+ id = "test-link" ,
40
+ href = "/test/pathname" ,
41
+ ),
42
+ dcc .Link (
43
+ html .Button ("I am a clickable hash button" ),
44
+ id = "test-link-hash" ,
45
+ href = "#test" ,
46
+ ),
47
+ dcc .Link (
48
+ html .Button ("I am a clickable search button" ),
49
+ id = "test-link-search" ,
50
+ href = "?testQuery=testValue" ,
51
+ refresh = False ,
52
+ ),
53
+ html .Button ("I am a magic button that updates pathname" , id = "test-button" ),
54
+ html .A ("link to click" , href = "/test/pathname/a" , id = "test-a" ),
55
+ html .A ("link to click" , href = "#test-hash" , id = "test-a-hash" ),
56
+ html .A ("link to click" , href = "?queryA=valueA" , id = "test-a-query" ),
57
+ html .Div (id = "test-pathname" , children = []),
58
+ html .Div (id = "test-hash" , children = []),
59
+ html .Div (id = "test-search" , children = []),
60
+ ]
61
+ )
62
+
63
+ @app .callback (
64
+ Output ("test-pathname" , "children" ), Input ("test-location" , "pathname" )
65
+ )
66
+ def update_test_pathname (pathname ):
67
+ return pathname
68
+
69
+ @app .callback (Output ("test-hash" , "children" ), Input ("test-location" , "hash" ))
70
+ def update_test_hash (hash_val ):
71
+ return hash_val or ""
72
+
73
+ @app .callback (Output ("test-search" , "children" ), Input ("test-location" , "search" ))
74
+ def update_test_search (search ):
75
+ return search or ""
76
+
77
+ @app .callback (
78
+ Output ("test-location" , "pathname" ),
79
+ Input ("test-button" , "n_clicks" ),
80
+ State ("test-location" , "pathname" ),
81
+ )
82
+ def update_pathname (n_clicks , current_pathname ):
83
+ if n_clicks is not None :
84
+ return "/new/pathname"
85
+
86
+ return current_pathname
87
+
88
+ dash_dcc .start_server (app )
89
+
90
+ dash_dcc .percy_snapshot ("link -- location" )
91
+
92
+ # Check that link updates pathname
93
+ dash_dcc .find_element ("#test-link" ).click ()
94
+ until (
95
+ lambda : dash_dcc .driver .current_url .replace ("http://localhost:8050" , "" )
96
+ == "/test/pathname" ,
97
+ 3 ,
98
+ )
99
+ dash_dcc .wait_for_text_to_equal ("#test-pathname" , "/test/pathname" )
100
+
101
+ # Check that hash is updated in the Location
102
+ dash_dcc .find_element ("#test-link-hash" ).click ()
103
+ dash_dcc .wait_for_text_to_equal ("#test-pathname" , "/test/pathname" )
104
+ dash_dcc .wait_for_text_to_equal ("#test-hash" , "#test" )
105
+ dash_dcc .percy_snapshot ("link -- /test/pathname#test" )
106
+
107
+ # Check that search is updated in the Location
108
+ # note that this goes through href and therefore wipes the hash
109
+ dash_dcc .find_element ("#test-link-search" ).click ()
110
+ dash_dcc .wait_for_text_to_equal ("#test-search" , "?testQuery=testValue" )
111
+ dash_dcc .wait_for_text_to_equal ("#test-hash" , "" )
112
+ dash_dcc .percy_snapshot ("link -- /test/pathname?testQuery=testValue" )
113
+
114
+ # Check that pathname is updated through a Button click via props
115
+ dash_dcc .find_element ("#test-button" ).click ()
116
+ dash_dcc .wait_for_text_to_equal ("#test-pathname" , "/new/pathname" )
117
+ dash_dcc .wait_for_text_to_equal ("#test-search" , "?testQuery=testValue" )
118
+ dash_dcc .percy_snapshot ("link -- /new/pathname?testQuery=testValue" )
119
+
120
+ # Check that pathname is updated through an a tag click via props
121
+ dash_dcc .find_element ("#test-a" ).click ()
122
+
123
+ dash_dcc .wait_for_text_to_equal ("#test-pathname" , "/test/pathname/a" )
124
+ dash_dcc .wait_for_text_to_equal ("#test-search" , "" )
125
+ dash_dcc .wait_for_text_to_equal ("#test-hash" , "" )
126
+ dash_dcc .percy_snapshot ("link -- /test/pathname/a" )
127
+
128
+ # Check that hash is updated through an a tag click via props
129
+ dash_dcc .find_element ("#test-a-hash" ).click ()
130
+ dash_dcc .wait_for_text_to_equal ("#test-pathname" , "/test/pathname/a" )
131
+ dash_dcc .wait_for_text_to_equal ("#test-search" , "" )
132
+ dash_dcc .wait_for_text_to_equal ("#test-hash" , "#test-hash" )
133
+ dash_dcc .percy_snapshot ("link -- /test/pathname/a#test-hash" )
134
+
135
+ # Check that hash is updated through an a tag click via props
136
+ dash_dcc .find_element ("#test-a-query" ).click ()
137
+ dash_dcc .wait_for_text_to_equal ("#test-pathname" , "/test/pathname/a" )
138
+ dash_dcc .wait_for_text_to_equal ("#test-search" , "?queryA=valueA" )
139
+ dash_dcc .wait_for_text_to_equal ("#test-hash" , "" )
140
+ dash_dcc .percy_snapshot ("link -- /test/pathname/a?queryA=valueA" )
0 commit comments