Skip to content

Commit 7e57fce

Browse files
committed
Finishing visibility tests
1 parent 6de5bde commit 7e57fce

File tree

6 files changed

+100
-23
lines changed

6 files changed

+100
-23
lines changed

examples/dynamic-tabs/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/dynamic-tabs/requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

shiny/api-examples/nav_insert/app-express.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
ui.input_action_button("addFoo", "Add New 'Foo' tab")
99

1010
with ui.navset_tab(id="set"):
11-
with ui.nav_panel("Hello"):
11+
with ui.nav_panel("Hello", value="Hello"):
1212
"This is the hello tab"
1313
with ui.nav_panel("Foo", value="Foo"):
1414
"This is the Foo tab"
@@ -22,9 +22,12 @@
2222
@reactive.event(input.add)
2323
def _():
2424
id = "Dynamic-" + str(input.add())
25+
with ui.hold() as new_panel:
26+
with ui.nav_panel(id, value=id):
27+
pass
2528
ui.nav_insert(
2629
"tabs",
27-
ui.nav_panel(id, value=id),
30+
new_panel,
2831
target="s2",
2932
position="before",
3033
)
@@ -38,9 +41,12 @@ def _():
3841
@reactive.event(input.addFoo)
3942
def _():
4043
n = str(input.addFoo())
44+
with ui.hold() as new_panel:
45+
with ui.nav_panel("Foo-" + n, value="Foo"):
46+
"This is the new Foo-" + n + " tab"
4147
ui.nav_insert(
4248
"tabs",
43-
ui.nav_panel("Foo-" + n, "This is the new Foo-" + n + " tab", value="Foo"),
49+
new_panel[0],
4450
target="Menu",
4551
position="before",
4652
select=True,

shiny/ui/_navs_dynamic.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ def nav_insert(
3636
Parameters
3737
----------
3838
id
39-
The ``id`` of the relevant navigation container (i.e., ``navset_*()`` object).
39+
The `id` of the relevant navigation container (i.e., `navset_*()` object).
4040
nav_panel
4141
The navigation item to insert (typically a :func:`~shiny.ui.nav_panel` or
4242
:func:`~shiny.ui.nav_menu`). A :func:`~shiny.ui.nav_menu` isn't allowed when the
43-
``target`` references an :func:`~shiny.ui.nav_menu` (or an item within it). A
44-
string is only allowed when the ``target`` references a
43+
`target` references an :func:`~shiny.ui.nav_menu` (or an item within it). A
44+
string is only allowed when the `target` references a
4545
:func:`~shiny.ui.nav_menu`.
4646
target
47-
The ``value`` of an existing :func:`shiny.ui.nav` item, next to which tab will
48-
be added. Can also be ``None``; see ``position``.
47+
The `value` of an existing :func:`shiny.ui.nav` item, next to which tab will
48+
be added. Can also be `None`; see `position`.
4949
position
5050
The position of the new nav item relative to the target nav item. If
51-
``target=None``, then ``"before"`` means the new nav item should be inserted at
52-
the head of the navlist, and ``"after"`` is the end.
51+
`target=None`, then `"before"` means the new nav item should be inserted at
52+
the head of the navlist, and `"after"` is the end.
5353
select
5454
Whether the nav item should be selected upon insertion.
5555
session
@@ -92,17 +92,16 @@ def nav_insert(
9292
session._send_message_sync({"shiny-insert-tab": msg})
9393

9494

95-
@add_example()
9695
def nav_remove(id: str, target: str, session: Optional[Session] = None) -> None:
9796
"""
9897
Remove a nav item from a navigation container.
9998
10099
Parameters
101100
----------
102101
id
103-
The ``id`` of the relevant navigation container (i.e., ``navset_*()`` object).
102+
The `id` of the relevant navigation container (i.e., `navset_*()` object).
104103
target
105-
The ``value`` of an existing :func:`shiny.ui.nav_panel` item to remove.
104+
The `value` of an existing :func:`shiny.ui.nav_panel` item to remove.
106105
session
107106
A :class:`~shiny.Session` instance. If not provided, it is inferred via
108107
:func:`~shiny.session.get_current_session`.
@@ -171,17 +170,16 @@ def nav_show(
171170
session._send_message_sync({"shiny-change-tab-visibility": msg})
172171

173172

174-
@add_example()
175173
def nav_hide(id: str, target: str, session: Optional[Session] = None) -> None:
176174
"""
177175
Hide a navigation item
178176
179177
Parameters
180178
----------
181179
id
182-
The ``id`` of the relevant navigation container (i.e., ``navset_*()`` object).
180+
The `id` of the relevant navigation container (i.e., `navset_*()` object).
183181
target
184-
The ``value`` of an existing :func:`shiny.ui.nav` item to hide.
182+
The `value` of an existing :func:`shiny.ui.nav` item to hide.
185183
session
186184
A :class:`~shiny.Session` instance. If not provided, it is inferred via
187185
:func:`~shiny.session.get_current_session`.

examples/dynamic-tabs/app.py renamed to tests/playwright/shiny/components/dynamic_navs/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
ui.input_action_button("showMenu", "Show 'Static' nav_menu"),
1212
),
1313
ui.navset_tab(
14-
ui.nav_panel("Hello", "This is the hello tab"),
15-
ui.nav_panel("Foo", "This is the Foo tab", value="Foo"),
14+
ui.nav_panel("Hello", "This is the hello tab", value="Hello"),
15+
ui.nav_panel("Foo", "Foo", value="Foo"),
1616
ui.nav_menu(
1717
"Static",
1818
ui.nav_panel("Static 1", "Static 1", value="s1"),
@@ -47,7 +47,7 @@ def _():
4747
n = str(input.addFoo())
4848
ui.nav_insert(
4949
"tabs",
50-
ui.nav_panel("Foo-" + n, "This is the new Foo-" + n + " tab", value="Foo"),
50+
ui.nav_panel("Foo-" + n, "Foo-" + n, value="Foo"),
5151
target="Menu",
5252
position="before",
5353
select=True,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# import pytest
2+
import pytest
3+
from playwright.sync_api import Page, expect
4+
5+
from shiny.playwright import controller
6+
from shiny.run import ShinyAppProc
7+
8+
9+
@pytest.mark.flaky(reruns=3, reruns_delay=2)
10+
def test_dynamic_navs(page: Page, local_app: ShinyAppProc) -> None:
11+
page.goto(local_app.url)
12+
13+
# Page begins with 2 tabs: "Hello" and "Foo" and a nav menu with 2 static items.
14+
navset_tab = controller.NavsetTab(page, "tabs")
15+
16+
# Click add-foo to add a new Foo tab
17+
addfoo = controller.InputActionButton(page, "addFoo")
18+
addfoo.click()
19+
controller.NavsetTab(page, "tabs").expect_nav_titles(
20+
["Hello", "Foo", "Foo-1", "Static 1", "Static 2"]
21+
)
22+
23+
# Click hide-tab to hide the Foo tabs
24+
hidetab = controller.InputActionButton(page, "hideTab")
25+
hidetab.click()
26+
27+
# Expect the Foo tabs to be hidden
28+
expect(
29+
page.get_by_role("presentation").filter(has_text="This is the Foo tab")
30+
).to_be_hidden()
31+
expect(page.get_by_role("presentation").filter(has_text="Foo-")).to_be_hidden()
32+
33+
# Click show-tab to show the Foo tabs again
34+
showtab = controller.InputActionButton(page, "showTab")
35+
showtab.click()
36+
37+
# Expect the Foo tabs to be visible again
38+
expect(
39+
page.get_by_role("presentation").filter(has_text="This is the Foo tab")
40+
).not_to_be_hidden()
41+
expect(page.get_by_role("presentation").filter(has_text="Foo-")).not_to_be_hidden()
42+
43+
# Click remove-foo to remove the Foo tabs
44+
removefoo = controller.InputActionButton(page, "removeFoo")
45+
removefoo.click()
46+
controller.NavsetTab(page, "tabs").expect_nav_titles(
47+
["Hello", "Static 1", "Static 2"]
48+
)
49+
50+
# Click add to add a dynamic tab
51+
add = controller.InputActionButton(page, "add")
52+
add.click()
53+
controller.NavsetTab(page, "tabs").expect_nav_titles(
54+
["Hello", "Static 1", "Dynamic-1", "Static 2"]
55+
)
56+
57+
# Click add again to add another dynamic tab
58+
add.click()
59+
controller.NavsetTab(page, "tabs").expect_nav_titles(
60+
["Hello", "Static 1", "Dynamic-1", "Dynamic-2", "Static 2"]
61+
)
62+
63+
# Click hide-menu to hide the static menu
64+
hidemenu = controller.InputActionButton(page, "hideMenu")
65+
hidemenu.click()
66+
67+
# Expect the Menu to be hidden
68+
expect(page.get_by_role("presentation").filter(has_text="Static")).to_be_hidden()
69+
70+
# Click show-menu to show the static menu again
71+
showmenu = controller.InputActionButton(page, "showMenu")
72+
showmenu.click()
73+
74+
# Expect the Menu to be visible again
75+
expect(
76+
page.get_by_role("presentation").filter(has_text="Static")
77+
).not_to_be_hidden()

0 commit comments

Comments
 (0)