Skip to content

Commit 9a60b0a

Browse files
committed
update test_scripts for webpack 5, and clean up its style & speed
1 parent 0a834bd commit 9a60b0a

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

tests/integration/test_scripts.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import time
22
import pytest
33

4-
from selenium.webdriver.common.by import By
5-
64
import dash_html_components as html
75
import dash_core_components as dcc
86

@@ -12,20 +10,26 @@
1210
from dash.exceptions import PreventUpdate
1311

1412

15-
def findSyncPlotlyJs(scripts):
16-
for script in scripts:
17-
if "dash_core_components/plotly" in script.get_attribute("src"):
18-
return script
13+
def get_script_sources(dash_duo):
14+
return [s.get_attribute("src") for s in dash_duo.find_elements("script")]
15+
16+
17+
def hasSyncPlotlyJs(dash_duo):
18+
return any("dash_core_components/plotly" in s for s in get_script_sources(dash_duo))
19+
20+
21+
def hasAsyncPlotlyJs(dash_duo):
22+
return any(
23+
"dash_core_components/async-plotlyjs" in s for s in get_script_sources(dash_duo)
24+
)
1925

2026

21-
def findAsyncPlotlyJs(scripts):
22-
for script in scripts:
23-
if "dash_core_components/async-plotlyjs" in script.get_attribute("src"):
24-
return script
27+
def hasWindowPlotly(dash_duo):
28+
return dash_duo.driver.execute_script("return !!window.Plotly")
2529

2630

2731
@pytest.mark.parametrize("is_eager", [True, False])
28-
def test_scripts(dash_duo, is_eager):
32+
def test_scri001_scripts(dash_duo, is_eager):
2933
app = Dash(__name__, eager_loading=is_eager)
3034
app.layout = html.Div([dcc.Graph(id="output", figure={"data": [{"y": [3, 1, 2]}]})])
3135

@@ -37,16 +41,18 @@ def test_scripts(dash_duo, is_eager):
3741
dev_tools_hot_reload=False,
3842
)
3943

40-
# Give time for the async dependency to be requested (if any)
41-
time.sleep(2)
44+
# Wait for the graph to appear
45+
dash_duo.find_element(".js-plotly-plot")
4246

43-
scripts = dash_duo.driver.find_elements(By.CSS_SELECTOR, "script")
47+
assert hasSyncPlotlyJs(dash_duo) is is_eager
4448

45-
assert (findSyncPlotlyJs(scripts) is None) is not is_eager
46-
assert (findAsyncPlotlyJs(scripts) is None) is is_eager
49+
# Webpack 5 deletes the script tag immediately after evaluating it
50+
# https://github.com/plotly/dash/pull/1685#issuecomment-877199466
51+
assert hasAsyncPlotlyJs(dash_duo) is False
52+
assert hasWindowPlotly(dash_duo) is True
4753

4854

49-
def test_scripts_on_request(dash_duo):
55+
def test_scri002_scripts_on_request(dash_duo):
5056
app = Dash(__name__, eager_loading=False)
5157
app.layout = html.Div(id="div", children=[html.Button(id="btn")])
5258

@@ -68,15 +74,16 @@ def load_chart(n_clicks):
6874
# Give time for the async dependency to be requested (if any)
6975
time.sleep(2)
7076

71-
scripts = dash_duo.driver.find_elements(By.CSS_SELECTOR, "script")
72-
assert findSyncPlotlyJs(scripts) is None
73-
assert findAsyncPlotlyJs(scripts) is None
77+
assert hasSyncPlotlyJs(dash_duo) is False
78+
assert hasAsyncPlotlyJs(dash_duo) is False
79+
assert hasWindowPlotly(dash_duo) is False
7480

7581
dash_duo.find_element("#btn").click()
7682

77-
# Give time for the async dependency to be requested (if any)
78-
time.sleep(2)
83+
# Wait for the graph to appear
84+
dash_duo.find_element(".js-plotly-plot")
7985

80-
scripts = dash_duo.driver.find_elements(By.CSS_SELECTOR, "script")
81-
assert findSyncPlotlyJs(scripts) is None
82-
assert findAsyncPlotlyJs(scripts) is not None
86+
assert hasSyncPlotlyJs(dash_duo) is False
87+
# Again, webpack 5 deletes the script tag immediately after evaluating it
88+
assert hasAsyncPlotlyJs(dash_duo) is False
89+
assert hasWindowPlotly(dash_duo) is True

0 commit comments

Comments
 (0)