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