Skip to content

Commit 2df34a9

Browse files
committed
Add NavLink active tests
1 parent 9515ef1 commit 2df34a9

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

tests/test_alert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from dash_html_components import Div
44

55

6-
def test_gene001_simple_callback(dash_duo):
6+
def test_dbal001_alert_content(dash_duo):
77
app = Dash()
88

99
app.layout = Div([Alert("Test content", id="alert")])

tests/test_navlink.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from dash import Dash
2+
from dash.dependencies import Input, Output
3+
from dash_bootstrap_components import NavLink
4+
from dash_core_components import Location
5+
from dash_html_components import Div
6+
7+
8+
def test_dbnl001_auto_active(dash_duo):
9+
"""
10+
Checks that NavLink is able to automatically set active status based on the
11+
current location.
12+
"""
13+
app = Dash()
14+
15+
app.layout = Div(
16+
[
17+
# Location is required to fire events to History
18+
Location(id="url"),
19+
NavLink("Page 1", id="page-1-link", href="/page-1", active=True),
20+
NavLink("Page 2", id="page-2-link", href="/page-2", active=False),
21+
NavLink(
22+
"Page 3", id="page-3-link", href="/page-3", active="partial"
23+
),
24+
NavLink(
25+
"Page 3 - extra",
26+
id="page-3-extra-link",
27+
href="/page-3/extra",
28+
active="exact",
29+
),
30+
Div(id="content"),
31+
]
32+
)
33+
34+
dash_duo.start_server(app)
35+
36+
assert "active" in dash_duo.wait_for_element_by_id(
37+
"page-1-link"
38+
).get_attribute("class")
39+
assert "active" not in dash_duo.wait_for_element_by_id(
40+
"page-2-link"
41+
).get_attribute("class")
42+
assert "active" not in dash_duo.wait_for_element_by_id(
43+
"page-3-link"
44+
).get_attribute("class")
45+
assert "active" not in dash_duo.wait_for_element_by_id(
46+
"page-3-extra-link"
47+
).get_attribute("class")
48+
49+
dash_duo.wait_for_element_by_id("page-3-link").click()
50+
51+
assert "active" in dash_duo.wait_for_element_by_id(
52+
"page-1-link"
53+
).get_attribute("class")
54+
assert "active" not in dash_duo.wait_for_element_by_id(
55+
"page-2-link"
56+
).get_attribute("class")
57+
assert "active" in dash_duo.wait_for_element_by_id(
58+
"page-3-link"
59+
).get_attribute("class")
60+
assert "active" not in dash_duo.wait_for_element_by_id(
61+
"page-3-extra-link"
62+
).get_attribute("class")
63+
64+
dash_duo.wait_for_element_by_id("page-3-extra-link").click()
65+
66+
assert "active" in dash_duo.wait_for_element_by_id(
67+
"page-1-link"
68+
).get_attribute("class")
69+
assert "active" not in dash_duo.wait_for_element_by_id(
70+
"page-2-link"
71+
).get_attribute("class")
72+
assert "active" in dash_duo.wait_for_element_by_id(
73+
"page-3-link"
74+
).get_attribute("class")
75+
assert "active" in dash_duo.wait_for_element_by_id(
76+
"page-3-extra-link"
77+
).get_attribute("class")
78+
79+
80+
def test_dbnl_002_manual_active(dash_duo):
81+
"""
82+
Update active status using a callback.
83+
"""
84+
app = Dash()
85+
86+
app.layout = Div(
87+
[
88+
# Location is required to fire events to History
89+
Location(id="url"),
90+
NavLink("Page 1", id="page-1-link", href="/page-1"),
91+
NavLink("Page 2", id="page-2-link", href="/page-2"),
92+
NavLink("Page 3", id="page-3-link", href="/page-3"),
93+
Div(id="content"),
94+
]
95+
)
96+
97+
@app.callback(
98+
[Output("page-{}-link".format(i), "active") for i in range(1, 4)],
99+
Input("url", "pathname"),
100+
)
101+
def set_active(pathname):
102+
return [pathname == "/page-{}".format(i) for i in range(1, 4)]
103+
104+
dash_duo.start_server(app)
105+
106+
assert all(
107+
[
108+
"active"
109+
not in dash_duo.wait_for_element_by_id(
110+
"page-{}-link".format(i)
111+
).get_attribute("class")
112+
for i in range(1, 4)
113+
]
114+
)
115+
116+
dash_duo.wait_for_element_by_id("page-1-link").click()
117+
118+
assert "active" in dash_duo.wait_for_element_by_id(
119+
"page-1-link"
120+
).get_attribute("class")
121+
assert "active" not in dash_duo.wait_for_element_by_id(
122+
"page-2-link"
123+
).get_attribute("class")
124+
assert "active" not in dash_duo.wait_for_element_by_id(
125+
"page-3-link"
126+
).get_attribute("class")
127+
128+
dash_duo.wait_for_element_by_id("page-3-link").click()
129+
130+
assert "active" not in dash_duo.wait_for_element_by_id(
131+
"page-1-link"
132+
).get_attribute("class")
133+
assert "active" not in dash_duo.wait_for_element_by_id(
134+
"page-2-link"
135+
).get_attribute("class")
136+
assert "active" in dash_duo.wait_for_element_by_id(
137+
"page-3-link"
138+
).get_attribute("class")

0 commit comments

Comments
 (0)