Skip to content

Commit ac5ec16

Browse files
tcbegleyann-marie-wardAnnMarieW
authored
R snippet callbacks (#632)
* V1 of testing R example callbacks * lint * Refactor callback testing * Added callback tests * Updated R snippets * Updates based on review comments * updated collapse and toast tests. fixed julia callback * changed timeout to 4 * changed timeout to 4 (oops missed one) Co-authored-by: Ann Marie Ward <amward@fastmail.us> Co-authored-by: AnnMarieW <amward@fastmail.com>
1 parent 889f024 commit ac5ec16

25 files changed

+1074
-8
lines changed

docs/components_page/components/__tests__/helpers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22

3+
from .wrappers import JL_WRAPPER, R_WRAPPER
4+
35

46
def py_source_to_app(py_source, env):
57
"""
@@ -47,3 +49,19 @@ def rename_variable(snippet_path, suffix, variable, assign_op="="):
4749
new_lines.append(line)
4850

4951
return "\n".join(new_lines)
52+
53+
54+
def load_r_app(path, component_name):
55+
return R_WRAPPER.format(
56+
snippet=path.read_text(),
57+
components=component_name,
58+
port=8050,
59+
)
60+
61+
62+
def load_jl_app(path, component_name):
63+
return JL_WRAPPER.format(
64+
snippet=path.read_text(),
65+
components=component_name,
66+
port=8050,
67+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Testing of callbacks in non-Python Alert snippets.
3+
"""
4+
from pathlib import Path
5+
6+
import dash.testing.wait as wait
7+
8+
from .helpers import load_jl_app, load_r_app
9+
10+
HERE = Path(__file__).parent
11+
12+
13+
def test_r_dismiss(dashr):
14+
r_app = load_r_app((HERE.parent / "alert" / "dismiss.R"), "alert")
15+
dashr.start_server(r_app)
16+
check_dismiss_callbacks(dashr)
17+
18+
19+
def test_jl_dismiss(dashjl):
20+
jl_app = load_jl_app((HERE.parent / "alert" / "dismiss.jl"), "alert")
21+
dashjl.start_server(jl_app)
22+
check_dismiss_callbacks(dashjl)
23+
24+
25+
def check_dismiss_callbacks(runner):
26+
assert runner.find_element("#alert-fade") != []
27+
runner.find_element("#alert-toggle-fade").click()
28+
wait.until(
29+
lambda: runner.find_elements("#alert-fade") == [],
30+
timeout=4,
31+
)
32+
33+
assert runner.find_element("#alert-no-fade") != []
34+
runner.find_element("#alert-toggle-no-fade").click()
35+
wait.until(
36+
lambda: runner.find_elements("#alert-no-fade") == [],
37+
timeout=4,
38+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Testing of callbacks in non-Python Button snippets.
3+
"""
4+
from pathlib import Path
5+
6+
import dash.testing.wait as wait
7+
8+
from .helpers import load_jl_app, load_r_app
9+
10+
HERE = Path(__file__).parent
11+
12+
13+
def test_r_button(dashr):
14+
r_app = load_r_app((HERE.parent / "button" / "usage.R"), "button")
15+
dashr.start_server(r_app)
16+
check_button_callbacks(dashr)
17+
18+
19+
def test_jl_button(dashjl):
20+
jl_app = load_jl_app((HERE.parent / "button" / "usage.jl"), "button")
21+
dashjl.start_server(jl_app)
22+
check_button_callbacks(dashjl)
23+
24+
25+
def check_button_callbacks(runner):
26+
runner.find_element("#example-button").click()
27+
wait.until(
28+
lambda: runner.find_element("#example-output").text
29+
== "Clicked 1 times.",
30+
timeout=4,
31+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Testing of callbacks in non-Python button_group snippets.
3+
"""
4+
from pathlib import Path
5+
6+
import dash.testing.wait as wait
7+
8+
from .helpers import load_jl_app, load_r_app
9+
10+
HERE = Path(__file__).parent
11+
12+
13+
def test_r_button_group(dashr):
14+
r_app = load_r_app(
15+
(HERE.parent / "button_group" / "radios.R"), "button_group"
16+
)
17+
dashr.start_server(r_app)
18+
check_button_group_callbacks(dashr)
19+
20+
21+
def test_jl_button_group(dashjl):
22+
jl_app = load_jl_app(
23+
(HERE.parent / "button_group" / "radios.jl"), "button_group"
24+
)
25+
dashjl.start_server(jl_app)
26+
check_button_group_callbacks(dashjl)
27+
28+
29+
def check_button_group_callbacks(runner):
30+
31+
runner.find_element(
32+
"label[for='_dbcprivate_radioitems_radios_input_2']"
33+
).click()
34+
wait.until(
35+
lambda: runner.find_element("#output").text == "Selected value: 2",
36+
timeout=4,
37+
)
38+
39+
runner.find_element(
40+
"label[for='_dbcprivate_radioitems_radios_input_3']"
41+
).click()
42+
wait.until(
43+
lambda: runner.find_element("#output").text == "Selected value: 3",
44+
timeout=4,
45+
)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""
2+
Testing of callbacks in non-Python Collapse snippets.
3+
"""
4+
from pathlib import Path
5+
6+
import dash.testing.wait as wait
7+
8+
from .helpers import load_jl_app, load_r_app
9+
10+
HERE = Path(__file__).parent
11+
12+
13+
# simple
14+
def test_r_collapse_simple(dashr):
15+
r_app = load_r_app((HERE.parent / "collapse" / "simple.R"), "collapse")
16+
dashr.start_server(r_app)
17+
check_collapse_simple_callbacks(dashr)
18+
19+
20+
def test_jl_collapse_simple(dashjl):
21+
jl_app = load_jl_app((HERE.parent / "collapse" / "simple.jl"), "collapse")
22+
dashjl.start_server(jl_app)
23+
check_collapse_simple_callbacks(dashjl)
24+
25+
26+
def check_collapse_simple_callbacks(runner):
27+
runner.find_element("#collapse-button").click()
28+
wait.until(
29+
lambda: runner.find_elements("#collapse") != [],
30+
timeout=4,
31+
)
32+
33+
34+
# --------------------------------------------
35+
# multiple
36+
37+
38+
def test_r_collapse_multiple(dashr):
39+
r_app = load_r_app((HERE.parent / "collapse" / "multiple.R"), "collapses")
40+
dashr.start_server(r_app)
41+
check_collapse_multiple_callbacks(dashr)
42+
43+
44+
def test_jl_collapse_multiple(dashjl):
45+
jl_app = load_jl_app(
46+
(HERE.parent / "collapse" / "multiple.jl"), "collapses"
47+
)
48+
dashjl.start_server(jl_app)
49+
check_collapse_multiple_callbacks(dashjl)
50+
51+
52+
def check_collapse_multiple_callbacks(runner):
53+
54+
runner.find_element("#left").click()
55+
wait.until(
56+
lambda: runner.find_element("#left-collapse").get_attribute("class")
57+
== "collapse",
58+
timeout=4,
59+
)
60+
61+
runner.find_element("#right").click()
62+
wait.until(
63+
lambda: runner.find_element("#right-collapse").get_attribute("class")
64+
== "collapse",
65+
timeout=4,
66+
)
67+
68+
runner.find_element("#both").click()
69+
wait.until(
70+
lambda: runner.find_element("#right-collapse").get_attribute("class")
71+
== "collapse show",
72+
timeout=4,
73+
)
74+
75+
76+
# --------------------------------------------
77+
# accordion
78+
79+
80+
def test_r_collapse_accordion(dashr):
81+
r_app = load_r_app((HERE.parent / "collapse" / "accordion.R"), "accordion")
82+
dashr.start_server(r_app)
83+
check_collapse_accordion_callbacks(dashr)
84+
85+
86+
def test_jl_collapse_accordion(dashjl):
87+
jl_app = load_jl_app(
88+
(HERE.parent / "collapse" / "accordion.jl"), "accordion"
89+
)
90+
dashjl.start_server(jl_app)
91+
check_collapse_accordion_callbacks(dashjl)
92+
93+
94+
def check_collapse_accordion_callbacks(runner):
95+
96+
runner.find_element("#group-1-toggle").click()
97+
wait.until(
98+
lambda: runner.find_element("#collapse-1").get_attribute("class")
99+
== "collapse show",
100+
timeout=4,
101+
)
102+
103+
runner.find_element("#group-2-toggle").click()
104+
wait.until(
105+
lambda: runner.find_element("#collapse-2").get_attribute("class")
106+
== "collapse show",
107+
timeout=4,
108+
)
109+
110+
runner.find_element("#group-3-toggle").click()
111+
wait.until(
112+
lambda: runner.find_element("#collapse-3").get_attribute("class")
113+
== "collapse show",
114+
timeout=4,
115+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Testing of callbacks in non-Python Dropdown snippets.
3+
"""
4+
from pathlib import Path
5+
6+
import dash.testing.wait as wait
7+
8+
from .helpers import load_jl_app, load_r_app
9+
10+
HERE = Path(__file__).parent
11+
12+
13+
def test_r_dropdown(dashr):
14+
r_app = load_r_app((HERE.parent / "dropdown" / "menu_items.R"), "dropdown")
15+
dashr.start_server(r_app)
16+
check_dropdown_callbacks(dashr)
17+
18+
19+
def test_jl_dropdown(dashjl):
20+
jl_app = load_jl_app(
21+
(HERE.parent / "dropdown" / "menu_items.jl"), "dropdown"
22+
)
23+
dashjl.start_server(jl_app)
24+
check_dropdown_callbacks(dashjl)
25+
26+
27+
def check_dropdown_callbacks(runner):
28+
runner.find_element(".btn").click()
29+
30+
runner.find_element("#dropdown-button").click()
31+
32+
wait.until(
33+
lambda: runner.find_element("#item-clicks").text
34+
== "Button clicked 1 times.",
35+
timeout=4,
36+
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Testing of callbacks in non-Python Fade snippets.
3+
"""
4+
from pathlib import Path
5+
6+
import dash.testing.wait as wait
7+
8+
from .helpers import load_jl_app, load_r_app
9+
10+
HERE = Path(__file__).parent
11+
12+
13+
def test_r_fade(dashr):
14+
r_app = load_r_app((HERE.parent / "fade" / "fade.R"), "fade")
15+
dashr.start_server(r_app)
16+
check_fade_callbacks(dashr)
17+
18+
19+
def test_jl_fade(dashjl):
20+
jl_app = load_jl_app((HERE.parent / "fade" / "fade.jl"), "fade")
21+
dashjl.start_server(jl_app)
22+
check_fade_callbacks(dashjl)
23+
24+
25+
def check_fade_callbacks(runner):
26+
assert runner.find_element("#fade") != []
27+
runner.find_element("#fade-button").click()
28+
wait.until(
29+
lambda: runner.find_element("#fade").get_attribute("style")
30+
== "visibility: hidden;",
31+
timeout=4,
32+
)
33+
34+
35+
def test_r_fade_transition(dashr):
36+
r_app = load_r_app((HERE.parent / "fade" / "transition.R"), "fade")
37+
dashr.start_server(r_app)
38+
check_fade_transition_callbacks(dashr)
39+
40+
41+
def test_jl_fade_transition(dashjl):
42+
jl_app = load_jl_app((HERE.parent / "fade" / "transition.jl"), "fade")
43+
dashjl.start_server(jl_app)
44+
check_fade_transition_callbacks(dashjl)
45+
46+
47+
def check_fade_transition_callbacks(runner):
48+
wait.until(
49+
lambda: len(runner.find_elements("#fade-transition")) == 1,
50+
timeout=4,
51+
)
52+
53+
runner.find_element("#fade-transition-button").click()
54+
wait.until(
55+
lambda: runner.find_element("#fade-transition").get_attribute("style")
56+
== "transition: opacity 2000ms ease 0s; visibility: hidden;",
57+
timeout=5,
58+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Testing of callbacks in non-Python Form snippets.
3+
"""
4+
from pathlib import Path
5+
6+
import dash.testing.wait as wait
7+
8+
from .helpers import load_jl_app, load_r_app
9+
10+
HERE = Path(__file__).parent
11+
12+
13+
def test_r_form(dashr):
14+
r_app = load_r_app((HERE.parent / "form" / "feedback.R"), "email_input")
15+
dashr.start_server(r_app)
16+
check_form_callbacks(dashr)
17+
18+
19+
def test_jl_form(dashjl):
20+
jl_app = load_jl_app((HERE.parent / "form" / "feedback.jl"), "email_input")
21+
dashjl.start_server(jl_app)
22+
check_form_callbacks(dashjl)
23+
24+
25+
def check_form_callbacks(runner):
26+
runner.find_element("#email-input").send_keys("x")
27+
wait.until(
28+
lambda: runner.find_element("#email-input").get_attribute("class")
29+
== "is-invalid form-control",
30+
timeout=4,
31+
)
32+
33+
runner.find_element("#email-input").send_keys("x@gmail.com")
34+
wait.until(
35+
lambda: runner.find_element("#email-input").get_attribute("class")
36+
== "is-valid form-control",
37+
timeout=4,
38+
)

0 commit comments

Comments
 (0)