test_state_inheritance.py 15 KB

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