test_dynamic_routes.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. """Integration tests for dynamic route page behavior."""
  2. from __future__ import annotations
  3. import time
  4. from collections.abc import Callable, Coroutine, Generator
  5. from urllib.parse import urlsplit
  6. import pytest
  7. from selenium.webdriver.common.by import By
  8. from reflex.testing import AppHarness, AppHarnessProd, WebDriver
  9. from .utils import poll_for_navigation
  10. def DynamicRoute():
  11. """App for testing dynamic routes."""
  12. import reflex as rx
  13. class DynamicState(rx.State):
  14. order: list[str] = []
  15. @rx.event
  16. def on_load(self):
  17. page_data = f"{self.router.page.path}-{self.page_id or 'no page id'}"
  18. print(f"on_load: {page_data}")
  19. self.order.append(page_data)
  20. @rx.event
  21. def on_load_redir(self):
  22. query_params = self.router.page.params
  23. page_data = f"on_load_redir-{query_params}"
  24. print(f"on_load_redir: {page_data}")
  25. self.order.append(page_data)
  26. return rx.redirect(f"/page/{query_params['page_id']}")
  27. @rx.var
  28. def next_page(self) -> str:
  29. try:
  30. return str(int(self.page_id) + 1)
  31. except ValueError:
  32. return "0"
  33. def index():
  34. return rx.fragment(
  35. rx.input(
  36. value=DynamicState.router.session.client_token,
  37. read_only=True,
  38. id="token",
  39. ),
  40. rx.input(value=rx.State.page_id, read_only=True, id="page_id"), # pyright: ignore [reportAttributeAccessIssue]
  41. rx.input(
  42. value=DynamicState.router.page.raw_path,
  43. read_only=True,
  44. id="raw_path",
  45. ),
  46. rx.link("index", href="/", id="link_index"),
  47. rx.link("page_X", href="/static/x", id="link_page_x"),
  48. rx.link(
  49. "next",
  50. href="/page/" + DynamicState.next_page,
  51. id="link_page_next",
  52. ),
  53. rx.link("missing", href="/missing", id="link_missing"),
  54. rx.list( # pyright: ignore [reportAttributeAccessIssue]
  55. rx.foreach(
  56. DynamicState.order, # pyright: ignore [reportAttributeAccessIssue]
  57. lambda i: rx.list_item(rx.text(i)),
  58. ),
  59. ),
  60. )
  61. class ArgState(rx.State):
  62. """The app state."""
  63. @rx.var(cache=False)
  64. def arg(self) -> int:
  65. return int(self.arg_str or 0)
  66. class ArgSubState(ArgState):
  67. @rx.var
  68. def cached_arg(self) -> int:
  69. return self.arg
  70. @rx.var
  71. def cached_arg_str(self) -> str:
  72. return self.arg_str
  73. @rx.page(route="/arg/[arg_str]")
  74. def arg() -> rx.Component:
  75. return rx.vstack(
  76. rx.input(
  77. value=DynamicState.router.session.client_token,
  78. read_only=True,
  79. id="token",
  80. ),
  81. rx.data_list.root(
  82. rx.data_list.item(
  83. rx.data_list.label("rx.State.arg_str (dynamic)"),
  84. rx.data_list.value(rx.State.arg_str, id="state-arg_str"), # pyright: ignore [reportAttributeAccessIssue]
  85. ),
  86. rx.data_list.item(
  87. rx.data_list.label("ArgState.arg_str (dynamic) (inherited)"),
  88. rx.data_list.value(ArgState.arg_str, id="argstate-arg_str"), # pyright: ignore [reportAttributeAccessIssue]
  89. ),
  90. rx.data_list.item(
  91. rx.data_list.label("ArgState.arg"),
  92. rx.data_list.value(ArgState.arg, id="argstate-arg"),
  93. ),
  94. rx.data_list.item(
  95. rx.data_list.label("ArgSubState.arg_str (dynamic) (inherited)"),
  96. rx.data_list.value(ArgSubState.arg_str, id="argsubstate-arg_str"), # pyright: ignore [reportAttributeAccessIssue]
  97. ),
  98. rx.data_list.item(
  99. rx.data_list.label("ArgSubState.arg (inherited)"),
  100. rx.data_list.value(ArgSubState.arg, id="argsubstate-arg"),
  101. ),
  102. rx.data_list.item(
  103. rx.data_list.label("ArgSubState.cached_arg"),
  104. rx.data_list.value(
  105. ArgSubState.cached_arg, id="argsubstate-cached_arg"
  106. ),
  107. ),
  108. rx.data_list.item(
  109. rx.data_list.label("ArgSubState.cached_arg_str"),
  110. rx.data_list.value(
  111. ArgSubState.cached_arg_str, id="argsubstate-cached_arg_str"
  112. ),
  113. ),
  114. ),
  115. rx.link("+", href=f"/arg/{ArgState.arg + 1}", id="next-page"),
  116. align="center",
  117. height="100vh",
  118. )
  119. @rx.page(route="/redirect-page/[page_id]", on_load=DynamicState.on_load_redir)
  120. def redirect_page():
  121. return rx.fragment(rx.text("redirecting..."))
  122. app = rx.App()
  123. app.add_page(index, route="/page/[page_id]", on_load=DynamicState.on_load)
  124. app.add_page(index, route="/static/x", on_load=DynamicState.on_load)
  125. app.add_page(index)
  126. app.add_custom_404_page(on_load=DynamicState.on_load)
  127. @pytest.fixture(scope="module")
  128. def dynamic_route(
  129. app_harness_env: type[AppHarness], tmp_path_factory
  130. ) -> Generator[AppHarness, None, None]:
  131. """Start DynamicRoute app at tmp_path via AppHarness.
  132. Args:
  133. app_harness_env: either AppHarness (dev) or AppHarnessProd (prod)
  134. tmp_path_factory: pytest tmp_path_factory fixture
  135. Yields:
  136. running AppHarness instance
  137. """
  138. with app_harness_env.create(
  139. root=tmp_path_factory.mktemp("dynamic_route"),
  140. app_name=f"dynamicroute_{app_harness_env.__name__.lower()}",
  141. app_source=DynamicRoute,
  142. ) as harness:
  143. yield harness
  144. @pytest.fixture
  145. def driver(dynamic_route: AppHarness) -> Generator[WebDriver, None, None]:
  146. """Get an instance of the browser open to the dynamic_route app.
  147. Args:
  148. dynamic_route: harness for DynamicRoute app
  149. Yields:
  150. WebDriver instance.
  151. """
  152. assert dynamic_route.app_instance is not None, "app is not running"
  153. driver = dynamic_route.frontend()
  154. # TODO: drop after flakiness is resolved
  155. driver.implicitly_wait(30)
  156. try:
  157. yield driver
  158. finally:
  159. driver.quit()
  160. @pytest.fixture()
  161. def token(dynamic_route: AppHarness, driver: WebDriver) -> str:
  162. """Get the token associated with backend state.
  163. Args:
  164. dynamic_route: harness for DynamicRoute app.
  165. driver: WebDriver instance.
  166. Returns:
  167. The token visible in the driver browser.
  168. """
  169. assert dynamic_route.app_instance is not None
  170. token_input = driver.find_element(By.ID, "token")
  171. assert token_input
  172. # wait for the backend connection to send the token
  173. token = dynamic_route.poll_for_value(token_input)
  174. assert token is not None
  175. return token
  176. @pytest.fixture()
  177. def poll_for_order(
  178. dynamic_route: AppHarness, token: str
  179. ) -> Callable[[list[str]], Coroutine[None, None, None]]:
  180. """Poll for the order list to match the expected order.
  181. Args:
  182. dynamic_route: harness for DynamicRoute app.
  183. token: The token visible in the driver browser.
  184. Returns:
  185. An async function that polls for the order list to match the expected order.
  186. """
  187. dynamic_state_name = dynamic_route.get_state_name("_dynamic_state")
  188. dynamic_state_full_name = dynamic_route.get_full_state_name(["_dynamic_state"])
  189. async def _poll_for_order(exp_order: list[str]):
  190. async def _backend_state():
  191. return await dynamic_route.get_state(f"{token}_{dynamic_state_full_name}")
  192. async def _check():
  193. return (await _backend_state()).substates[
  194. dynamic_state_name
  195. ].order == exp_order
  196. await AppHarness._poll_for_async(_check, timeout=60)
  197. assert (
  198. list((await _backend_state()).substates[dynamic_state_name].order)
  199. == exp_order
  200. )
  201. return _poll_for_order
  202. @pytest.mark.asyncio
  203. async def test_on_load_navigate(
  204. dynamic_route: AppHarness,
  205. driver: WebDriver,
  206. token: str,
  207. poll_for_order: Callable[[list[str]], Coroutine[None, None, None]],
  208. ):
  209. """Click links to navigate between dynamic pages with on_load event.
  210. Args:
  211. dynamic_route: harness for DynamicRoute app.
  212. driver: WebDriver instance.
  213. token: The token visible in the driver browser.
  214. poll_for_order: function that polls for the order list to match the expected order.
  215. """
  216. dynamic_state_full_name = dynamic_route.get_full_state_name(["_dynamic_state"])
  217. assert dynamic_route.app_instance is not None
  218. is_prod = isinstance(dynamic_route, AppHarnessProd)
  219. link = driver.find_element(By.ID, "link_page_next")
  220. assert link
  221. exp_order = [f"/page/[page_id]-{ix}" for ix in range(10)]
  222. # click the link a few times
  223. for ix in range(10):
  224. # wait for navigation, then assert on url
  225. with poll_for_navigation(driver):
  226. link.click()
  227. assert urlsplit(driver.current_url).path == f"/page/{ix}/"
  228. link = driver.find_element(By.ID, "link_page_next")
  229. page_id_input = driver.find_element(By.ID, "page_id")
  230. raw_path_input = driver.find_element(By.ID, "raw_path")
  231. assert link
  232. assert page_id_input
  233. assert dynamic_route.poll_for_value(
  234. page_id_input, exp_not_equal=str(ix - 1)
  235. ) == str(ix)
  236. assert dynamic_route.poll_for_value(raw_path_input) == f"/page/{ix}/"
  237. await poll_for_order(exp_order)
  238. # manually load the next page to trigger client side routing in prod mode
  239. if is_prod:
  240. exp_order += ["/404-no page id"]
  241. exp_order += ["/page/[page_id]-10"]
  242. with poll_for_navigation(driver):
  243. driver.get(f"{dynamic_route.frontend_url}/page/10/")
  244. await poll_for_order(exp_order)
  245. # make sure internal nav still hydrates after redirect
  246. exp_order += ["/page/[page_id]-11"]
  247. link = driver.find_element(By.ID, "link_page_next")
  248. with poll_for_navigation(driver):
  249. link.click()
  250. await poll_for_order(exp_order)
  251. # load same page with a query param and make sure it passes through
  252. if is_prod:
  253. exp_order += ["/404-no page id"]
  254. exp_order += ["/page/[page_id]-11"]
  255. with poll_for_navigation(driver):
  256. driver.get(f"{driver.current_url}?foo=bar")
  257. await poll_for_order(exp_order)
  258. assert (
  259. await dynamic_route.get_state(f"{token}_{dynamic_state_full_name}")
  260. ).router.page.params["foo"] == "bar"
  261. # hit a 404 and ensure we still hydrate
  262. exp_order += ["/404-no page id"]
  263. with poll_for_navigation(driver):
  264. driver.get(f"{dynamic_route.frontend_url}/missing")
  265. await poll_for_order(exp_order)
  266. # browser nav should still trigger hydration
  267. if is_prod:
  268. exp_order += ["/404-no page id"]
  269. exp_order += ["/page/[page_id]-11"]
  270. with poll_for_navigation(driver):
  271. driver.back()
  272. await poll_for_order(exp_order)
  273. # next/link to a 404 and ensure we still hydrate
  274. exp_order += ["/404-no page id"]
  275. link = driver.find_element(By.ID, "link_missing")
  276. with poll_for_navigation(driver):
  277. link.click()
  278. await poll_for_order(exp_order)
  279. # hit a page that redirects back to dynamic page
  280. if is_prod:
  281. exp_order += ["/404-no page id"]
  282. exp_order += ["on_load_redir-{'foo': 'bar', 'page_id': '0'}", "/page/[page_id]-0"]
  283. with poll_for_navigation(driver):
  284. driver.get(f"{dynamic_route.frontend_url}/redirect-page/0/?foo=bar")
  285. await poll_for_order(exp_order)
  286. # should have redirected back to page 0
  287. assert urlsplit(driver.current_url).path == "/page/0/"
  288. @pytest.mark.asyncio
  289. async def test_on_load_navigate_non_dynamic(
  290. dynamic_route: AppHarness,
  291. driver: WebDriver,
  292. poll_for_order: Callable[[list[str]], Coroutine[None, None, None]],
  293. ):
  294. """Click links to navigate between static pages with on_load event.
  295. Args:
  296. dynamic_route: harness for DynamicRoute app.
  297. driver: WebDriver instance.
  298. poll_for_order: function that polls for the order list to match the expected order.
  299. """
  300. assert dynamic_route.app_instance is not None
  301. link = driver.find_element(By.ID, "link_page_x")
  302. assert link
  303. with poll_for_navigation(driver):
  304. link.click()
  305. assert urlsplit(driver.current_url).path == "/static/x/"
  306. await poll_for_order(["/static/x-no page id"])
  307. # go back to the index and navigate back to the static route
  308. link = driver.find_element(By.ID, "link_index")
  309. with poll_for_navigation(driver):
  310. link.click()
  311. assert urlsplit(driver.current_url).path == "/"
  312. link = driver.find_element(By.ID, "link_page_x")
  313. with poll_for_navigation(driver):
  314. link.click()
  315. assert urlsplit(driver.current_url).path == "/static/x/"
  316. await poll_for_order(["/static/x-no page id", "/static/x-no page id"])
  317. @pytest.mark.asyncio
  318. async def test_render_dynamic_arg(
  319. dynamic_route: AppHarness,
  320. driver: WebDriver,
  321. token: str,
  322. ):
  323. """Assert that dynamic arg var is rendered correctly in different contexts.
  324. Args:
  325. dynamic_route: harness for DynamicRoute app.
  326. driver: WebDriver instance.
  327. token: The token visible in the driver browser.
  328. """
  329. assert dynamic_route.app_instance is not None
  330. with poll_for_navigation(driver):
  331. driver.get(f"{dynamic_route.frontend_url}/arg/0")
  332. # TODO: drop after flakiness is resolved
  333. time.sleep(3)
  334. def assert_content(expected: str, expect_not: str):
  335. ids = [
  336. "state-arg_str",
  337. "argstate-arg",
  338. "argstate-arg_str",
  339. "argsubstate-arg_str",
  340. "argsubstate-arg",
  341. "argsubstate-cached_arg",
  342. "argsubstate-cached_arg_str",
  343. ]
  344. for id in ids:
  345. el = driver.find_element(By.ID, id)
  346. assert el
  347. assert (
  348. dynamic_route.poll_for_content(el, timeout=30, exp_not_equal=expect_not)
  349. == expected
  350. )
  351. assert_content("0", "")
  352. next_page_link = driver.find_element(By.ID, "next-page")
  353. assert next_page_link
  354. with poll_for_navigation(driver):
  355. next_page_link.click()
  356. assert driver.current_url == f"{dynamic_route.frontend_url}/arg/1/"
  357. assert_content("1", "0")
  358. next_page_link = driver.find_element(By.ID, "next-page")
  359. assert next_page_link
  360. with poll_for_navigation(driver):
  361. next_page_link.click()
  362. assert driver.current_url == f"{dynamic_route.frontend_url}/arg/2/"
  363. assert_content("2", "1")