test_icon.py 2.4 KB

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