|
| 1 | +# -*- coding: UTF-8 -*- |
| 2 | + |
| 3 | +import pytest |
| 4 | +from dash.testing import wait |
| 5 | +from dash import Dash, Input, Output, dcc, html |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.DCC793 |
| 9 | +@pytest.mark.parametrize("component_type,component_kwargs,selector_class_name", [ |
| 10 | + (dcc.Dropdown, {"multi": True, "value": "NYC"}, "Select-value"), |
| 11 | + (dcc.Dropdown, {"multi": False, "value": "NYC"}, "Select-value"), |
| 12 | + (dcc.Checklist, {"value": ["NYC"], "labelClassName": "Select-value-label"}, "Select-value-label"), |
| 13 | + (dcc.RadioItems, {"value": "NYC", "labelClassName": "Select-value-label"}, "Select-value-label"), |
| 14 | +]) |
| 15 | +def test_ddot001_dropdown_radioitems_checklist_option_title(dash_dcc, component_type, component_kwargs, selector_class_name): |
| 16 | + app = Dash(__name__) |
| 17 | + |
| 18 | + id_prefix = component_type.__name__.lower() |
| 19 | + |
| 20 | + app.layout = html.Div( |
| 21 | + [ |
| 22 | + dcc.Input( |
| 23 | + id=f"{id_prefix}_title_input", |
| 24 | + type="text", |
| 25 | + placeholder="Enter a title for New York City", |
| 26 | + ), |
| 27 | + component_type( |
| 28 | + id=id_prefix, |
| 29 | + options=[ |
| 30 | + {"label": "New York City", "value": "NYC"}, |
| 31 | + {"label": "Montréal", "value": "MTL"}, |
| 32 | + {"label": "San Francisco", "value": "SF"}, |
| 33 | + ], |
| 34 | + **component_kwargs |
| 35 | + ), |
| 36 | + ] |
| 37 | + ) |
| 38 | + |
| 39 | + @app.callback( |
| 40 | + Output(id_prefix, "options"), [Input(f"{id_prefix}_title_input", "value")] |
| 41 | + ) |
| 42 | + def add_title_to_option(title): |
| 43 | + return [ |
| 44 | + {"label": "New York City", "title": title, "value": "NYC"}, |
| 45 | + {"label": "Montréal", "value": "MTL"}, |
| 46 | + {"label": "San Francisco", "value": "SF"}, |
| 47 | + ] |
| 48 | + |
| 49 | + dash_dcc.start_server(app) |
| 50 | + |
| 51 | + component_option_element = dash_dcc.wait_for_element(f"#{id_prefix} .{selector_class_name}") |
| 52 | + |
| 53 | + component_title_input = dash_dcc.wait_for_element(f"#{id_prefix}_title_input") |
| 54 | + |
| 55 | + # Empty string title ('') (default for no title) |
| 56 | + |
| 57 | + wait.until(lambda: component_option_element.get_attribute("title") == "", 3) |
| 58 | + |
| 59 | + component_title_input.send_keys("The Big Apple") |
| 60 | + |
| 61 | + wait.until( |
| 62 | + lambda: component_option_element.get_attribute("title") == "The Big Apple", 3 |
| 63 | + ) |
| 64 | + |
| 65 | + dash_dcc.clear_input(component_title_input) |
| 66 | + |
| 67 | + component_title_input.send_keys("Gotham City?") |
| 68 | + |
| 69 | + wait.until( |
| 70 | + lambda: component_option_element.get_attribute("title") == "Gotham City?", 3 |
| 71 | + ) |
| 72 | + |
| 73 | + dash_dcc.clear_input(component_title_input) |
| 74 | + |
| 75 | + wait.until(lambda: component_option_element.get_attribute("title") == "", 3) |
| 76 | + |
| 77 | + assert dash_dcc.get_logs() == [] |
0 commit comments