|
| 1 | +from dash import Dash, Input, Output, html, dcc, State, ALL |
| 2 | + |
| 3 | + |
| 4 | +class Section: |
| 5 | + def __init__(self, idx): |
| 6 | + self.idx = idx |
| 7 | + self.options = ["A", "B", "C"] |
| 8 | + |
| 9 | + @property |
| 10 | + def section_id(self): |
| 11 | + return {"type": "section-container", "id": self.idx} |
| 12 | + |
| 13 | + @property |
| 14 | + def dropdown_id(self): |
| 15 | + return {"type": "dropdown", "id": self.idx} |
| 16 | + |
| 17 | + @property |
| 18 | + def swap_btn_id(self): |
| 19 | + return {"type": "swap-btn", "id": self.idx} |
| 20 | + |
| 21 | + def get_layout(self) -> html.Div: |
| 22 | + layout = html.Div( |
| 23 | + id=self.section_id, |
| 24 | + children=[ |
| 25 | + html.H1(f"I am section {self.idx}"), |
| 26 | + html.Button( |
| 27 | + "SWAP", |
| 28 | + id=self.swap_btn_id, |
| 29 | + n_clicks=0, |
| 30 | + className=f"swap_button_{self.idx}", |
| 31 | + ), |
| 32 | + dcc.Dropdown( |
| 33 | + self.options, |
| 34 | + id=self.dropdown_id, |
| 35 | + multi=True, |
| 36 | + value=[], |
| 37 | + className=f"dropdown_{self.idx}", |
| 38 | + ), |
| 39 | + ], |
| 40 | + ) |
| 41 | + return layout |
| 42 | + |
| 43 | + |
| 44 | +def test_roc001_reorder_children(dash_duo): |
| 45 | + app = Dash() |
| 46 | + |
| 47 | + app.layout = html.Div( |
| 48 | + id="main-app", children=[*[Section(idx=i).get_layout() for i in range(2)]] |
| 49 | + ) |
| 50 | + |
| 51 | + @app.callback( |
| 52 | + Output("main-app", "children"), |
| 53 | + Input({"type": "swap-btn", "id": ALL}, "n_clicks"), |
| 54 | + State("main-app", "children"), |
| 55 | + prevent_initial_call=True, |
| 56 | + ) |
| 57 | + def swap_button_action(n_clicks, children): |
| 58 | + if any(n > 0 for n in n_clicks): |
| 59 | + return children[::-1] |
| 60 | + |
| 61 | + dash_duo.start_server(app) |
| 62 | + |
| 63 | + for i in range(2): |
| 64 | + dash_duo.wait_for_text_to_equal("h1", f"I am section {i}") |
| 65 | + dash_duo.wait_for_text_to_equal( |
| 66 | + f".dropdown_{i} .Select-multi-value-wrapper", "Select..." |
| 67 | + ) |
| 68 | + dash_duo.find_element(f".dropdown_{i}").click() |
| 69 | + dash_duo.find_element(f".dropdown_{i} .VirtualizedSelectOption").click() |
| 70 | + dash_duo.wait_for_text_to_equal( |
| 71 | + f".dropdown_{i} .Select-multi-value-wrapper", "×A\n " |
| 72 | + ) |
| 73 | + dash_duo.find_element(f".dropdown_{i}").click() |
| 74 | + dash_duo.find_element(f".dropdown_{i} .VirtualizedSelectOption").click() |
| 75 | + dash_duo.wait_for_text_to_equal( |
| 76 | + f".dropdown_{i} .Select-multi-value-wrapper", "×A\n ×B\n " |
| 77 | + ) |
| 78 | + dash_duo.find_element(f".dropdown_{i}").click() |
| 79 | + dash_duo.find_element(f".dropdown_{i} .VirtualizedSelectOption").click() |
| 80 | + dash_duo.wait_for_text_to_equal( |
| 81 | + f".dropdown_{i} .Select-multi-value-wrapper", "×A\n ×B\n ×C\n " |
| 82 | + ) |
| 83 | + dash_duo.find_element(f".swap_button_{i}").click() |
| 84 | + dash_duo.wait_for_text_to_equal( |
| 85 | + f".dropdown_{0} .Select-multi-value-wrapper", "×A\n ×B\n ×C\n " |
| 86 | + ) |
| 87 | + dash_duo.wait_for_text_to_equal( |
| 88 | + f".dropdown_{1} .Select-multi-value-wrapper", "×A\n ×B\n ×C\n " |
| 89 | + ) |
0 commit comments