test_radix_themes.py 4.3 KB

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