test_tailwind.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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)", "rgb(239, 68, 68)"]
  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. class UnusedState(rx.State):
  24. pass
  25. def index():
  26. return rx.el.div(
  27. rx.chakra.text(paragraph_text, class_name=paragraph_class_name),
  28. rx.el.p(paragraph_text, class_name=paragraph_class_name),
  29. rdxt.text(paragraph_text, as_="p", class_name=paragraph_class_name),
  30. id="p-content",
  31. )
  32. app = rx.App(style={"font_family": "monospace"})
  33. app.add_page(index)
  34. if tailwind_disabled:
  35. config = rx.config.get_config()
  36. config.tailwind = None
  37. @pytest.fixture(params=[False, True], ids=["tailwind_enabled", "tailwind_disabled"])
  38. def tailwind_disabled(request) -> bool:
  39. """Tailwind disabled fixture.
  40. Args:
  41. request: pytest request fixture.
  42. Returns:
  43. True if tailwind is disabled, False otherwise.
  44. """
  45. return request.param
  46. @pytest.fixture()
  47. def tailwind_app(tmp_path, tailwind_disabled) -> Generator[AppHarness, None, None]:
  48. """Start TailwindApp app at tmp_path via AppHarness with tailwind disabled via config.
  49. Args:
  50. tmp_path: pytest tmp_path fixture
  51. tailwind_disabled: Whether tailwind is disabled for the app.
  52. Yields:
  53. running AppHarness instance
  54. """
  55. with AppHarness.create(
  56. root=tmp_path,
  57. app_source=functools.partial(TailwindApp, tailwind_disabled=tailwind_disabled), # type: ignore
  58. app_name="tailwind_disabled_app" if tailwind_disabled else "tailwind_app",
  59. ) as harness:
  60. yield harness
  61. def test_tailwind_app(tailwind_app: AppHarness, tailwind_disabled: bool):
  62. """Test that the app can compile without tailwind.
  63. Args:
  64. tailwind_app: AppHarness instance.
  65. tailwind_disabled: Whether tailwind is disabled for the app.
  66. """
  67. assert tailwind_app.app_instance is not None
  68. assert tailwind_app.backend is not None
  69. driver = tailwind_app.frontend()
  70. # Assert the app is stateless.
  71. with pytest.raises(ValueError) as errctx:
  72. _ = tailwind_app.app_instance.state_manager
  73. errctx.match("The state manager has not been initialized.")
  74. # Assert content is visible (and not some error)
  75. content = driver.find_element(By.ID, "p-content")
  76. paragraphs = content.find_elements(By.TAG_NAME, "p")
  77. assert len(paragraphs) == 3
  78. for p in paragraphs:
  79. assert tailwind_app.poll_for_content(p, exp_not_equal="") == PARAGRAPH_TEXT
  80. assert p.value_of_css_property("font-family") == "monospace"
  81. if tailwind_disabled:
  82. # expect default color, not "text-red-500" from tailwind utility class
  83. assert p.value_of_css_property("color") not in TEXT_RED_500_COLOR
  84. else:
  85. # expect "text-red-500" from tailwind utility class
  86. assert p.value_of_css_property("color") in TEXT_RED_500_COLOR