test_stateless_app.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Integration tests for a stateless app."""
  2. from typing import Generator
  3. import httpx
  4. import pytest
  5. from playwright.sync_api import Page, expect
  6. import reflex as rx
  7. from reflex.testing import AppHarness
  8. def StatelessApp():
  9. """A stateless app that renders a heading."""
  10. import reflex as rx
  11. def index():
  12. return rx.heading("This is a stateless app")
  13. app = rx.App()
  14. app.add_page(index)
  15. @pytest.fixture(scope="module")
  16. def stateless_app(tmp_path_factory) -> Generator[AppHarness, None, None]:
  17. """Create a stateless app AppHarness.
  18. Args:
  19. tmp_path_factory: pytest fixture for creating temporary directories.
  20. Yields:
  21. AppHarness: A harness for testing the stateless app.
  22. """
  23. with AppHarness.create(
  24. root=tmp_path_factory.mktemp("stateless_app"),
  25. app_source=StatelessApp,
  26. ) as harness:
  27. yield harness
  28. def test_statelessness(stateless_app: AppHarness, page: Page):
  29. """Test that the stateless app renders a heading but backend/_event is not mounted.
  30. Args:
  31. stateless_app: A harness for testing the stateless app.
  32. page: A Playwright page.
  33. """
  34. assert stateless_app.frontend_url is not None
  35. assert stateless_app.backend is not None
  36. assert stateless_app.backend.started
  37. res = httpx.get(rx.config.get_config().api_url + "/_event")
  38. assert res.status_code == 404
  39. res2 = httpx.get(rx.config.get_config().api_url + "/ping")
  40. assert res2.status_code == 200
  41. page.goto(stateless_app.frontend_url)
  42. expect(page.get_by_role("heading")).to_have_text("This is a stateless app")