Skip to content

Commit 9aebd43

Browse files
committed
Extend tests to cover onmount case
1 parent bbe8847 commit 9aebd43

File tree

1 file changed

+91
-2
lines changed

1 file changed

+91
-2
lines changed

tests/integration/tests_playwright/test_frontend_path.py

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,106 @@ def onload_redirect_with_prefix_app(tmp_path) -> Generator[AppHarness, None, Non
9797
environment.REFLEX_FRONTEND_PATH.set("")
9898

9999

100+
def OnMountRedirectApp():
101+
"""App demonstrate on_mount redirection behaviour."""
102+
import reflex as rx
103+
104+
@rx.page("/")
105+
def index():
106+
return rx.container(
107+
rx.input(
108+
value=rx.State.router.session.client_token,
109+
read_only=True,
110+
id="token",
111+
),
112+
rx.vstack(
113+
rx.heading("This is the index page!"),
114+
rx.button("Go to Subpage!", on_click=rx.redirect("/subpage")),
115+
),
116+
)
117+
118+
@rx.page("/subpage")
119+
def subpage():
120+
return rx.container(
121+
rx.vstack(
122+
rx.heading("This is the sub page!"),
123+
rx.button("Go to index!", on_click=rx.redirect("/")),
124+
rx.button("Bounce to index!", on_click=rx.redirect("/bouncer")),
125+
)
126+
)
127+
128+
@rx.page("/bouncer")
129+
def bouncer():
130+
return rx.container(
131+
rx.vstack(
132+
rx.heading("This is the bouncer page!"),
133+
rx.text("You should not be here!"),
134+
rx.spinner("Go to index!", on_mount=rx.redirect("/")),
135+
),
136+
)
137+
138+
app = rx.App() # noqa: F841
139+
140+
141+
@pytest.fixture
142+
def onmount_redirect_app(tmp_path) -> Generator[AppHarness, None, None]:
143+
"""Start the OnMountRedirectApp without setting REFLEX_FRONTEND_PATH".
144+
145+
This is a baseline used to show on_mount redirects work without a frontend_path.
146+
147+
Args:
148+
tmp_path: pytest tmp_path fixture
149+
150+
Yields:
151+
running AppHarness instance
152+
"""
153+
with AppHarnessProd.create(
154+
root=tmp_path / "onmount_redirect_app",
155+
app_source=OnMountRedirectApp,
156+
) as harness:
157+
assert harness.app_instance is not None, "app is not running"
158+
yield harness
159+
160+
161+
@pytest.fixture
162+
def onmount_redirect_with_prefix_app(tmp_path) -> Generator[AppHarness, None, None]:
163+
"""Start the OnMountRedirectApp with REFLEX_FRONTEND_PATH set to "/prefix".
164+
165+
This simulates setting the REFLEX_FRONTEND_PATH to identify issues with redirection.
166+
167+
Args:
168+
tmp_path: pytest tmp_path fixture
169+
170+
Yields:
171+
running AppHarness instance
172+
"""
173+
prefix = "/prefix"
174+
try:
175+
environment.REFLEX_FRONTEND_PATH.set(prefix)
176+
with AppHarness.create(
177+
root=tmp_path / "onmount_redirect_with_prefix_app",
178+
app_source=OnMountRedirectApp,
179+
) as harness:
180+
assert harness.app_instance is not None, "app is not running"
181+
environment.REFLEX_FRONTEND_PATH.set("")
182+
yield harness
183+
finally:
184+
environment.REFLEX_FRONTEND_PATH.set("")
185+
186+
100187
@pytest.mark.parametrize(
101188
("app_fixture_name", "frontend_path"),
102189
[
103190
("onload_redirect_app", ""),
104191
("onload_redirect_with_prefix_app", "/prefix"),
192+
("onmount_redirect_app", ""),
193+
("onmount_redirect_with_prefix_app", "/prefix"),
105194
],
106195
)
107-
def test_onload_redirect(
196+
def test_redirection_triggers(
108197
app_fixture_name: str, frontend_path: str, page: Page, request
109198
):
110-
"""Ensure that on_load redirects work correctly when a frontend_path is present.
199+
"""Ensure that on_load and on_mount redirects work with/without a frontend_path.
111200
112201
Args:
113202
app_fixture_name: Name of the app fixture to use for the test.

0 commit comments

Comments
 (0)