test_state_inheritance.py 16 KB

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