test_icon.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Integration tests for the Icon component."""
  2. from collections.abc import Generator
  3. import pytest
  4. from selenium.webdriver.common.by import By
  5. from reflex.components.lucide.icon import LUCIDE_ICON_LIST
  6. from reflex.testing import AppHarness
  7. from tests.integration.test_dynamic_components import poll_for_result
  8. def Icons():
  9. import reflex as rx
  10. from reflex.components.lucide.icon import LUCIDE_ICON_LIST
  11. app = rx.App()
  12. class State(rx.State):
  13. pass
  14. @app.add_page
  15. def index():
  16. return rx.vstack(
  17. rx.el.input(
  18. custom_attrs={
  19. "id": "token",
  20. },
  21. value=State.router.session.client_token,
  22. is_read_only=True,
  23. ),
  24. *[
  25. rx.el.div(
  26. rx.icon(icon_name),
  27. id=icon_name,
  28. )
  29. for icon_name in LUCIDE_ICON_LIST
  30. ],
  31. )
  32. @pytest.fixture(scope="module")
  33. def icons(tmp_path_factory) -> Generator[AppHarness, None, None]:
  34. """Start Icons app at tmp_path via AppHarness.
  35. Args:
  36. tmp_path_factory: pytest tmp_path_factory fixture
  37. Yields:
  38. running AppHarness instance
  39. """
  40. with AppHarness.create(
  41. root=tmp_path_factory.mktemp("icons"),
  42. app_source=Icons,
  43. ) as harness:
  44. assert harness.app_instance is not None, "app is not running"
  45. yield harness
  46. @pytest.fixture
  47. def driver(icons: AppHarness):
  48. """Get an instance of the browser open to the dynamic components app.
  49. Args:
  50. icons: AppHarness for the dynamic components
  51. Yields:
  52. WebDriver instance.
  53. """
  54. driver = icons.frontend()
  55. try:
  56. token_input = poll_for_result(
  57. lambda: driver.find_element(By.ID, "token"), max_attempts=30
  58. )
  59. assert token_input
  60. # wait for the backend connection to send the token
  61. token = icons.poll_for_value(token_input)
  62. assert token is not None
  63. yield driver
  64. finally:
  65. driver.quit()
  66. def test_icons(driver, icons: AppHarness):
  67. """Test that the var operations produce the right results.
  68. Args:
  69. driver: selenium WebDriver open to the app
  70. icons: AppHarness for the dynamic components
  71. """
  72. for icon_name in LUCIDE_ICON_LIST:
  73. icon = poll_for_result(
  74. lambda icon_name=icon_name: driver.find_element(By.ID, icon_name)
  75. )
  76. assert icon