test_tailwind.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """Test case for disabling tailwind in the config."""
  2. import functools
  3. from typing import Generator
  4. import pytest
  5. from selenium.webdriver.common.by import By
  6. from reflex.testing import AppHarness
  7. PARAGRAPH_TEXT = "Tailwind Is Cool"
  8. PARAGRAPH_CLASS_NAME = "text-red-500"
  9. TEXT_RED_500_COLOR = "rgba(239, 68, 68, 1)"
  10. def TailwindApp(
  11. tailwind_disabled: bool = False,
  12. paragraph_text: str = PARAGRAPH_TEXT,
  13. paragraph_class_name: str = PARAGRAPH_CLASS_NAME,
  14. ):
  15. """App with tailwind optionally disabled.
  16. Args:
  17. tailwind_disabled: Whether tailwind is disabled for the app.
  18. paragraph_text: Text for the paragraph.
  19. paragraph_class_name: Tailwind class_name for the paragraph.
  20. """
  21. import reflex as rx
  22. import reflex.components.radix.themes as rdxt
  23. def index():
  24. return rx.el.div(
  25. rx.text(paragraph_text, class_name=paragraph_class_name),
  26. rx.el.p(paragraph_text, class_name=paragraph_class_name),
  27. rdxt.text(paragraph_text, as_="p", class_name=paragraph_class_name),
  28. id="p-content",
  29. )
  30. app = rx.App(style={"font_family": "monospace"})
  31. app.add_page(index)
  32. if tailwind_disabled:
  33. config = rx.config.get_config()
  34. config.tailwind = None
  35. @pytest.fixture(params=[False, True], ids=["tailwind_enabled", "tailwind_disabled"])
  36. def tailwind_disabled(request) -> bool:
  37. """Tailwind disabled fixture.
  38. Args:
  39. request: pytest request fixture.
  40. Returns:
  41. True if tailwind is disabled, False otherwise.
  42. """
  43. return request.param
  44. @pytest.fixture()
  45. def tailwind_app(tmp_path, tailwind_disabled) -> Generator[AppHarness, None, None]:
  46. """Start TailwindApp app at tmp_path via AppHarness with tailwind disabled via config.
  47. Args:
  48. tmp_path: pytest tmp_path fixture
  49. tailwind_disabled: Whether tailwind is disabled for the app.
  50. Yields:
  51. running AppHarness instance
  52. """
  53. with AppHarness.create(
  54. root=tmp_path,
  55. app_source=functools.partial(TailwindApp, tailwind_disabled=tailwind_disabled), # type: ignore
  56. app_name="tailwind_disabled_app" if tailwind_disabled else "tailwind_app",
  57. ) as harness:
  58. yield harness
  59. def test_tailwind_app(tailwind_app: AppHarness, tailwind_disabled: bool):
  60. """Test that the app can compile without tailwind.
  61. Args:
  62. tailwind_app: AppHarness instance.
  63. tailwind_disabled: Whether tailwind is disabled for the app.
  64. """
  65. assert tailwind_app.app_instance is not None
  66. assert tailwind_app.backend is not None
  67. driver = tailwind_app.frontend()
  68. # Assert the app is stateless.
  69. with pytest.raises(ValueError) as errctx:
  70. _ = tailwind_app.app_instance.state_manager
  71. errctx.match("The state manager has not been initialized.")
  72. # Assert content is visible (and not some error)
  73. content = driver.find_element(By.ID, "p-content")
  74. paragraphs = content.find_elements(By.TAG_NAME, "p")
  75. assert len(paragraphs) == 3
  76. for p in paragraphs:
  77. assert tailwind_app.poll_for_content(p, exp_not_equal="") == PARAGRAPH_TEXT
  78. assert p.value_of_css_property("font-family") == "monospace"
  79. if tailwind_disabled:
  80. # expect default color, not "text-red-500" from tailwind utility class
  81. assert p.value_of_css_property("color") != TEXT_RED_500_COLOR
  82. else:
  83. # expect "text-red-500" from tailwind utility class
  84. assert p.value_of_css_property("color") == TEXT_RED_500_COLOR