Skip to content

Commit d1db799

Browse files
committed
Add test dropdown remove options.
1 parent 6ee2328 commit d1db799

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import json
2+
3+
from dash import Dash, html, dcc, Output, Input
4+
from dash.exceptions import PreventUpdate
5+
6+
7+
sample_dropdown_options = [
8+
{"label": "New York City", "value": "NYC"},
9+
{"label": "Montreal", "value": "MTL"},
10+
{"label": "San Francisco", "value": "SF"},
11+
]
12+
13+
14+
def test_ddro001_remove_option_single(dash_dcc):
15+
dropdown_options = sample_dropdown_options
16+
17+
app = Dash(__name__)
18+
value = 'SF'
19+
20+
app.layout = html.Div([
21+
dcc.Dropdown(
22+
options=dropdown_options,
23+
value=value,
24+
id='dropdown',
25+
),
26+
html.Button('Remove option', id='remove'),
27+
html.Div(id='value-output')
28+
])
29+
30+
@app.callback(
31+
Output('dropdown', 'options'),
32+
[Input('remove', 'n_clicks')]
33+
)
34+
def on_click(n_clicks):
35+
if not n_clicks:
36+
raise PreventUpdate
37+
return sample_dropdown_options[:-1]
38+
39+
@app.callback(
40+
Output('value-output', 'children'),
41+
[Input('dropdown', 'value')]
42+
)
43+
def on_change(val):
44+
if not val:
45+
raise PreventUpdate
46+
return val or 'None'
47+
48+
dash_dcc.start_server(app)
49+
btn = dash_dcc.wait_for_element('#remove')
50+
btn.click()
51+
52+
dash_dcc.wait_for_text_to_equal('#value-output', 'None')
53+
54+
55+
def test_ddro002_remove_option_multi(dash_dcc):
56+
dropdown_options = sample_dropdown_options
57+
58+
app = Dash(__name__)
59+
value = ['MTL', 'SF']
60+
61+
app.layout = html.Div([
62+
dcc.Dropdown(
63+
options=dropdown_options,
64+
value=value,
65+
multi=True,
66+
id='dropdown',
67+
),
68+
html.Button('Remove option', id='remove'),
69+
html.Div(id='value-output')
70+
])
71+
72+
@app.callback(
73+
Output('dropdown', 'options'),
74+
[Input('remove', 'n_clicks')]
75+
)
76+
def on_click(n_clicks):
77+
if not n_clicks:
78+
raise PreventUpdate
79+
return sample_dropdown_options[:-1]
80+
81+
@app.callback(
82+
Output('value-output', 'children'),
83+
[Input('dropdown', 'value')]
84+
)
85+
def on_change(val):
86+
return json.dumps(val)
87+
88+
dash_dcc.start_server(app)
89+
btn = dash_dcc.wait_for_element('#remove')
90+
btn.click()
91+
92+
dash_dcc.wait_for_text_to_equal('#value-output', '["MTL"]')

0 commit comments

Comments
 (0)