1
0

test_tailwind.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. GLOBAL_PARAGRAPH_COLOR = "rgba(0, 0, 242, 1)"
  10. def TailwindApp(
  11. tailwind_disabled: bool = False,
  12. paragraph_text: str = PARAGRAPH_TEXT,
  13. paragraph_class_name: str = PARAGRAPH_CLASS_NAME,
  14. global_paragraph_color: str = GLOBAL_PARAGRAPH_COLOR,
  15. ):
  16. """App with tailwind optionally disabled.
  17. Args:
  18. tailwind_disabled: Whether tailwind is disabled for the app.
  19. paragraph_text: Text for the paragraph.
  20. paragraph_class_name: Tailwind class_name for the paragraph.
  21. global_paragraph_color: Color for the paragraph set in global app styles.
  22. """
  23. import reflex as rx
  24. import reflex.components.radix.themes as rdxt
  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. rdxt.text(paragraph_text, as_="p", class_name=paragraph_class_name),
  30. id="p-content",
  31. )
  32. app = rx.App(style={"p": {"color": global_paragraph_color}})
  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. if tailwind_disabled:
  81. # expect "blue" color from global stylesheet, not "text-red-500" from tailwind utility class
  82. assert p.value_of_css_property("color") == GLOBAL_PARAGRAPH_COLOR
  83. else:
  84. # expect "text-red-500" from tailwind utility class
  85. assert p.value_of_css_property("color") == "rgba(239, 68, 68, 1)"