test_state_inheritance.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. @rx.var
  61. def computed_basevar(self) -> str:
  62. return "computed_basevar1"
  63. class Base2(rx.State):
  64. base2: str = "base2"
  65. @rx.var
  66. def computed_basevar(self) -> str:
  67. return "computed_basevar2"
  68. class Child1(Base1, OtherMixin):
  69. pass
  70. class Child2(Base2, Mixin, OtherMixin):
  71. pass
  72. class Child3(Child2):
  73. child3: str = "child3"
  74. @rx.var
  75. def computed_childvar(self) -> str:
  76. return "computed_childvar"
  77. def index() -> rx.Component:
  78. return rx.vstack(
  79. rx.chakra.input(
  80. id="token", value=Base1.router.session.client_token, is_read_only=True
  81. ),
  82. # Base 1
  83. rx.heading(Base1.computed_mixin, id="base1-computed_mixin"),
  84. rx.heading(Base1.computed_basevar, id="base1-computed_basevar"),
  85. rx.heading(Base1.computed_child_mixin, id="base1-child-mixin"),
  86. rx.heading(Base1.base1, id="base1-base1"),
  87. rx.button(
  88. "Base1.on_click_mixin",
  89. on_click=Base1.on_click_mixin, # type: ignore
  90. id="base1-mixin-btn",
  91. ),
  92. # Base 2
  93. rx.heading(Base2.computed_basevar, id="base2-computed_basevar"),
  94. rx.heading(Base2.base2, id="base2-base2"),
  95. # Child 1
  96. rx.heading(Child1.computed_basevar, id="child1-computed_basevar"),
  97. rx.heading(Child1.computed_mixin, id="child1-computed_mixin"),
  98. rx.heading(Child1.computed_other_mixin, id="child1-other-mixin"),
  99. rx.heading(Child1.computed_child_mixin, id="child1-child-mixin"),
  100. rx.heading(Child1.base1, id="child1-base1"),
  101. rx.heading(Child1.other_mixin, id="child1-other_mixin"),
  102. rx.button(
  103. "Child1.on_click_other_mixin",
  104. on_click=Child1.on_click_other_mixin, # type: ignore
  105. id="child1-other-mixin-btn",
  106. ),
  107. # Child 2
  108. rx.heading(Child2.computed_basevar, id="child2-computed_basevar"),
  109. rx.heading(Child2.computed_mixin, id="child2-computed_mixin"),
  110. rx.heading(Child2.computed_other_mixin, id="child2-other-mixin"),
  111. rx.heading(Child2.computed_child_mixin, id="child2-child-mixin"),
  112. rx.heading(Child2.base2, id="child2-base2"),
  113. rx.heading(Child2.other_mixin, id="child2-other_mixin"),
  114. rx.button(
  115. "Child2.on_click_mixin",
  116. on_click=Child2.on_click_mixin, # type: ignore
  117. id="child2-mixin-btn",
  118. ),
  119. rx.button(
  120. "Child2.on_click_other_mixin",
  121. on_click=Child2.on_click_other_mixin, # type: ignore
  122. id="child2-other-mixin-btn",
  123. ),
  124. # Child 3
  125. rx.heading(Child3.computed_basevar, id="child3-computed_basevar"),
  126. rx.heading(Child3.computed_mixin, id="child3-computed_mixin"),
  127. rx.heading(Child3.computed_other_mixin, id="child3-other-mixin"),
  128. rx.heading(Child3.computed_childvar, id="child3-computed_childvar"),
  129. rx.heading(Child3.computed_child_mixin, id="child3-child-mixin"),
  130. rx.heading(Child3.child3, id="child3-child3"),
  131. rx.heading(Child3.base2, id="child3-base2"),
  132. rx.heading(Child3.other_mixin, id="child3-other_mixin"),
  133. rx.button(
  134. "Child3.on_click_mixin",
  135. on_click=Child3.on_click_mixin, # type: ignore
  136. id="child3-mixin-btn",
  137. ),
  138. rx.button(
  139. "Child3.on_click_other_mixin",
  140. on_click=Child3.on_click_other_mixin, # type: ignore
  141. id="child3-other-mixin-btn",
  142. ),
  143. )
  144. app = rx.App()
  145. app.add_page(index)
  146. @pytest.fixture(scope="session")
  147. def state_inheritance(
  148. tmp_path_factory,
  149. ) -> Generator[AppHarness, None, None]:
  150. """Start StateInheritance app at tmp_path via AppHarness.
  151. Args:
  152. tmp_path_factory: pytest tmp_path_factory fixture
  153. Yields:
  154. running AppHarness instance
  155. """
  156. with AppHarness.create(
  157. root=tmp_path_factory.mktemp(f"state_inheritance"),
  158. app_source=StateInheritance, # type: ignore
  159. ) as harness:
  160. yield harness
  161. @pytest.fixture
  162. def driver(state_inheritance: AppHarness) -> Generator[WebDriver, None, None]:
  163. """Get an instance of the browser open to the state_inheritance app.
  164. Args:
  165. state_inheritance: harness for StateInheritance app
  166. Yields:
  167. WebDriver instance.
  168. """
  169. assert state_inheritance.app_instance is not None, "app is not running"
  170. driver = state_inheritance.frontend()
  171. try:
  172. yield driver
  173. finally:
  174. driver.quit()
  175. @pytest.fixture()
  176. def token(state_inheritance: AppHarness, driver: WebDriver) -> str:
  177. """Get a function that returns the active token.
  178. Args:
  179. state_inheritance: harness for StateInheritance app.
  180. driver: WebDriver instance.
  181. Returns:
  182. The token for the connected client
  183. """
  184. assert state_inheritance.app_instance is not None
  185. token_input = driver.find_element(By.ID, "token")
  186. assert token_input
  187. # wait for the backend connection to send the token
  188. token = state_inheritance.poll_for_value(token_input, timeout=DEFAULT_TIMEOUT * 2)
  189. assert token is not None
  190. return token
  191. def test_state_inheritance(
  192. state_inheritance: AppHarness,
  193. driver: WebDriver,
  194. token: str,
  195. ):
  196. """Test that background tasks work as expected.
  197. Args:
  198. state_inheritance: harness for StateInheritance app.
  199. driver: WebDriver instance.
  200. token: The token for the connected client.
  201. """
  202. assert state_inheritance.app_instance is not None
  203. # Initial State values Test
  204. # Base 1
  205. base1_mixin = driver.find_element(By.ID, "base1-computed_mixin")
  206. assert base1_mixin.text == "computed_mixin"
  207. base1_computed_basevar = driver.find_element(By.ID, "base1-computed_basevar")
  208. assert base1_computed_basevar.text == "computed_basevar1"
  209. base1_computed_child_mixin = driver.find_element(By.ID, "base1-child-mixin")
  210. assert base1_computed_child_mixin.text == "computed_child_mixin"
  211. base1_base1 = driver.find_element(By.ID, "base1-base1")
  212. assert base1_base1.text == "base1"
  213. # Base 2
  214. base2_computed_basevar = driver.find_element(By.ID, "base2-computed_basevar")
  215. assert base2_computed_basevar.text == "computed_basevar2"
  216. base2_base2 = driver.find_element(By.ID, "base2-base2")
  217. assert base2_base2.text == "base2"
  218. # Child 1
  219. child1_computed_basevar = driver.find_element(By.ID, "child1-computed_basevar")
  220. assert child1_computed_basevar.text == "computed_basevar1"
  221. child1_mixin = driver.find_element(By.ID, "child1-computed_mixin")
  222. assert child1_mixin.text == "computed_mixin"
  223. child1_computed_other_mixin = driver.find_element(By.ID, "child1-other-mixin")
  224. assert child1_computed_other_mixin.text == "other_mixin"
  225. child1_computed_child_mixin = driver.find_element(By.ID, "child1-child-mixin")
  226. assert child1_computed_child_mixin.text == "computed_child_mixin"
  227. child1_base1 = driver.find_element(By.ID, "child1-base1")
  228. assert child1_base1.text == "base1"
  229. child1_other_mixin = driver.find_element(By.ID, "child1-other_mixin")
  230. assert child1_other_mixin.text == "other_mixin"
  231. # Child 2
  232. child2_computed_basevar = driver.find_element(By.ID, "child2-computed_basevar")
  233. assert child2_computed_basevar.text == "computed_basevar2"
  234. child2_mixin = driver.find_element(By.ID, "child2-computed_mixin")
  235. assert child2_mixin.text == "computed_mixin"
  236. child2_computed_other_mixin = driver.find_element(By.ID, "child2-other-mixin")
  237. assert child2_computed_other_mixin.text == "other_mixin"
  238. child2_computed_child_mixin = driver.find_element(By.ID, "child2-child-mixin")
  239. assert child2_computed_child_mixin.text == "computed_child_mixin"
  240. child2_base2 = driver.find_element(By.ID, "child2-base2")
  241. assert child2_base2.text == "base2"
  242. child2_other_mixin = driver.find_element(By.ID, "child2-other_mixin")
  243. assert child2_other_mixin.text == "other_mixin"
  244. # Child 3
  245. child3_computed_basevar = driver.find_element(By.ID, "child3-computed_basevar")
  246. assert child3_computed_basevar.text == "computed_basevar2"
  247. child3_mixin = driver.find_element(By.ID, "child3-computed_mixin")
  248. assert child3_mixin.text == "computed_mixin"
  249. child3_computed_other_mixin = driver.find_element(By.ID, "child3-other-mixin")
  250. assert child3_computed_other_mixin.text == "other_mixin"
  251. child3_computed_childvar = driver.find_element(By.ID, "child3-computed_childvar")
  252. assert child3_computed_childvar.text == "computed_childvar"
  253. child3_computed_child_mixin = driver.find_element(By.ID, "child3-child-mixin")
  254. assert child3_computed_child_mixin.text == "computed_child_mixin"
  255. child3_child3 = driver.find_element(By.ID, "child3-child3")
  256. assert child3_child3.text == "child3"
  257. child3_base2 = driver.find_element(By.ID, "child3-base2")
  258. assert child3_base2.text == "base2"
  259. child3_other_mixin = driver.find_element(By.ID, "child3-other_mixin")
  260. assert child3_other_mixin.text == "other_mixin"
  261. # Event Handler Tests
  262. raises_alert(driver, "base1-mixin-btn")
  263. raises_alert(driver, "child2-mixin-btn")
  264. raises_alert(driver, "child3-mixin-btn")
  265. child1_other_mixin_btn = driver.find_element(By.ID, "child1-other-mixin-btn")
  266. child1_other_mixin_btn.click()
  267. child1_other_mixin_value = state_inheritance.poll_for_content(
  268. child1_other_mixin, exp_not_equal="other_mixin"
  269. )
  270. child1_computed_mixin_value = state_inheritance.poll_for_content(
  271. child1_computed_other_mixin, exp_not_equal="other_mixin"
  272. )
  273. assert child1_other_mixin_value == "Child1.clicked.1"
  274. assert child1_computed_mixin_value == "Child1.clicked.1"
  275. child2_other_mixin_btn = driver.find_element(By.ID, "child2-other-mixin-btn")
  276. child2_other_mixin_btn.click()
  277. child2_other_mixin_value = state_inheritance.poll_for_content(
  278. child2_other_mixin, exp_not_equal="other_mixin"
  279. )
  280. child2_computed_mixin_value = state_inheritance.poll_for_content(
  281. child2_computed_other_mixin, exp_not_equal="other_mixin"
  282. )
  283. child3_other_mixin_value = state_inheritance.poll_for_content(
  284. child3_other_mixin, exp_not_equal="other_mixin"
  285. )
  286. child3_computed_mixin_value = state_inheritance.poll_for_content(
  287. child3_computed_other_mixin, exp_not_equal="other_mixin"
  288. )
  289. assert child2_other_mixin_value == "Child2.clicked.1"
  290. assert child2_computed_mixin_value == "Child2.clicked.1"
  291. assert child3_other_mixin_value == "Child2.clicked.1"
  292. assert child3_computed_mixin_value == "Child2.clicked.1"
  293. child3_other_mixin_btn = driver.find_element(By.ID, "child3-other-mixin-btn")
  294. child3_other_mixin_btn.click()
  295. child2_other_mixin_value = state_inheritance.poll_for_content(
  296. child2_other_mixin, exp_not_equal="Child2.clicked.1"
  297. )
  298. child2_computed_mixin_value = state_inheritance.poll_for_content(
  299. child2_computed_other_mixin, exp_not_equal="other_mixin"
  300. )
  301. child3_other_mixin_value = state_inheritance.poll_for_content(
  302. child3_other_mixin, exp_not_equal="other_mixin"
  303. )
  304. child3_computed_mixin_value = state_inheritance.poll_for_content(
  305. child3_computed_other_mixin, exp_not_equal="other_mixin"
  306. )
  307. assert child2_other_mixin_value == "Child2.clicked.2"
  308. assert child2_computed_mixin_value == "Child2.clicked.2"
  309. assert child3_other_mixin.text == "Child2.clicked.2"
  310. assert child3_computed_other_mixin.text == "Child2.clicked.2"