|
5 | 5 | from selenium.webdriver.common.keys import Keys
|
6 | 6 |
|
7 | 7 | import dash
|
8 |
| -from dash.dependencies import Input, Output |
| 8 | +from dash.dependencies import Input, Output, State, MATCH |
9 | 9 |
|
10 | 10 | import dash_core_components as dcc
|
11 | 11 | import dash_html_components as html
|
@@ -451,3 +451,78 @@ def set_out(val):
|
451 | 451 | dash_duo.find_element("#persistence-val").send_keys("2")
|
452 | 452 | assert not dash_duo.get_logs()
|
453 | 453 | dash_duo.wait_for_text_to_equal("#out", "artichoke")
|
| 454 | + |
| 455 | + |
| 456 | +def test_rdps012_pattern_matching(dash_duo): |
| 457 | + # copy of rdps010 but with dict IDs, |
| 458 | + # plus a button to change the dict ID so the persistence should reset |
| 459 | + def make_input(persistence, n): |
| 460 | + return dcc.Input( |
| 461 | + id={"i": n, "id": "persisted"}, |
| 462 | + className="persisted", |
| 463 | + value="a", |
| 464 | + persistence=persistence, |
| 465 | + ) |
| 466 | + |
| 467 | + app = dash.Dash(__name__) |
| 468 | + app.layout = html.Div( |
| 469 | + [html.Button("click", id="btn", n_clicks=0), html.Div(id="content")] |
| 470 | + ) |
| 471 | + |
| 472 | + @app.callback(Output("content", "children"), [Input("btn", "n_clicks")]) |
| 473 | + def content(n): |
| 474 | + return [ |
| 475 | + dcc.Input( |
| 476 | + id={"i": n, "id": "persistence-val"}, |
| 477 | + value="", |
| 478 | + className="persistence-val", |
| 479 | + ), |
| 480 | + html.Div(make_input("", n), id={"i": n, "id": "persisted-container"}), |
| 481 | + html.Div(id={"i": n, "id": "out"}, className="out"), |
| 482 | + ] |
| 483 | + |
| 484 | + @app.callback( |
| 485 | + Output({"i": MATCH, "id": "persisted-container"}, "children"), |
| 486 | + [Input({"i": MATCH, "id": "persistence-val"}, "value")], |
| 487 | + [State("btn", "n_clicks")], |
| 488 | + ) |
| 489 | + def set_persistence(val, n): |
| 490 | + return make_input(val, n) |
| 491 | + |
| 492 | + @app.callback( |
| 493 | + Output({"i": MATCH, "id": "out"}, "children"), |
| 494 | + [Input({"i": MATCH, "id": "persisted"}, "value")], |
| 495 | + ) |
| 496 | + def set_out(val): |
| 497 | + return val |
| 498 | + |
| 499 | + dash_duo.start_server(app) |
| 500 | + |
| 501 | + for _ in range(3): |
| 502 | + dash_duo.wait_for_text_to_equal(".out", "a") |
| 503 | + dash_duo.find_element(".persisted").send_keys("lpaca") |
| 504 | + dash_duo.wait_for_text_to_equal(".out", "alpaca") |
| 505 | + |
| 506 | + dash_duo.find_element(".persistence-val").send_keys("s") |
| 507 | + dash_duo.wait_for_text_to_equal(".out", "a") |
| 508 | + dash_duo.find_element(".persisted").send_keys("nchovies") |
| 509 | + dash_duo.wait_for_text_to_equal(".out", "anchovies") |
| 510 | + |
| 511 | + dash_duo.find_element(".persistence-val").send_keys("2") |
| 512 | + dash_duo.wait_for_text_to_equal(".out", "a") |
| 513 | + dash_duo.find_element(".persisted").send_keys( |
| 514 | + Keys.BACK_SPACE |
| 515 | + ) # persist falsy value |
| 516 | + dash_duo.wait_for_text_to_equal(".out", "") |
| 517 | + |
| 518 | + # alpaca not saved with falsy persistence |
| 519 | + dash_duo.clear_input(".persistence-val") |
| 520 | + dash_duo.wait_for_text_to_equal(".out", "a") |
| 521 | + |
| 522 | + # anchovies and aardvark saved |
| 523 | + dash_duo.find_element(".persistence-val").send_keys("s") |
| 524 | + dash_duo.wait_for_text_to_equal(".out", "anchovies") |
| 525 | + dash_duo.find_element(".persistence-val").send_keys("2") |
| 526 | + dash_duo.wait_for_text_to_equal(".out", "") |
| 527 | + |
| 528 | + dash_duo.find_element("#btn").click() |
0 commit comments