test_radix_themes.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. @rx.var
  17. def token(self) -> str:
  18. return self.get_token()
  19. def index() -> rx.Component:
  20. return rdxt.box(
  21. rdxt.text_field(id="token", value=State.token, read_only=True),
  22. rdxt.text_field(id="tf-bare", value=State.v, on_change=State.set_v), # type: ignore
  23. rdxt.text_field_root(
  24. rdxt.text_field_slot("🧸"),
  25. rdxt.text_field(id="tf-slotted", value=State.v, on_change=State.set_v), # type: ignore
  26. ),
  27. rdxt.flex(
  28. rdxt.switch(
  29. id="switch1",
  30. checked=State.checked,
  31. on_checked_change=State.set_checked, # type: ignore
  32. ),
  33. rx.cond(
  34. State.checked,
  35. rdxt.text("💡", id="bulb"),
  36. rdxt.text("🌙", id="moon"),
  37. ),
  38. direction="row",
  39. gap="2",
  40. ),
  41. p="5",
  42. )
  43. app = rx.App(
  44. state=State,
  45. theme=rdxt.theme(rdxt.theme_panel(), accent_color="grass"),
  46. )
  47. app.add_page(index)
  48. app.compile()
  49. @pytest.fixture(scope="session")
  50. def radix_themes_app(
  51. tmp_path_factory,
  52. ) -> Generator[AppHarness, None, None]:
  53. """Start BackgroundTask app at tmp_path via AppHarness.
  54. Args:
  55. tmp_path_factory: pytest tmp_path_factory fixture
  56. Yields:
  57. running AppHarness instance
  58. """
  59. with AppHarness.create(
  60. root=tmp_path_factory.mktemp(f"radix_themes_app"),
  61. app_source=RadixThemesApp, # type: ignore
  62. ) as harness:
  63. yield harness
  64. @pytest.fixture
  65. def driver(radix_themes_app: AppHarness) -> Generator[WebDriver, None, None]:
  66. """Get an instance of the browser open to the radix_themes_app app.
  67. Args:
  68. radix_themes_app: harness for BackgroundTask app
  69. Yields:
  70. WebDriver instance.
  71. """
  72. assert radix_themes_app.app_instance is not None, "app is not running"
  73. driver = radix_themes_app.frontend()
  74. try:
  75. yield driver
  76. finally:
  77. driver.quit()
  78. @pytest.fixture()
  79. def token(radix_themes_app: AppHarness, driver: WebDriver) -> str:
  80. """Get a function that returns the active token.
  81. Args:
  82. radix_themes_app: harness for BackgroundTask app.
  83. driver: WebDriver instance.
  84. Returns:
  85. The token for the connected client
  86. """
  87. assert radix_themes_app.app_instance is not None
  88. token_input = driver.find_element(By.ID, "token")
  89. assert token_input
  90. # wait for the backend connection to send the token
  91. token = radix_themes_app.poll_for_value(token_input, timeout=DEFAULT_TIMEOUT * 2)
  92. assert token is not None
  93. return token
  94. def test_radix_themes_app(
  95. radix_themes_app: AppHarness,
  96. driver: WebDriver,
  97. token: str,
  98. ):
  99. """Test that background tasks work as expected.
  100. Args:
  101. radix_themes_app: harness for BackgroundTask app.
  102. driver: WebDriver instance.
  103. token: The token for the connected client.
  104. """
  105. assert radix_themes_app.app_instance is not None
  106. tf_bare = driver.find_element(By.ID, "tf-bare")
  107. tf_slotted = driver.find_element(By.ID, "tf-slotted")
  108. switch = driver.find_element(By.ID, "switch1")
  109. tf_bare.send_keys("hello")
  110. assert radix_themes_app.poll_for_value(tf_slotted) == "hello"
  111. tf_slotted.send_keys(Keys.ARROW_LEFT, Keys.ARROW_LEFT, Keys.ARROW_LEFT, "y je")
  112. assert (
  113. radix_themes_app.poll_for_value(tf_bare, exp_not_equal="hello") == "hey jello"
  114. )
  115. driver.find_element(By.ID, "moon")
  116. switch.click()
  117. time.sleep(0.5)
  118. driver.find_element(By.ID, "bulb")
  119. with pytest.raises(Exception):
  120. driver.find_element(By.ID, "moon")
  121. switch.click()
  122. time.sleep(0.5)
  123. driver.find_element(By.ID, "moon")
  124. with pytest.raises(Exception):
  125. driver.find_element(By.ID, "bulb")