1
0

test_state_inheritance.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. """Test state inheritance."""
  2. from __future__ import annotations
  3. from contextlib import suppress
  4. from typing import Generator
  5. import pytest
  6. from selenium.common.exceptions import NoAlertPresentException
  7. from selenium.webdriver.common.alert import Alert
  8. from selenium.webdriver.common.by import By
  9. from reflex.testing import DEFAULT_TIMEOUT, AppHarness, WebDriver
  10. def get_alert_or_none(driver: WebDriver) -> Alert | None:
  11. """Switch to an alert if present.
  12. Args:
  13. driver: WebDriver instance.
  14. Returns:
  15. The alert if present, otherwise None.
  16. """
  17. with suppress(NoAlertPresentException):
  18. return driver.switch_to.alert
  19. def raises_alert(driver: WebDriver, element: str) -> None:
  20. """Click an element and check that an alert is raised.
  21. Args:
  22. driver: WebDriver instance.
  23. element: The element to click.
  24. """
  25. btn = driver.find_element(By.ID, element)
  26. btn.click()
  27. alert = AppHarness._poll_for(lambda: get_alert_or_none(driver))
  28. assert isinstance(alert, Alert)
  29. assert alert.text == "clicked"
  30. alert.accept()
  31. def StateInheritance():
  32. """Test that state inheritance works as expected."""
  33. import reflex as rx
  34. class ChildMixin:
  35. # mixin basevars only work with pydantic/rx.Base models
  36. # child_mixin: str = "child_mixin"
  37. @rx.var
  38. def computed_child_mixin(self) -> str:
  39. return "computed_child_mixin"
  40. class Mixin(ChildMixin):
  41. # mixin basevars only work with pydantic/rx.Base models
  42. # mixin: str = "mixin"
  43. @rx.var
  44. def computed_mixin(self) -> str:
  45. return "computed_mixin"
  46. def on_click_mixin(self):
  47. return rx.call_script("alert('clicked')")
  48. class OtherMixin(rx.Base):
  49. other_mixin: str = "other_mixin"
  50. other_mixin_clicks: int = 0
  51. @rx.var
  52. def computed_other_mixin(self) -> str:
  53. return self.other_mixin
  54. def on_click_other_mixin(self):
  55. self.other_mixin_clicks += 1
  56. self.other_mixin = (
  57. f"{self.__class__.__name__}.clicked.{self.other_mixin_clicks}"
  58. )
  59. class Base1(rx.State, Mixin):
  60. _base1: str = "_base1"
  61. base1: str = "base1"
  62. @rx.var
  63. def computed_basevar(self) -> str:
  64. return "computed_basevar1"
  65. @rx.var
  66. def computed_backend_vars_base1(self) -> str:
  67. return self._base1
  68. class Base2(rx.State):
  69. _base2: str = "_base2"
  70. base2: str = "base2"
  71. @rx.var
  72. def computed_basevar(self) -> str:
  73. return "computed_basevar2"
  74. @rx.var
  75. def computed_backend_vars_base2(self) -> str:
  76. return self._base2
  77. class Child1(Base1, OtherMixin):
  78. pass
  79. class Child2(Base2, Mixin, OtherMixin):
  80. pass
  81. class Child3(Child2):
  82. _child3: str = "_child3"
  83. child3: str = "child3"
  84. @rx.var
  85. def computed_childvar(self) -> str:
  86. return "computed_childvar"
  87. @rx.var
  88. def computed_backend_vars_child3(self) -> str:
  89. return f"{self._base2}.{self._child3}"
  90. def index() -> rx.Component:
  91. return rx.vstack(
  92. rx.chakra.input(
  93. id="token", value=Base1.router.session.client_token, is_read_only=True
  94. ),
  95. # Base 1
  96. rx.heading(Base1.computed_mixin, id="base1-computed_mixin"),
  97. rx.heading(Base1.computed_basevar, id="base1-computed_basevar"),
  98. rx.heading(Base1.computed_child_mixin, id="base1-child-mixin"),
  99. rx.heading(Base1.base1, id="base1-base1"),
  100. rx.button(
  101. "Base1.on_click_mixin",
  102. on_click=Base1.on_click_mixin, # type: ignore
  103. id="base1-mixin-btn",
  104. ),
  105. rx.heading(
  106. Base1.computed_backend_vars_base1, id="base1-computed_backend_vars"
  107. ),
  108. # Base 2
  109. rx.heading(Base2.computed_basevar, id="base2-computed_basevar"),
  110. rx.heading(Base2.base2, id="base2-base2"),
  111. rx.heading(
  112. Base2.computed_backend_vars_base2, id="base2-computed_backend_vars"
  113. ),
  114. # Child 1
  115. rx.heading(Child1.computed_basevar, id="child1-computed_basevar"),
  116. rx.heading(Child1.computed_mixin, id="child1-computed_mixin"),
  117. rx.heading(Child1.computed_other_mixin, id="child1-other-mixin"),
  118. rx.heading(Child1.computed_child_mixin, id="child1-child-mixin"),
  119. rx.heading(Child1.base1, id="child1-base1"),
  120. rx.heading(Child1.other_mixin, id="child1-other_mixin"),
  121. rx.button(
  122. "Child1.on_click_other_mixin",
  123. on_click=Child1.on_click_other_mixin, # type: ignore
  124. id="child1-other-mixin-btn",
  125. ),
  126. # Child 2
  127. rx.heading(Child2.computed_basevar, id="child2-computed_basevar"),
  128. rx.heading(Child2.computed_mixin, id="child2-computed_mixin"),
  129. rx.heading(Child2.computed_other_mixin, id="child2-other-mixin"),
  130. rx.heading(Child2.computed_child_mixin, id="child2-child-mixin"),
  131. rx.heading(Child2.base2, id="child2-base2"),
  132. rx.heading(Child2.other_mixin, id="child2-other_mixin"),
  133. rx.button(
  134. "Child2.on_click_mixin",
  135. on_click=Child2.on_click_mixin, # type: ignore
  136. id="child2-mixin-btn",
  137. ),
  138. rx.button(
  139. "Child2.on_click_other_mixin",
  140. on_click=Child2.on_click_other_mixin, # type: ignore
  141. id="child2-other-mixin-btn",
  142. ),
  143. # Child 3
  144. rx.heading(Child3.computed_basevar, id="child3-computed_basevar"),
  145. rx.heading(Child3.computed_mixin, id="child3-computed_mixin"),
  146. rx.heading(Child3.computed_other_mixin, id="child3-other-mixin"),
  147. rx.heading(Child3.computed_childvar, id="child3-computed_childvar"),
  148. rx.heading(Child3.computed_child_mixin, id="child3-child-mixin"),
  149. rx.heading(Child3.child3, id="child3-child3"),
  150. rx.heading(Child3.base2, id="child3-base2"),
  151. rx.heading(Child3.other_mixin, id="child3-other_mixin"),
  152. rx.button(
  153. "Child3.on_click_mixin",
  154. on_click=Child3.on_click_mixin, # type: ignore
  155. id="child3-mixin-btn",
  156. ),
  157. rx.button(
  158. "Child3.on_click_other_mixin",
  159. on_click=Child3.on_click_other_mixin, # type: ignore
  160. id="child3-other-mixin-btn",
  161. ),
  162. rx.heading(
  163. Child3.computed_backend_vars_child3, id="child3-computed_backend_vars"
  164. ),
  165. )
  166. app = rx.App()
  167. app.add_page(index)
  168. @pytest.fixture(scope="module")
  169. def state_inheritance(
  170. tmp_path_factory,
  171. ) -> Generator[AppHarness, None, None]:
  172. """Start StateInheritance app at tmp_path via AppHarness.
  173. Args:
  174. tmp_path_factory: pytest tmp_path_factory fixture
  175. Yields:
  176. running AppHarness instance
  177. """
  178. with AppHarness.create(
  179. root=tmp_path_factory.mktemp(f"state_inheritance"),
  180. app_source=StateInheritance, # type: ignore
  181. ) as harness:
  182. yield harness
  183. @pytest.fixture
  184. def driver(state_inheritance: AppHarness) -> Generator[WebDriver, None, None]:
  185. """Get an instance of the browser open to the state_inheritance app.
  186. Args:
  187. state_inheritance: harness for StateInheritance app
  188. Yields:
  189. WebDriver instance.
  190. """
  191. assert state_inheritance.app_instance is not None, "app is not running"
  192. driver = state_inheritance.frontend()
  193. try:
  194. yield driver
  195. finally:
  196. driver.quit()
  197. @pytest.fixture()
  198. def token(state_inheritance: AppHarness, driver: WebDriver) -> str:
  199. """Get a function that returns the active token.
  200. Args:
  201. state_inheritance: harness for StateInheritance app.
  202. driver: WebDriver instance.
  203. Returns:
  204. The token for the connected client
  205. """
  206. assert state_inheritance.app_instance is not None
  207. token_input = driver.find_element(By.ID, "token")
  208. assert token_input
  209. # wait for the backend connection to send the token
  210. token = state_inheritance.poll_for_value(token_input, timeout=DEFAULT_TIMEOUT * 2)
  211. assert token is not None
  212. return token
  213. def test_state_inheritance(
  214. state_inheritance: AppHarness,
  215. driver: WebDriver,
  216. token: str,
  217. ):
  218. """Test that background tasks work as expected.
  219. Args:
  220. state_inheritance: harness for StateInheritance app.
  221. driver: WebDriver instance.
  222. token: The token for the connected client.
  223. """
  224. assert state_inheritance.app_instance is not None
  225. # Initial State values Test
  226. # Base 1
  227. base1_mixin = driver.find_element(By.ID, "base1-computed_mixin")
  228. assert base1_mixin.text == "computed_mixin"
  229. base1_computed_basevar = driver.find_element(By.ID, "base1-computed_basevar")
  230. assert base1_computed_basevar.text == "computed_basevar1"
  231. base1_computed_child_mixin = driver.find_element(By.ID, "base1-child-mixin")
  232. assert base1_computed_child_mixin.text == "computed_child_mixin"
  233. base1_base1 = driver.find_element(By.ID, "base1-base1")
  234. assert base1_base1.text == "base1"
  235. base1_computed_backend_vars = driver.find_element(
  236. By.ID, "base1-computed_backend_vars"
  237. )
  238. assert base1_computed_backend_vars.text == "_base1"
  239. # Base 2
  240. base2_computed_basevar = driver.find_element(By.ID, "base2-computed_basevar")
  241. assert base2_computed_basevar.text == "computed_basevar2"
  242. base2_base2 = driver.find_element(By.ID, "base2-base2")
  243. assert base2_base2.text == "base2"
  244. base2_computed_backend_vars = driver.find_element(
  245. By.ID, "base2-computed_backend_vars"
  246. )
  247. assert base2_computed_backend_vars.text == "_base2"
  248. # Child 1
  249. child1_computed_basevar = driver.find_element(By.ID, "child1-computed_basevar")
  250. assert child1_computed_basevar.text == "computed_basevar1"
  251. child1_mixin = driver.find_element(By.ID, "child1-computed_mixin")
  252. assert child1_mixin.text == "computed_mixin"
  253. child1_computed_other_mixin = driver.find_element(By.ID, "child1-other-mixin")
  254. assert child1_computed_other_mixin.text == "other_mixin"
  255. child1_computed_child_mixin = driver.find_element(By.ID, "child1-child-mixin")
  256. assert child1_computed_child_mixin.text == "computed_child_mixin"
  257. child1_base1 = driver.find_element(By.ID, "child1-base1")
  258. assert child1_base1.text == "base1"
  259. child1_other_mixin = driver.find_element(By.ID, "child1-other_mixin")
  260. assert child1_other_mixin.text == "other_mixin"
  261. # Child 2
  262. child2_computed_basevar = driver.find_element(By.ID, "child2-computed_basevar")
  263. assert child2_computed_basevar.text == "computed_basevar2"
  264. child2_mixin = driver.find_element(By.ID, "child2-computed_mixin")
  265. assert child2_mixin.text == "computed_mixin"
  266. child2_computed_other_mixin = driver.find_element(By.ID, "child2-other-mixin")
  267. assert child2_computed_other_mixin.text == "other_mixin"
  268. child2_computed_child_mixin = driver.find_element(By.ID, "child2-child-mixin")
  269. assert child2_computed_child_mixin.text == "computed_child_mixin"
  270. child2_base2 = driver.find_element(By.ID, "child2-base2")
  271. assert child2_base2.text == "base2"
  272. child2_other_mixin = driver.find_element(By.ID, "child2-other_mixin")
  273. assert child2_other_mixin.text == "other_mixin"
  274. # Child 3
  275. child3_computed_basevar = driver.find_element(By.ID, "child3-computed_basevar")
  276. assert child3_computed_basevar.text == "computed_basevar2"
  277. child3_mixin = driver.find_element(By.ID, "child3-computed_mixin")
  278. assert child3_mixin.text == "computed_mixin"
  279. child3_computed_other_mixin = driver.find_element(By.ID, "child3-other-mixin")
  280. assert child3_computed_other_mixin.text == "other_mixin"
  281. child3_computed_childvar = driver.find_element(By.ID, "child3-computed_childvar")
  282. assert child3_computed_childvar.text == "computed_childvar"
  283. child3_computed_child_mixin = driver.find_element(By.ID, "child3-child-mixin")
  284. assert child3_computed_child_mixin.text == "computed_child_mixin"
  285. child3_child3 = driver.find_element(By.ID, "child3-child3")
  286. assert child3_child3.text == "child3"
  287. child3_base2 = driver.find_element(By.ID, "child3-base2")
  288. assert child3_base2.text == "base2"
  289. child3_other_mixin = driver.find_element(By.ID, "child3-other_mixin")
  290. assert child3_other_mixin.text == "other_mixin"
  291. child3_computed_backend_vars = driver.find_element(
  292. By.ID, "child3-computed_backend_vars"
  293. )
  294. assert child3_computed_backend_vars.text == "_base2._child3"
  295. # Event Handler Tests
  296. raises_alert(driver, "base1-mixin-btn")
  297. raises_alert(driver, "child2-mixin-btn")
  298. raises_alert(driver, "child3-mixin-btn")
  299. child1_other_mixin_btn = driver.find_element(By.ID, "child1-other-mixin-btn")
  300. child1_other_mixin_btn.click()
  301. child1_other_mixin_value = state_inheritance.poll_for_content(
  302. child1_other_mixin, exp_not_equal="other_mixin"
  303. )
  304. child1_computed_mixin_value = state_inheritance.poll_for_content(
  305. child1_computed_other_mixin, exp_not_equal="other_mixin"
  306. )
  307. assert child1_other_mixin_value == "Child1.clicked.1"
  308. assert child1_computed_mixin_value == "Child1.clicked.1"
  309. child2_other_mixin_btn = driver.find_element(By.ID, "child2-other-mixin-btn")
  310. child2_other_mixin_btn.click()
  311. child2_other_mixin_value = state_inheritance.poll_for_content(
  312. child2_other_mixin, exp_not_equal="other_mixin"
  313. )
  314. child2_computed_mixin_value = state_inheritance.poll_for_content(
  315. child2_computed_other_mixin, exp_not_equal="other_mixin"
  316. )
  317. child3_other_mixin_value = state_inheritance.poll_for_content(
  318. child3_other_mixin, exp_not_equal="other_mixin"
  319. )
  320. child3_computed_mixin_value = state_inheritance.poll_for_content(
  321. child3_computed_other_mixin, exp_not_equal="other_mixin"
  322. )
  323. assert child2_other_mixin_value == "Child2.clicked.1"
  324. assert child2_computed_mixin_value == "Child2.clicked.1"
  325. assert child3_other_mixin_value == "Child2.clicked.1"
  326. assert child3_computed_mixin_value == "Child2.clicked.1"
  327. child3_other_mixin_btn = driver.find_element(By.ID, "child3-other-mixin-btn")
  328. child3_other_mixin_btn.click()
  329. child2_other_mixin_value = state_inheritance.poll_for_content(
  330. child2_other_mixin, exp_not_equal="Child2.clicked.1"
  331. )
  332. child2_computed_mixin_value = state_inheritance.poll_for_content(
  333. child2_computed_other_mixin, exp_not_equal="other_mixin"
  334. )
  335. child3_other_mixin_value = state_inheritance.poll_for_content(
  336. child3_other_mixin, exp_not_equal="other_mixin"
  337. )
  338. child3_computed_mixin_value = state_inheritance.poll_for_content(
  339. child3_computed_other_mixin, exp_not_equal="other_mixin"
  340. )
  341. assert child2_other_mixin_value == "Child2.clicked.2"
  342. assert child2_computed_mixin_value == "Child2.clicked.2"
  343. assert child3_other_mixin.text == "Child2.clicked.2"
  344. assert child3_computed_other_mixin.text == "Child2.clicked.2"