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

Commit 011d384

Browse files
committed
Add test for Storage component.
1 parent 02320a8 commit 011d384

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

test/test_integration.py

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

1011
import dash
1112
from dash.dependencies import Input, Output, State
1213
import dash_html_components as html
1314
import dash_core_components as dcc
1415
import dash_table_experiments as dt
15-
from selenium import webdriver
1616
from selenium.webdriver.common.keys import Keys
1717
from selenium.common.exceptions import InvalidElementStateException
1818

@@ -708,6 +708,7 @@ def update_output(start_date, end_date):
708708
end_date.click()
709709

710710
self.assertEquals(date_content.text, '1997-05-03 - 1997-05-04')
711+
711712
def test_interval(self):
712713
app = dash.Dash(__name__)
713714
app.layout = html.Div([
@@ -792,3 +793,60 @@ def test_confirm_dialog_provider(self):
792793
])
793794

794795
self._test_confirm(app, 'ConfirmDialogProvider')
796+
797+
def test_storage_component(self):
798+
app = dash.Dash(__name__)
799+
800+
getter = 'return window.sessionStorage.getItem("{}");'
801+
clicked_getter = getter.format('storage')
802+
dummy_getter = getter.format('dummy')
803+
dummy_data = 'Hello world'
804+
805+
app.layout = html.Div([
806+
dcc.Storage(id='storage',
807+
storage_type='session'),
808+
html.Button('click me', id='btn'),
809+
html.Button('clear', id='clear-btn'),
810+
dcc.Storage(id='dummy',
811+
storage_type='session',
812+
data=dummy_data)
813+
])
814+
815+
@app.callback(Output('storage', 'data'),
816+
[Input('btn', 'n_clicks')],
817+
[State('storage', 'data')])
818+
def on_click(n_clicks, storage):
819+
if n_clicks is None:
820+
return
821+
storage = storage or {}
822+
return {'clicked': storage.get('clicked', 0) + 1}
823+
824+
@app.callback(Output('storage', 'clear_data'),
825+
[Input('clear-btn', 'n_clicks')])
826+
def on_clear(n_clicks):
827+
if n_clicks is None:
828+
return
829+
return True
830+
831+
self.startServer(app)
832+
833+
time.sleep(1)
834+
835+
dummy = self.driver.execute_script(dummy_getter)
836+
self.assertEqual(dummy_data, dummy)
837+
838+
click_btn = self.wait_for_element_by_css_selector('#btn')
839+
clear_btn = self.wait_for_element_by_css_selector('#clear-btn')
840+
841+
for i in range(10):
842+
click_btn.click()
843+
time.sleep(0.5)
844+
845+
click_data = json.loads(self.driver.execute_script(clicked_getter))
846+
self.assertEqual(i+1, click_data.get('clicked'))
847+
848+
clear_btn.click()
849+
time.sleep(0.5)
850+
851+
cleared_data = self.driver.execute_script(clicked_getter)
852+
self.assertTrue(cleared_data is None)

0 commit comments

Comments
 (0)