Skip to content

Commit 2669aa9

Browse files
committed
modernize flaky request hooks test
and remove a redundant one
1 parent 708f36e commit 2669aa9

File tree

3 files changed

+194
-274
lines changed

3 files changed

+194
-274
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import json
2+
3+
import dash_html_components as html
4+
import dash_core_components as dcc
5+
from dash import Dash
6+
from dash.dependencies import Output, Input
7+
8+
9+
def test_rdrh001_request_hooks(dash_duo):
10+
app = Dash(__name__)
11+
12+
app.index_string = """<!DOCTYPE html>
13+
<html>
14+
<head>
15+
{%metas%}
16+
<title>{%title%}</title>
17+
{%favicon%}
18+
{%css%}
19+
</head>
20+
<body>
21+
<div>Testing custom DashRenderer</div>
22+
{%app_entry%}
23+
<footer>
24+
{%config%}
25+
{%scripts%}
26+
<script id="_dash-renderer" type"application/json">
27+
const renderer = new DashRenderer({
28+
request_pre: (payload) => {
29+
var output = document.getElementById('output-pre')
30+
var outputPayload = document.getElementById('output-pre-payload')
31+
if(output) {
32+
output.innerHTML = 'request_pre changed this text!';
33+
}
34+
if(outputPayload) {
35+
outputPayload.innerHTML = JSON.stringify(payload);
36+
}
37+
},
38+
request_post: (payload, response) => {
39+
var output = document.getElementById('output-post')
40+
var outputPayload = document.getElementById('output-post-payload')
41+
var outputResponse = document.getElementById('output-post-response')
42+
if(output) {
43+
output.innerHTML = 'request_post changed this text!';
44+
}
45+
if(outputPayload) {
46+
outputPayload.innerHTML = JSON.stringify(payload);
47+
}
48+
if(outputResponse) {
49+
outputResponse.innerHTML = JSON.stringify(response);
50+
}
51+
}
52+
})
53+
</script>
54+
</footer>
55+
<div>With request hooks</div>
56+
</body>
57+
</html>"""
58+
59+
app.layout = html.Div(
60+
[
61+
dcc.Input(id="input", value="initial value"),
62+
html.Div(
63+
html.Div(
64+
[
65+
html.Div(id="output-1"),
66+
html.Div(id="output-pre"),
67+
html.Div(id="output-pre-payload"),
68+
html.Div(id="output-post"),
69+
html.Div(id="output-post-payload"),
70+
html.Div(id="output-post-response"),
71+
]
72+
)
73+
),
74+
]
75+
)
76+
77+
@app.callback(Output("output-1", "children"), [Input("input", "value")])
78+
def update_output(value):
79+
return value
80+
81+
dash_duo.start_server(app)
82+
83+
_in = dash_duo.find_element("#input")
84+
dash_duo.clear_input(_in)
85+
86+
_in.send_keys("fire request hooks")
87+
88+
dash_duo.wait_for_text_to_equal("#output-1", "fire request hooks")
89+
dash_duo.wait_for_text_to_equal("#output-pre", "request_pre changed this text!")
90+
dash_duo.wait_for_text_to_equal("#output-post", "request_post changed this text!")
91+
92+
assert json.loads(dash_duo.find_element("#output-pre-payload").text) == {
93+
"output": "output-1.children",
94+
"outputs": {"id": "output-1", "property": "children"},
95+
"changedPropIds": ["input.value"],
96+
"inputs": [
97+
{"id": "input", "property": "value", "value": "fire request hooks"}
98+
],
99+
}
100+
101+
assert json.loads(dash_duo.find_element("#output-post-payload").text) == {
102+
"output": "output-1.children",
103+
"outputs": {"id": "output-1", "property": "children"},
104+
"changedPropIds": ["input.value"],
105+
"inputs": [
106+
{"id": "input", "property": "value", "value": "fire request hooks"}
107+
],
108+
}
109+
110+
assert json.loads(dash_duo.find_element("#output-post-response").text) == {
111+
"output-1": {"children": "fire request hooks"}
112+
}
113+
114+
dash_duo.percy_snapshot(name="request-hooks render")
115+
116+
117+
def test_rdrh002_with_custom_renderer_interpolated(dash_duo):
118+
119+
renderer = """
120+
<script id="_dash-renderer" type="application/javascript">
121+
console.log('firing up a custom renderer!')
122+
const renderer = new DashRenderer({
123+
request_pre: () => {
124+
var output = document.getElementById('output-pre')
125+
if(output) {
126+
output.innerHTML = 'request_pre was here!';
127+
}
128+
},
129+
request_post: () => {
130+
var output = document.getElementById('output-post')
131+
if(output) {
132+
output.innerHTML = 'request_post!!!';
133+
}
134+
}
135+
})
136+
</script>
137+
"""
138+
139+
class CustomDash(Dash):
140+
def interpolate_index(self, **kwargs):
141+
return """<!DOCTYPE html>
142+
<html>
143+
<head>
144+
<title>My App</title>
145+
</head>
146+
<body>
147+
148+
<div id="custom-header">My custom header</div>
149+
{app_entry}
150+
{config}
151+
{scripts}
152+
{renderer}
153+
<div id="custom-footer">My custom footer</div>
154+
</body>
155+
</html>""".format(
156+
app_entry=kwargs["app_entry"],
157+
config=kwargs["config"],
158+
scripts=kwargs["scripts"],
159+
renderer=renderer,
160+
)
161+
162+
app = CustomDash()
163+
164+
app.layout = html.Div(
165+
[
166+
dcc.Input(id="input", value="initial value"),
167+
html.Div(
168+
html.Div(
169+
[
170+
html.Div(id="output-1"),
171+
html.Div(id="output-pre"),
172+
html.Div(id="output-post"),
173+
]
174+
)
175+
),
176+
]
177+
)
178+
179+
@app.callback(Output("output-1", "children"), [Input("input", "value")])
180+
def update_output(value):
181+
return value
182+
183+
dash_duo.start_server(app)
184+
185+
input1 = dash_duo.find_element("#input")
186+
dash_duo.clear_input(input1)
187+
188+
input1.send_keys("fire request hooks")
189+
190+
dash_duo.wait_for_text_to_equal("#output-1", "fire request hooks")
191+
assert dash_duo.find_element("#output-pre").text == "request_pre was here!"
192+
assert dash_duo.find_element("#output-post").text == "request_post!!!"
193+
194+
dash_duo.percy_snapshot(name="request-hooks interpolated")

