Skip to content

Commit bf208c4

Browse files
Added persistence callback test.
1 parent 82b50dd commit bf208c4

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

dash/dash-renderer/src/observers/executedCallbacks.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ import {ICallback, IStoredCallback} from '../types/callbacks';
3636
import {updateProps, setPaths, handleAsyncError} from '../actions';
3737
import {getPath, computePaths} from '../actions/paths';
3838

39-
import {applyPersistence, prunePersistence, setPersistance} from '../persistence';
39+
import {
40+
applyPersistence,
41+
prunePersistence,
42+
setPersistance
43+
} from '../persistence';
4044
import {IStoreObserverDefinition} from '../StoreObserver';
4145

4246
const observer: IStoreObserverDefinition<IStoreState> = {
@@ -66,7 +70,7 @@ const observer: IStoreObserverDefinition<IStoreState> = {
6670
const {props} = applyPersistence({props: updatedProps}, dispatch);
6771

6872
// Save props to storage if persistance is active.
69-
setPersistance(path(itempath, layout), updatedProps, dispatch)
73+
setPersistance(path(itempath, layout), updatedProps, dispatch);
7074

7175
dispatch(
7276
updateProps({

dash/dash-renderer/src/persistence.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,15 @@ export function setPersistance(layout, newProps, dispatch) {
339339
if (storage.hasItem(valsKey)) {
340340
originalVal = storage.getItem(valsKey)[1];
341341
if (newVal !== originalVal) {
342-
storage.setItem(valsKey, [newVal, originalVal], dispatch);
342+
storage.setItem(
343+
valsKey,
344+
[newVal, originalVal],
345+
dispatch
346+
);
347+
} else {
348+
storage.removeItem(valsKey);
343349
}
344-
else {
345-
storage.removeItem(valsKey)
346-
}
347-
}
348-
else {
350+
} else {
349351
storage.setItem(valsKey, [newVal, originalVal], dispatch);
350352
}
351353
}

tests/integration/renderer/test_persistence.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,64 @@ def update_container2(n_clicks):
558558
# persistenceTransforms should return upper case strings
559559
dash_duo.wait_for_text_to_equal("#component-propName", "ALPACA")
560560
dash_duo.wait_for_text_to_equal("#component-propPart", "ARTICHOKE")
561+
562+
563+
def test_rdps014_save_callback_persistence(dash_duo):
564+
app = dash.Dash(__name__)
565+
566+
def make_input(persistence):
567+
return dcc.Input(id="persisted", value="a", persistence=persistence)
568+
569+
app.layout = html.Div(
570+
[
571+
dcc.Input(id="persistence-val", value=""),
572+
dcc.Input(id="persistence-key", value=""),
573+
html.Div(make_input(""), id="persisted-container"),
574+
html.Div(id="out"),
575+
]
576+
)
577+
578+
# this is not a good way to set persistence, as it doesn't allow you to
579+
# get the right initial value. Much better is to update the whole component
580+
# as we do in the previous test case... but it shouldn't break this way.
581+
@app.callback(
582+
Output("persisted", "persistence"), [Input("persistence-key", "value")]
583+
)
584+
def set_persistence(val):
585+
return val
586+
587+
@app.callback(Output("persisted", "value"), [Input("persistence-val", "value")])
588+
def set_value(val):
589+
return val
590+
591+
@app.callback(Output("out", "children"), [Input("persisted", "value")])
592+
def set_out(val):
593+
return val
594+
595+
dash_duo.start_server(app)
596+
597+
dash_duo.wait_for_text_to_equal("#out", "")
598+
599+
dash_duo.find_element("#persistence-key").send_keys("a")
600+
time.sleep(0.2)
601+
assert not dash_duo.get_logs()
602+
dash_duo.wait_for_text_to_equal("#out", "")
603+
dash_duo.find_element("#persistence-val").send_keys("alpaca")
604+
dash_duo.wait_for_text_to_equal("#out", "alpaca")
605+
606+
dash_duo.find_element("#persistence-key").send_keys("b")
607+
dash_duo.wait_for_text_to_equal("#out", "")
608+
dash_duo.clear_input("#persistence-val")
609+
dash_duo.find_element("#persistence-val").send_keys("artichoke")
610+
dash_duo.wait_for_text_to_equal("#out", "artichoke")
611+
612+
# no persistence, still goes back to original value
613+
dash_duo.clear_input("#persistence-key")
614+
dash_duo.wait_for_text_to_equal("#out", "")
615+
616+
# apricot and artichoke saved
617+
dash_duo.find_element("#persistence-key").send_keys("a")
618+
dash_duo.wait_for_text_to_equal("#out", "alpaca")
619+
dash_duo.find_element("#persistence-key").send_keys("b")
620+
assert not dash_duo.get_logs()
621+
dash_duo.wait_for_text_to_equal("#out", "artichoke")

0 commit comments

Comments
 (0)