test_state_inheritance.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. """Test state inheritance."""
  2. from typing import Generator
  3. import pytest
  4. from selenium.webdriver.common.by import By
  5. from reflex.testing import DEFAULT_TIMEOUT, AppHarness, WebDriver
  6. def StateInheritance():
  7. """Test that state inheritance works as expected."""
  8. import reflex as rx
  9. class ChildMixin:
  10. @rx.var
  11. def child_mixin(self) -> str:
  12. return "child_mixin"
  13. class Mixin(ChildMixin):
  14. @rx.var
  15. def mixin(self) -> str:
  16. return "mixin"
  17. class OtherMixin(rx.Base):
  18. @rx.var
  19. def other_mixin(self) -> str:
  20. return "other_mixin"
  21. class Base1(rx.State, Mixin):
  22. @rx.var
  23. def basevar(self) -> str:
  24. return "basevar1"
  25. class Base2(rx.State):
  26. @rx.var
  27. def basevar(self) -> str:
  28. return "basevar2"
  29. class Child1(Base1, OtherMixin):
  30. pass
  31. class Child2(Base2, Mixin, OtherMixin):
  32. pass
  33. class Child3(Child2):
  34. @rx.var
  35. def childvar(self) -> str:
  36. return "childvar"
  37. def index() -> rx.Component:
  38. return rx.vstack(
  39. rx.input(
  40. id="token", value=Base1.router.session.client_token, is_read_only=True
  41. ),
  42. rx.heading(Base1.mixin, id="base1-mixin"),
  43. rx.heading(Base1.basevar, id="base1-basevar"),
  44. rx.heading(Base1.child_mixin, id="base1-child-mixin"),
  45. rx.heading(Base2.basevar, id="base2-basevar"),
  46. rx.heading(Child1.basevar, id="child1-basevar"),
  47. rx.heading(Child1.mixin, id="child1-mixin"),
  48. rx.heading(Child1.other_mixin, id="child1-other-mixin"),
  49. rx.heading(Child1.child_mixin, id="child1-child-mixin"),
  50. rx.heading(Child2.basevar, id="child2-basevar"),
  51. rx.heading(Child2.mixin, id="child2-mixin"),
  52. rx.heading(Child2.other_mixin, id="child2-other-mixin"),
  53. rx.heading(Child2.child_mixin, id="child2-child-mixin"),
  54. rx.heading(Child3.basevar, id="child3-basevar"),
  55. rx.heading(Child3.mixin, id="child3-mixin"),
  56. rx.heading(Child3.other_mixin, id="child3-other-mixin"),
  57. rx.heading(Child3.childvar, id="child3-childvar"),
  58. rx.heading(Child3.child_mixin, id="child3-child-mixin"),
  59. )
  60. app = rx.App()
  61. app.add_page(index)
  62. @pytest.fixture(scope="session")
  63. def state_inheritance(
  64. tmp_path_factory,
  65. ) -> Generator[AppHarness, None, None]:
  66. """Start StateInheritance app at tmp_path via AppHarness.
  67. Args:
  68. tmp_path_factory: pytest tmp_path_factory fixture
  69. Yields:
  70. running AppHarness instance
  71. """
  72. with AppHarness.create(
  73. root=tmp_path_factory.mktemp(f"state_inheritance"),
  74. app_source=StateInheritance, # type: ignore
  75. ) as harness:
  76. yield harness
  77. @pytest.fixture
  78. def driver(state_inheritance: AppHarness) -> Generator[WebDriver, None, None]:
  79. """Get an instance of the browser open to the state_inheritance app.
  80. Args:
  81. state_inheritance: harness for StateInheritance app
  82. Yields:
  83. WebDriver instance.
  84. """
  85. assert state_inheritance.app_instance is not None, "app is not running"
  86. driver = state_inheritance.frontend()
  87. try:
  88. yield driver
  89. finally:
  90. driver.quit()
  91. @pytest.fixture()
  92. def token(state_inheritance: AppHarness, driver: WebDriver) -> str:
  93. """Get a function that returns the active token.
  94. Args:
  95. state_inheritance: harness for StateInheritance app.
  96. driver: WebDriver instance.
  97. Returns:
  98. The token for the connected client
  99. """
  100. assert state_inheritance.app_instance is not None
  101. token_input = driver.find_element(By.ID, "token")
  102. assert token_input
  103. # wait for the backend connection to send the token
  104. token = state_inheritance.poll_for_value(token_input, timeout=DEFAULT_TIMEOUT * 2)
  105. assert token is not None
  106. return token
  107. def test_state_inheritance(
  108. state_inheritance: AppHarness,
  109. driver: WebDriver,
  110. token: str,
  111. ):
  112. """Test that background tasks work as expected.
  113. Args:
  114. state_inheritance: harness for StateInheritance app.
  115. driver: WebDriver instance.
  116. token: The token for the connected client.
  117. """
  118. assert state_inheritance.app_instance is not None
  119. base1_mixin = driver.find_element(By.ID, "base1-mixin")
  120. assert base1_mixin.text == "mixin"
  121. base1_basevar = driver.find_element(By.ID, "base1-basevar")
  122. assert base1_basevar.text == "basevar1"
  123. base1_child_mixin = driver.find_element(By.ID, "base1-child-mixin")
  124. assert base1_child_mixin.text == "child_mixin"
  125. base2_basevar = driver.find_element(By.ID, "base2-basevar")
  126. assert base2_basevar.text == "basevar2"
  127. child1_basevar = driver.find_element(By.ID, "child1-basevar")
  128. assert child1_basevar.text == "basevar1"
  129. child1_mixin = driver.find_element(By.ID, "child1-mixin")
  130. assert child1_mixin.text == "mixin"
  131. child1_other_mixin = driver.find_element(By.ID, "child1-other-mixin")
  132. assert child1_other_mixin.text == "other_mixin"
  133. child1_child_mixin = driver.find_element(By.ID, "child1-child-mixin")
  134. assert child1_child_mixin.text == "child_mixin"
  135. child2_basevar = driver.find_element(By.ID, "child2-basevar")
  136. assert child2_basevar.text == "basevar2"
  137. child2_mixin = driver.find_element(By.ID, "child2-mixin")
  138. assert child2_mixin.text == "mixin"
  139. child2_other_mixin = driver.find_element(By.ID, "child2-other-mixin")
  140. assert child2_other_mixin.text == "other_mixin"
  141. child2_child_mixin = driver.find_element(By.ID, "child2-child-mixin")
  142. assert child2_child_mixin.text == "child_mixin"
  143. child3_basevar = driver.find_element(By.ID, "child3-basevar")
  144. assert child3_basevar.text == "basevar2"
  145. child3_mixin = driver.find_element(By.ID, "child3-mixin")
  146. assert child3_mixin.text == "mixin"
  147. child3_other_mixin = driver.find_element(By.ID, "child3-other-mixin")
  148. assert child3_other_mixin.text == "other_mixin"
  149. child3_childvar = driver.find_element(By.ID, "child3-childvar")
  150. assert child3_childvar.text == "childvar"
  151. child3_child_mixin = driver.find_element(By.ID, "child3-child-mixin")
  152. assert child3_child_mixin.text == "child_mixin"