|
3 | 3 | import dash_core_components as dcc
|
4 | 4 | from dash import Dash
|
5 | 5 | from dash.dependencies import Input, Output
|
| 6 | +from dash.exceptions import PreventUpdate |
6 | 7 | from .IntegrationTests import IntegrationTests
|
7 | 8 | from .utils import assert_clean_console, invincible, wait_for
|
8 | 9 |
|
@@ -64,3 +65,50 @@ def update_output(value):
|
64 | 65 | )
|
65 | 66 |
|
66 | 67 | assert_clean_console(self)
|
| 68 | + |
| 69 | + def test_aborted_callback(self): |
| 70 | + """Raising PreventUpdate prevents update and triggering dependencies""" |
| 71 | + |
| 72 | + initial_input = 'initial input' |
| 73 | + initial_output = 'initial output' |
| 74 | + |
| 75 | + app = Dash(__name__) |
| 76 | + app.layout = html.Div([ |
| 77 | + dcc.Input(id='input', value=initial_input), |
| 78 | + html.Div(initial_output, id='output1'), |
| 79 | + html.Div(initial_output, id='output2'), |
| 80 | + ]) |
| 81 | + |
| 82 | + callback1_count = Value('i', 0) |
| 83 | + callback2_count = Value('i', 0) |
| 84 | + |
| 85 | + @app.callback(Output('output1', 'children'), [Input('input', 'value')]) |
| 86 | + def callback1(value): |
| 87 | + callback1_count.value = callback1_count.value + 1 |
| 88 | + raise PreventUpdate("testing callback does not update") |
| 89 | + return value |
| 90 | + |
| 91 | + @app.callback(Output('output2', 'children'), [Input('output1', 'children')]) |
| 92 | + def callback2(value): |
| 93 | + callback2_count.value = callback2_count.value + 1 |
| 94 | + return value |
| 95 | + |
| 96 | + self.startServer(app) |
| 97 | + |
| 98 | + input_ = self.wait_for_element_by_id('input') |
| 99 | + input_.clear() |
| 100 | + input_.send_keys('x') |
| 101 | + output1 = self.wait_for_element_by_id('output1') |
| 102 | + output2 = self.wait_for_element_by_id('output2') |
| 103 | + |
| 104 | + # callback1 runs twice (initial page load and through send_keys) |
| 105 | + self.assertEqual(callback1_count.value, 2) |
| 106 | + |
| 107 | + # callback2 is never triggered, even on initial load |
| 108 | + self.assertEqual(callback2_count.value, 0) |
| 109 | + |
| 110 | + # double check that output1 and output2 children were not updated |
| 111 | + self.assertEqual(output1.text, initial_output) |
| 112 | + self.assertEqual(output2.text, initial_output) |
| 113 | + |
| 114 | + assert_clean_console(self) |
0 commit comments