|
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 |
|
5 |
| -from dash import Dash, html, dcc, Output, Input, State |
| 5 | +from dash import Dash, html, dcc, Output, Input, State, Patch |
6 | 6 | from dash.exceptions import PreventUpdate
|
7 | 7 |
|
| 8 | +from selenium.webdriver.common.keys import Keys |
8 | 9 |
|
9 | 10 | sample_dropdown_options = [
|
10 | 11 | {"label": "New York City", "value": "NYC"},
|
@@ -147,3 +148,44 @@ def print_value(n_clicks, options, value):
|
147 | 148 | btn.click()
|
148 | 149 | dash_dcc.wait_for_text_to_equal("#value-output", '["NYC"]')
|
149 | 150 | dash_dcc.wait_for_text_to_equal("#options-output", '["MTL", "NYC"]')
|
| 151 | + |
| 152 | + |
| 153 | +def test_ddro004_empty_string_not_updated(dash_dcc): |
| 154 | + # The value should stay as empty string and not null. |
| 155 | + app = Dash() |
| 156 | + app.layout = html.Div( |
| 157 | + [ |
| 158 | + dcc.Dropdown(["a", "b", "c"], value="", id="drop"), |
| 159 | + html.Div(id="output"), |
| 160 | + dcc.Store(data={"count": 0}, id="count"), |
| 161 | + html.Div(id="count-output"), |
| 162 | + ] |
| 163 | + ) |
| 164 | + |
| 165 | + @app.callback( |
| 166 | + Output("output", "children"), |
| 167 | + Output("count", "data"), |
| 168 | + Input("drop", "value"), |
| 169 | + ) |
| 170 | + def on_value(value): |
| 171 | + count = Patch() |
| 172 | + count.count += 1 |
| 173 | + if value is None: |
| 174 | + return "Value is none", count |
| 175 | + return f"Value={value}", count |
| 176 | + |
| 177 | + app.clientside_callback( |
| 178 | + "data => data.count", Output("count-output", "children"), Input("count", "data") |
| 179 | + ) |
| 180 | + |
| 181 | + dash_dcc.start_server(app) |
| 182 | + dash_dcc.wait_for_text_to_equal("#output", "Value=") |
| 183 | + |
| 184 | + dash_dcc.wait_for_text_to_equal("#count-output", "1") |
| 185 | + |
| 186 | + select_input = dash_dcc.find_element("#drop input") |
| 187 | + select_input.send_keys("a") |
| 188 | + select_input.send_keys(Keys.ENTER) |
| 189 | + |
| 190 | + dash_dcc.wait_for_text_to_equal("#output", "Value=a") |
| 191 | + dash_dcc.wait_for_text_to_equal("#count-output", "2") |
0 commit comments