-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtest_component_as_prop.py
295 lines (248 loc) · 9.47 KB
/
test_component_as_prop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import uuid
from dash import Dash, Input, Output, callback_context, State, MATCH
from dash_test_components import ComponentAsProp
from dash.dcc import Checklist
from dash.html import Button, Div, Span
def opt(u):
return {
"label": [
Button(
"click me", id={"type": "button", "index": u}, className="label-button"
),
Span(id={"type": "text", "index": u}, className="label-result"),
],
"value": u,
}
def test_rdcap001_component_as_prop(dash_duo):
app = Dash(__name__)
app.layout = Div(
[
ComponentAsProp(
element=Div(
"as-props",
id="as-props",
)
),
ComponentAsProp(
id="clicker-container", element=Button("click-me", id="clicker")
),
ComponentAsProp(
id="nested-output-container",
element=Div(id="nested-output"),
),
Div(
[
Button("click-nested", id="send-nested"),
Div(id="output-from-prop"),
]
),
ComponentAsProp(
id="elements",
element=[
Div("one", id="list-one"),
Div("two", id="list-two"),
Div(id="list-output"),
],
),
Div(
[
Button("click-list", id="to-list"),
Div(id="output-from-list"),
Button("click footer", id="to-footer"),
Div(id="from-header"),
Div(id="from-list-of-dict"),
Button("click to list", id="update-list-of-dict"),
],
),
ComponentAsProp(
id="shaped",
shapeEl={
"header": Button("header", id="button-header"),
"footer": Div("initial", id="footer"),
},
),
ComponentAsProp(
id="list-of-dict",
list_of_shapes=[
{"label": Button(f"click-{i}", id=f"list-click-{i}"), "value": i}
for i in range(1, 4)
],
),
ComponentAsProp(
"list-of-dict-update",
list_of_shapes=[
{
"label": Div("update me", id="update-in-list-of-dict"),
"value": 1,
},
],
),
ComponentAsProp(
id="list-of-list-of-nodes",
list_of_shapes=[
{
"label": [
Div("first-label", id="first-label"),
Div("second-label", id="second-label"),
],
"value": 2,
}
],
),
ComponentAsProp(
id="list-in-shape",
shapeEl={
"header": [
Div("one", id="first-in-shape"),
Div("two", id="second-in-shape"),
]
},
),
ComponentAsProp(
id="multi-component",
multi_components=[
{
"id": "multi",
"first": Span("first"),
"second": Span("second"),
},
{
"id": "multi2",
"first": Span("foo"),
"second": Span("bar"),
},
],
),
]
)
@app.callback(
Output("output-from-prop", "children"), [Input("clicker", "n_clicks")]
)
def from_as_prop(n_clicks):
return f"From prop: {n_clicks}"
@app.callback(
Output("nested-output", "children"), [Input("send-nested", "n_clicks")]
)
def send_nested(n_clicks):
return f"Nested: {n_clicks}"
@app.callback(
Output("output-from-list", "children"), [Input("list-two", "n_clicks")]
)
def send_list_output(n_clicks):
return f"From list: {n_clicks}"
@app.callback(Output("list-output", "children"), [Input("to-list", "n_clicks")])
def send_to_list(n_clicks):
return f"To list: {n_clicks}"
@app.callback(
Output("from-header", "children"), [Input("button-header", "n_clicks")]
)
def from_header(n_clicks):
return f"From header: {n_clicks}"
@app.callback(Output("footer", "children"), [Input("to-footer", "n_clicks")])
def send_to_footer(n_clicks):
return f"To footer: {n_clicks}"
@app.callback(
Output("update-in-list-of-dict", "children"),
[Input("update-list-of-dict", "n_clicks")],
)
def send_to_list_of_dict(n_clicks):
return f"Updated: {n_clicks}"
@app.callback(
Output("from-list-of-dict", "children"),
[Input(f"list-click-{i}", "n_clicks") for i in range(1, 4)],
prevent_initial_call=True,
)
def updated_from_list(*_):
return callback_context.triggered[0]["prop_id"]
dash_duo.start_server(app)
assert dash_duo.get_logs() == []
dash_duo.wait_for_text_to_equal("#as-props", "as-props")
elements = dash_duo.find_elements("#elements div")
assert len(elements) == 3
clicker = dash_duo.wait_for_element("#clicker")
clicker.click()
dash_duo.wait_for_text_to_equal("#output-from-prop", "From prop: 1")
nested = dash_duo.wait_for_element("#send-nested")
nested.click()
dash_duo.wait_for_text_to_equal("#nested-output", "Nested: 1")
to_list = dash_duo.find_element("#to-list")
to_list.click()
dash_duo.wait_for_text_to_equal("#list-output", "To list: 1")
from_list = dash_duo.find_element("#list-two")
from_list.click()
dash_duo.wait_for_text_to_equal("#output-from-list", "From list: 1")
from_header = dash_duo.find_element("#button-header")
from_header.click()
dash_duo.wait_for_text_to_equal("#from-header", "From header: 1")
to_footer = dash_duo.find_element("#to-footer")
to_footer.click()
dash_duo.wait_for_text_to_equal("#footer", "To footer: 1")
for btn_id in (f"list-click-{i}" for i in range(1, 4)):
dash_duo.find_element(f"#{btn_id}").click()
dash_duo.wait_for_text_to_equal("#from-list-of-dict", f"{btn_id}.n_clicks")
dash_duo.find_element("#update-list-of-dict").click()
dash_duo.wait_for_text_to_equal("#update-in-list-of-dict", "Updated: 1")
dash_duo.wait_for_text_to_equal("#first-label", "first-label")
dash_duo.wait_for_text_to_equal("#second-label", "second-label")
dash_duo.wait_for_text_to_equal("#first-in-shape", "one")
dash_duo.wait_for_text_to_equal("#second-in-shape", "two")
dash_duo.wait_for_text_to_equal("#multi", "first - second")
dash_duo.wait_for_text_to_equal("#multi2", "foo - bar")
assert dash_duo.get_logs() == []
def test_rdcap002_component_as_props_dynamic_id(dash_duo):
# Test for issue 2296
app = Dash(__name__)
n = 3
app.layout = Div(
[
Button("add options", id="add-option", style={"marginBottom": "25px"}),
Checklist([opt(str(uuid.uuid4())) for i in range(n)], id="options"),
]
)
@app.callback(
Output("options", "options"),
Input("add-option", "n_clicks"),
State("options", "options"),
prevent_initial_call=True,
)
def add_option(_, options):
return [*options, opt(str(uuid.uuid4()))]
@app.callback(
Output({"type": "text", "index": MATCH}, "children"),
Input({"type": "button", "index": MATCH}, "n_clicks"),
)
def demo(n_clicks):
return n_clicks
dash_duo.start_server(app)
dash_duo.wait_for_element("#add-option").click()
for i in range(1, n + 2):
dash_duo.wait_for_text_to_equal(f"#options label:nth-child({i}) span", "")
dash_duo.wait_for_element(f"#options label:nth-child({i}) button").click()
dash_duo.wait_for_text_to_equal(f"#options label:nth-child({i}) span", "1")
def test_rdcap003_side_effect_regression(dash_duo):
# Test for #2411, regression introduced by original rdcap002 fix
# callback on the same components that is output with same id but not property triggered
# on cap components of array type like Checklist.options[] and Dropdown.options[].
app = Dash(__name__)
app.layout = Div([Button("3<->2", id="a"), Checklist(id="b"), Div(0, id="counter")])
app.clientside_callback(
"function(_, prev) {return parseInt(prev) + 1}",
Output("counter", "children"),
Input("b", "value"),
State("counter", "children"),
prevent_initial_call=True,
)
@app.callback(Output("b", "options"), Input("a", "n_clicks"))
def opts(n):
n_out = 3 - (n or 0) % 2
return [str(i) for i in range(n_out)]
dash_duo.start_server(app)
dash_duo.wait_for_text_to_equal("#counter", "0")
dash_duo.find_element("#a").click()
assert len(dash_duo.find_elements("#b label > input")) == 2
dash_duo.wait_for_text_to_equal("#counter", "0")
dash_duo.find_element("#a").click()
assert len(dash_duo.find_elements("#b label > input")) == 3
dash_duo.wait_for_text_to_equal("#counter", "0")
dash_duo.find_elements("#b label > input")[0].click()
dash_duo.wait_for_text_to_equal("#counter", "1")