test_state_inheritance.py 16 KB

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