tests/integration/test_integration.py

Lines changed: 0 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -222,158 +222,6 @@ def create_layout():
222222
assert dash_duo.find_element("#a").text == "Hello World"
223223

224224

225-
def test_inin014_with_custom_renderer(dash_duo):
226-
app = Dash(__name__)
227-
228-
app.index_string = """<!DOCTYPE html>
229-
<html>
230-
<head>
231-
{%metas%}
232-
<title>{%title%}</title>
233-
{%favicon%}
234-
{%css%}
235-
</head>
236-
<body>
237-
<div>Testing custom DashRenderer</div>
238-
{%app_entry%}
239-
<footer>
240-
{%config%}
241-
{%scripts%}
242-
<script id="_dash-renderer" type="application/javascript">
243-
console.log('firing up a custom renderer!')
244-
const renderer = new DashRenderer({
245-
request_pre: () => {
246-
var output = document.getElementById('output-pre')
247-
if(output) {
248-
output.innerHTML = 'request_pre!!!';
249-
}
250-
},
251-
request_post: () => {
252-
var output = document.getElementById('output-post')
253-
if(output) {
254-
output.innerHTML = 'request_post ran!';
255-
}
256-
}
257-
})
258-
</script>
259-
</footer>
260-
<div>With request hooks</div>
261-
</body>
262-
</html>"""
263-
264-
app.layout = html.Div(
265-
[
266-
dcc.Input(id="input", value="initial value"),
267-
html.Div(
268-
html.Div(
269-
[
270-
html.Div(id="output-1"),
271-
html.Div(id="output-pre"),
272-
html.Div(id="output-post"),
273-
]
274-
)
275-
),
276-
]
277-
)
278-
279-
@app.callback(Output("output-1", "children"), [Input("input", "value")])
280-
def update_output(value):
281-
return value
282-
283-
dash_duo.start_server(app)
284-
285-
input1 = dash_duo.find_element("#input")
286-
dash_duo.clear_input(input1)
287-
288-
input1.send_keys("fire request hooks")
289-
290-
dash_duo.wait_for_text_to_equal("#output-1", "fire request hooks")
291-
assert dash_duo.find_element("#output-pre").text == "request_pre!!!"
292-
assert dash_duo.find_element("#output-post").text == "request_post ran!"
293-
294-
dash_duo.percy_snapshot(name="request-hooks intg")
295-
296-
297-
def test_inin015_with_custom_renderer_interpolated(dash_duo):
298-
299-
renderer = """
300-
<script id="_dash-renderer" type="application/javascript">
301-
console.log('firing up a custom renderer!')
302-
const renderer = new DashRenderer({
303-
request_pre: () => {
304-
var output = document.getElementById('output-pre')
305-
if(output) {
306-
output.innerHTML = 'request_pre was here!';
307-
}
308-
},
309-
request_post: () => {
310-
var output = document.getElementById('output-post')
311-
if(output) {
312-
output.innerHTML = 'request_post!!!';
313-
}
314-
}
315-
})
316-
</script>
317-
"""
318-
319-
class CustomDash(Dash):
320-
def interpolate_index(self, **kwargs):
321-
return """<!DOCTYPE html>
322-
<html>
323-
<head>
324-
<title>My App</title>
325-
</head>
326-
<body>
327-
328-
<div id="custom-header">My custom header</div>
329-
{app_entry}
330-
{config}
331-
{scripts}
332-
{renderer}
333-
<div id="custom-footer">My custom footer</div>
334-
</body>
335-
</html>""".format(
336-
app_entry=kwargs["app_entry"],
337-
config=kwargs["config"],
338-
scripts=kwargs["scripts"],
339-
renderer=renderer,
340-
)
341-
342-
app = CustomDash()
343-
344-
app.layout = html.Div(
345-
[
346-
dcc.Input(id="input", value="initial value"),
347-
html.Div(
348-
html.Div(
349-
[
350-
html.Div(id="output-1"),
351-
html.Div(id="output-pre"),
352-
html.Div(id="output-post"),
353-
]
354-
)
355-
),
356-
]
357-
)
358-
359-
@app.callback(Output("output-1", "children"), [Input("input", "value")])
360-
def update_output(value):
361-
return value
362-
363-
dash_duo.start_server(app)
364-
365-
input1 = dash_duo.find_element("#input")
366-
dash_duo.clear_input(input1)
367-
368-
input1.send_keys("fire request hooks")
369-
370-
dash_duo.wait_for_text_to_equal("#output-1", "fire request hooks")
371-
assert dash_duo.find_element("#output-pre").text == "request_pre was here!"
372-
assert dash_duo.find_element("#output-post").text == "request_post!!!"
373-
374-
dash_duo.percy_snapshot(name="request-hooks interpolated")
375-
376-
377225
def test_inin017_late_component_register(dash_duo):
378226
app = Dash()
379227

0 commit comments

Comments
 (0)