|
1 | 1 | import json
|
2 | 2 | from multiprocessing import Lock, Value
|
3 | 3 | import pytest
|
| 4 | +import time |
4 | 5 |
|
5 | 6 | import dash_core_components as dcc
|
6 | 7 | import dash_html_components as html
|
@@ -449,3 +450,204 @@ def content(n, d, v):
|
449 | 450 | wait.until(
|
450 | 451 | lambda: dash_duo.find_element("#output").text == '[1, 2, "A"]', 3,
|
451 | 452 | )
|
| 453 | + |
| 454 | + |
| 455 | +def test_cbsc010_event_properties(dash_duo): |
| 456 | + app = dash.Dash(__name__) |
| 457 | + app.layout = html.Div([html.Button("Click Me", id="button"), html.Div(id="output")]) |
| 458 | + |
| 459 | + call_count = Value("i", 0) |
| 460 | + |
| 461 | + @app.callback(Output("output", "children"), [Input("button", "n_clicks")]) |
| 462 | + def update_output(n_clicks): |
| 463 | + if not n_clicks: |
| 464 | + raise PreventUpdate |
| 465 | + call_count.value += 1 |
| 466 | + return "Click" |
| 467 | + |
| 468 | + dash_duo.start_server(app) |
| 469 | + dash_duo.wait_for_text_to_equal("#output", "") |
| 470 | + assert call_count.value == 0 |
| 471 | + |
| 472 | + dash_duo.find_element("#button").click() |
| 473 | + dash_duo.wait_for_text_to_equal("#output", "Click") |
| 474 | + assert call_count.value == 1 |
| 475 | + |
| 476 | + |
| 477 | +def test_cbsc011_one_call_for_multiple_outputs_initial(dash_duo): |
| 478 | + app = dash.Dash(__name__) |
| 479 | + call_count = Value("i", 0) |
| 480 | + |
| 481 | + app.layout = html.Div( |
| 482 | + [ |
| 483 | + html.Div( |
| 484 | + [ |
| 485 | + dcc.Input(value="Input {}".format(i), id="input-{}".format(i)) |
| 486 | + for i in range(10) |
| 487 | + ] |
| 488 | + ), |
| 489 | + html.Div(id="container"), |
| 490 | + dcc.RadioItems(), |
| 491 | + ] |
| 492 | + ) |
| 493 | + |
| 494 | + @app.callback( |
| 495 | + Output("container", "children"), |
| 496 | + [Input("input-{}".format(i), "value") for i in range(10)], |
| 497 | + ) |
| 498 | + def dynamic_output(*args): |
| 499 | + call_count.value += 1 |
| 500 | + return json.dumps(args, indent=2) |
| 501 | + |
| 502 | + dash_duo.start_server(app) |
| 503 | + dash_duo.wait_for_text_to_equal("#input-9", "Input 9") |
| 504 | + |
| 505 | + assert call_count.value == 1 |
| 506 | + dash_duo.percy_snapshot("test_rendering_layout_calls_callback_once_per_output") |
| 507 | + |
| 508 | + |
| 509 | +def test_cbsc012_one_call_for_multiple_outputs_update(dash_duo): |
| 510 | + app = dash.Dash(__name__, suppress_callback_exceptions=True) |
| 511 | + call_count = Value("i", 0) |
| 512 | + |
| 513 | + app.layout = html.Div( |
| 514 | + [ |
| 515 | + html.Button(id="display-content", children="Display Content"), |
| 516 | + html.Div(id="container"), |
| 517 | + dcc.RadioItems(), |
| 518 | + ] |
| 519 | + ) |
| 520 | + |
| 521 | + @app.callback(Output("container", "children"), Input("display-content", "n_clicks")) |
| 522 | + def display_output(n_clicks): |
| 523 | + if not n_clicks: |
| 524 | + return "" |
| 525 | + return html.Div( |
| 526 | + [ |
| 527 | + html.Div( |
| 528 | + [ |
| 529 | + dcc.Input(value="Input {}".format(i), id="input-{}".format(i)) |
| 530 | + for i in range(10) |
| 531 | + ] |
| 532 | + ), |
| 533 | + html.Div(id="dynamic-output"), |
| 534 | + ] |
| 535 | + ) |
| 536 | + |
| 537 | + @app.callback( |
| 538 | + Output("dynamic-output", "children"), |
| 539 | + [Input("input-{}".format(i), "value") for i in range(10)], |
| 540 | + ) |
| 541 | + def dynamic_output(*args): |
| 542 | + call_count.value += 1 |
| 543 | + return json.dumps(args, indent=2) |
| 544 | + |
| 545 | + dash_duo.start_server(app) |
| 546 | + |
| 547 | + dash_duo.find_element("#display-content").click() |
| 548 | + |
| 549 | + dash_duo.wait_for_text_to_equal("#input-9", "Input 9") |
| 550 | + assert call_count.value == 1 |
| 551 | + |
| 552 | + dash_duo.percy_snapshot("test_rendering_new_content_calls_callback_once_per_output") |
| 553 | + |
| 554 | + |
| 555 | +def test_cbsc013_multi_output_out_of_order(dash_duo): |
| 556 | + app = dash.Dash(__name__) |
| 557 | + app.layout = html.Div( |
| 558 | + [ |
| 559 | + html.Button(id="input", n_clicks=0), |
| 560 | + html.Div(id="output1"), |
| 561 | + html.Div(id="output2"), |
| 562 | + ] |
| 563 | + ) |
| 564 | + |
| 565 | + call_count = Value("i", 0) |
| 566 | + lock = Lock() |
| 567 | + |
| 568 | + @app.callback( |
| 569 | + Output("output1", "children"), |
| 570 | + Output("output2", "children"), |
| 571 | + Input("input", "n_clicks"), |
| 572 | + ) |
| 573 | + def update_output(n_clicks): |
| 574 | + call_count.value += 1 |
| 575 | + if n_clicks == 1: |
| 576 | + with lock: |
| 577 | + pass |
| 578 | + return n_clicks, n_clicks + 1 |
| 579 | + |
| 580 | + dash_duo.start_server(app) |
| 581 | + |
| 582 | + button = dash_duo.find_element("#input") |
| 583 | + with lock: |
| 584 | + button.click() |
| 585 | + button.click() |
| 586 | + |
| 587 | + dash_duo.wait_for_text_to_equal("#output1", "2") |
| 588 | + dash_duo.wait_for_text_to_equal("#output2", "3") |
| 589 | + assert call_count.value == 3 |
| 590 | + dash_duo.percy_snapshot( |
| 591 | + "test_callbacks_called_multiple_times_and_out_of_order_multi_output" |
| 592 | + ) |
| 593 | + assert dash_duo.driver.execute_script("return !window.store.getState().isLoading;") |
| 594 | + |
| 595 | + |
| 596 | +def test_cbsc014_multiple_properties_update_at_same_time_on_same_component(dash_duo): |
| 597 | + call_count = Value("i", 0) |
| 598 | + timestamp_1 = Value("d", -5) |
| 599 | + timestamp_2 = Value("d", -5) |
| 600 | + |
| 601 | + app = dash.Dash(__name__) |
| 602 | + app.layout = html.Div( |
| 603 | + [ |
| 604 | + html.Div(id="container"), |
| 605 | + html.Button("Click", id="button-1", n_clicks=0, n_clicks_timestamp=-1), |
| 606 | + html.Button("Click", id="button-2", n_clicks=0, n_clicks_timestamp=-1), |
| 607 | + ] |
| 608 | + ) |
| 609 | + |
| 610 | + @app.callback( |
| 611 | + Output("container", "children"), |
| 612 | + Input("button-1", "n_clicks"), |
| 613 | + Input("button-1", "n_clicks_timestamp"), |
| 614 | + Input("button-2", "n_clicks"), |
| 615 | + Input("button-2", "n_clicks_timestamp"), |
| 616 | + ) |
| 617 | + def update_output(n1, t1, n2, t2): |
| 618 | + call_count.value += 1 |
| 619 | + timestamp_1.value = t1 |
| 620 | + timestamp_2.value = t2 |
| 621 | + return "{}, {}".format(n1, n2) |
| 622 | + |
| 623 | + dash_duo.start_server(app) |
| 624 | + |
| 625 | + dash_duo.wait_for_text_to_equal("#container", "0, 0") |
| 626 | + assert timestamp_1.value == -1 |
| 627 | + assert timestamp_2.value == -1 |
| 628 | + assert call_count.value == 1 |
| 629 | + dash_duo.percy_snapshot("button initialization 1") |
| 630 | + |
| 631 | + dash_duo.find_element("#button-1").click() |
| 632 | + dash_duo.wait_for_text_to_equal("#container", "1, 0") |
| 633 | + assert timestamp_1.value > ((time.time() - (24 * 60 * 60)) * 1000) |
| 634 | + assert timestamp_2.value == -1 |
| 635 | + assert call_count.value == 2 |
| 636 | + dash_duo.percy_snapshot("button-1 click") |
| 637 | + prev_timestamp_1 = timestamp_1.value |
| 638 | + |
| 639 | + dash_duo.find_element("#button-2").click() |
| 640 | + dash_duo.wait_for_text_to_equal("#container", "1, 1") |
| 641 | + assert timestamp_1.value == prev_timestamp_1 |
| 642 | + assert timestamp_2.value > ((time.time() - 24 * 60 * 60) * 1000) |
| 643 | + assert call_count.value == 3 |
| 644 | + dash_duo.percy_snapshot("button-2 click") |
| 645 | + prev_timestamp_2 = timestamp_2.value |
| 646 | + |
| 647 | + dash_duo.find_element("#button-2").click() |
| 648 | + dash_duo.wait_for_text_to_equal("#container", "1, 2") |
| 649 | + assert timestamp_1.value == prev_timestamp_1 |
| 650 | + assert timestamp_2.value > prev_timestamp_2 |
| 651 | + assert timestamp_2.value > timestamp_1.value |
| 652 | + assert call_count.value == 4 |
| 653 | + dash_duo.percy_snapshot("button-2 click again") |
0 commit comments