Skip to content

Commit fb60ce0

Browse files
committed
Add test arbitrary callbacks.
1 parent 2f84022 commit fb60ce0

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
from dash import Dash, Input, Output, html, set_props, register_page
2+
3+
4+
def test_arb001_global_set_props(dash_duo):
5+
app = Dash()
6+
app.layout = html.Div(
7+
[
8+
html.Div(id="output"),
9+
html.Div(id="secondary-output"),
10+
html.Button("click", id="clicker"),
11+
]
12+
)
13+
14+
@app.callback(
15+
Output("output", "children"),
16+
Input("clicker", "n_clicks"),
17+
prevent_initial_call=True,
18+
)
19+
def on_click(n_clicks):
20+
set_props("secondary-output", {"children": "secondary"})
21+
return f"Clicked {n_clicks} times"
22+
23+
dash_duo.start_server(app)
24+
25+
dash_duo.wait_for_element("#clicker").click()
26+
dash_duo.wait_for_text_to_equal("#output", "Clicked 1 times")
27+
dash_duo.wait_for_text_to_equal("#secondary-output", "secondary")
28+
29+
30+
def test_arb002_no_output_callbacks(dash_duo):
31+
app = Dash()
32+
33+
app.layout = html.Div(
34+
[
35+
html.Div(id="secondary-output"),
36+
html.Button("no-output", id="no-output"),
37+
html.Button("no-output2", id="no-output2"),
38+
]
39+
)
40+
41+
@app.callback(
42+
Input("no-output", "n_clicks"),
43+
prevent_initial_call=True,
44+
)
45+
def no_output(_):
46+
set_props("secondary-output", {"children": "no-output"})
47+
48+
@app.callback(
49+
Input("no-output2", "n_clicks"),
50+
prevent_initial_call=True,
51+
)
52+
def no_output(_):
53+
set_props("secondary-output", {"children": "no-output2"})
54+
55+
dash_duo.start_server(app)
56+
57+
dash_duo.wait_for_element("#no-output").click()
58+
dash_duo.wait_for_text_to_equal("#secondary-output", "no-output")
59+
60+
dash_duo.wait_for_element("#no-output2").click()
61+
dash_duo.wait_for_text_to_equal("#secondary-output", "no-output2")
62+
63+
64+
def test_arb003_arbitrary_pages(dash_duo):
65+
app = Dash(use_pages=True, pages_folder="")
66+
67+
register_page(
68+
"page",
69+
"/",
70+
layout=html.Div(
71+
[
72+
html.Div(id="secondary-output"),
73+
html.Button("no-output", id="no-output"),
74+
html.Button("no-output2", id="no-output2"),
75+
]
76+
),
77+
)
78+
79+
@app.callback(
80+
Input("no-output", "n_clicks"),
81+
prevent_initial_call=True,
82+
)
83+
def no_output(_):
84+
set_props("secondary-output", {"children": "no-output"})
85+
86+
@app.callback(
87+
Input("no-output2", "n_clicks"),
88+
prevent_initial_call=True,
89+
)
90+
def no_output(_):
91+
set_props("secondary-output", {"children": "no-output2"})
92+
93+
dash_duo.start_server(app)
94+
95+
dash_duo.wait_for_element("#no-output").click()
96+
dash_duo.wait_for_text_to_equal("#secondary-output", "no-output")
97+
98+
dash_duo.wait_for_element("#no-output2").click()
99+
dash_duo.wait_for_text_to_equal("#secondary-output", "no-output2")
100+
101+
102+
def test_arb004_wildcard_set_props(dash_duo):
103+
app = Dash()
104+
app.layout = html.Div(
105+
[
106+
html.Button("click", id="click"),
107+
html.Div(html.Div(id={"id": "output", "index": 0}), id="output"),
108+
]
109+
)
110+
111+
@app.callback(
112+
Input("click", "n_clicks"),
113+
prevent_initial_call=True,
114+
)
115+
def on_click(n_clicks):
116+
set_props(
117+
{"id": "output", "index": 0}, {"children": f"Clicked {n_clicks} times"}
118+
)
119+
120+
dash_duo.start_server(app)
121+
122+
dash_duo.wait_for_element("#click").click()
123+
dash_duo.wait_for_text_to_equal("#output", "Clicked 1 times")

0 commit comments

Comments
 (0)