test_radix_themes.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """Integration test for @radix-ui/themes integration."""
  2. from __future__ import annotations
  3. import time
  4. from typing import Generator
  5. import pytest
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.common.keys import Keys
  8. from reflex.testing import DEFAULT_TIMEOUT, AppHarness, WebDriver
  9. def RadixThemesApp():
  10. """App using radix-themes components."""
  11. import reflex as rx
  12. import reflex.components.radix.themes as rdxt
  13. class State(rx.State):
  14. v: str = ""
  15. checked: bool = False
  16. def index() -> rx.Component:
  17. return rdxt.box(
  18. rdxt.text_field(
  19. id="token", value=State.router.session.client_token, read_only=True
  20. ),
  21. rdxt.text_field(id="tf-bare", value=State.v, on_change=State.set_v), # type: ignore
  22. rdxt.text_field_root(
  23. rdxt.text_field_slot("🧸"),
  24. rdxt.text_field(id="tf-slotted", value=State.v, on_change=State.set_v), # type: ignore
  25. ),
  26. rdxt.flex(
  27. rdxt.switch(
  28. id="switch1",
  29. checked=State.checked,
  30. on_checked_change=State.set_checked, # type: ignore
  31. ),
  32. rx.cond(
  33. State.checked,
  34. rdxt.text("💡", id="bulb"),
  35. rdxt.text("🌙", id="moon"),
  36. ),
  37. direction="row",
  38. gap="2",
  39. ),
  40. rdxt.button("This is a button", size="4", variant="solid", color="plum"),
  41. rdxt.grid(
  42. *[
  43. rdxt.box(rdxt.text(f"Cell {i}"), width="10vw", height="10vw")
  44. for i in range(1, 10)
  45. ],
  46. columns="3",
  47. ),
  48. rdxt.container(
  49. rdxt.section(
  50. rdxt.heading("Section 1"),
  51. rdxt.text(
  52. "text one with ",
  53. rdxt.kbd("K"),
  54. rdxt.kbd("E"),
  55. rdxt.kbd("Y"),
  56. "s",
  57. ),
  58. ),
  59. rdxt.section(
  60. rdxt.heading("Section 2", size="2"),
  61. rdxt.code("Inline code yo"),
  62. ),
  63. rdxt.section(
  64. rdxt.heading("Section 3"),
  65. rdxt.link("Link to google", href="https://google.com"),
  66. rdxt.strong("Strong text"),
  67. rdxt.em("Emphasized text"),
  68. rdxt.blockquote("Blockquote text"),
  69. rdxt.quote("Inline quote"),
  70. ),
  71. ),
  72. p="5",
  73. )
  74. app = rx.App(
  75. state=State,
  76. theme=rdxt.theme(rdxt.theme_panel(), accent_color="grass"),
  77. )
  78. app.add_page(index)
  79. app.compile()
  80. @pytest.fixture(scope="session")
  81. def radix_themes_app(
  82. tmp_path_factory,
  83. ) -> Generator[AppHarness, None, None]:
  84. """Start BackgroundTask app at tmp_path via AppHarness.
  85. Args:
  86. tmp_path_factory: pytest tmp_path_factory fixture
  87. Yields:
  88. running AppHarness instance
  89. """
  90. with AppHarness.create(
  91. root=tmp_path_factory.mktemp(f"radix_themes_app"),
  92. app_source=RadixThemesApp, # type: ignore
  93. ) as harness:
  94. yield harness
  95. @pytest.fixture
  96. def driver(radix_themes_app: AppHarness) -> Generator[WebDriver, None, None]:
  97. """Get an instance of the browser open to the radix_themes_app app.
  98. Args:
  99. radix_themes_app: harness for BackgroundTask app
  100. Yields:
  101. WebDriver instance.
  102. """
  103. assert radix_themes_app.app_instance is not None, "app is not running"
  104. driver = radix_themes_app.frontend()
  105. try:
  106. yield driver
  107. finally:
  108. driver.quit()
  109. @pytest.fixture()
  110. def token(radix_themes_app: AppHarness, driver: WebDriver) -> str:
  111. """Get a function that returns the active token.
  112. Args:
  113. radix_themes_app: harness for BackgroundTask app.
  114. driver: WebDriver instance.
  115. Returns:
  116. The token for the connected client
  117. """
  118. assert radix_themes_app.app_instance is not None
  119. token_input = driver.find_element(By.ID, "token")
  120. assert token_input
  121. # wait for the backend connection to send the token
  122. token = radix_themes_app.poll_for_value(token_input, timeout=DEFAULT_TIMEOUT * 2)
  123. assert token is not None
  124. return token
  125. def test_radix_themes_app(
  126. radix_themes_app: AppHarness,
  127. driver: WebDriver,
  128. token: str,
  129. ):
  130. """Test that background tasks work as expected.
  131. Args:
  132. radix_themes_app: harness for BackgroundTask app.
  133. driver: WebDriver instance.
  134. token: The token for the connected client.
  135. """
  136. assert radix_themes_app.app_instance is not None
  137. tf_bare = driver.find_element(By.ID, "tf-bare")
  138. tf_slotted = driver.find_element(By.ID, "tf-slotted")
  139. switch = driver.find_element(By.ID, "switch1")
  140. tf_bare.send_keys("hello")
  141. assert radix_themes_app.poll_for_value(tf_slotted) == "hello"
  142. tf_slotted.send_keys(Keys.ARROW_LEFT, Keys.ARROW_LEFT, Keys.ARROW_LEFT, "y je")
  143. assert (
  144. radix_themes_app.poll_for_value(tf_bare, exp_not_equal="hello") == "hey jello"
  145. )
  146. driver.find_element(By.ID, "moon")
  147. switch.click()
  148. time.sleep(0.5)
  149. driver.find_element(By.ID, "bulb")
  150. with pytest.raises(Exception):
  151. driver.find_element(By.ID, "moon")
  152. switch.click()
  153. time.sleep(0.5)
  154. driver.find_element(By.ID, "moon")
  155. with pytest.raises(Exception):
  156. driver.find_element(By.ID, "bulb")