test_tailwind.py 4.1 KB

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