Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 9a03708

Browse files
committed
Add test for Storage component.
1 parent 81d3724 commit 9a03708

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

test/test_integration.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import sys
77
import time
8+
import json
89
import pandas as pd
910

1011
import dash
@@ -531,7 +532,7 @@ def on_click(n_clicks):
531532
}
532533
]
533534
}
534-
535+
535536
self.startServer(app=app)
536537

537538
button_one = self.wait_for_element_by_css_selector('#one')
@@ -932,6 +933,7 @@ def update_output(start_date, end_date):
932933
end_date.click()
933934

934935
self.assertEquals(date_content.text, '1997-05-03 - 1997-05-04')
936+
935937
def test_interval(self):
936938
app = dash.Dash(__name__)
937939
app.layout = html.Div([
@@ -1141,3 +1143,60 @@ def render_content(click, prev_graph):
11411143
button.click()
11421144
time.sleep(2) # Wait for graph to re-render
11431145
self.snapshot('render-empty-graph')
1146+
1147+
def test_storage_component(self):
1148+
app = dash.Dash(__name__)
1149+
1150+
getter = 'return window.sessionStorage.getItem("{}");'
1151+
clicked_getter = getter.format('storage')
1152+
dummy_getter = getter.format('dummy')
1153+
dummy_data = 'Hello world'
1154+
1155+
app.layout = html.Div([
1156+
dcc.Storage(id='storage',
1157+
storage_type='session'),
1158+
html.Button('click me', id='btn'),
1159+
html.Button('clear', id='clear-btn'),
1160+
dcc.Storage(id='dummy',
1161+
storage_type='session',
1162+
data=dummy_data)
1163+
])
1164+
1165+
@app.callback(Output('storage', 'data'),
1166+
[Input('btn', 'n_clicks')],
1167+
[State('storage', 'data')])
1168+
def on_click(n_clicks, storage):
1169+
if n_clicks is None:
1170+
return
1171+
storage = storage or {}
1172+
return {'clicked': storage.get('clicked', 0) + 1}
1173+
1174+
@app.callback(Output('storage', 'clear_data'),
1175+
[Input('clear-btn', 'n_clicks')])
1176+
def on_clear(n_clicks):
1177+
if n_clicks is None:
1178+
return
1179+
return True
1180+
1181+
self.startServer(app)
1182+
1183+
time.sleep(1)
1184+
1185+
dummy = self.driver.execute_script(dummy_getter)
1186+
self.assertEqual(dummy_data, dummy)
1187+
1188+
click_btn = self.wait_for_element_by_css_selector('#btn')
1189+
clear_btn = self.wait_for_element_by_css_selector('#clear-btn')
1190+
1191+
for i in range(10):
1192+
click_btn.click()
1193+
time.sleep(0.5)
1194+
1195+
click_data = json.loads(self.driver.execute_script(clicked_getter))
1196+
self.assertEqual(i+1, click_data.get('clicked'))
1197+
1198+
clear_btn.click()
1199+
time.sleep(0.5)
1200+
1201+
cleared_data = self.driver.execute_script(clicked_getter)
1202+
self.assertTrue(cleared_data is None)

0 commit comments

Comments
 (0)