test_shared_state.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Test shared state."""
  2. from __future__ import annotations
  3. from typing import Generator
  4. import pytest
  5. from reflex.testing import AppHarness, WebDriver
  6. def SharedStateApp():
  7. """Test that shared state works as expected."""
  8. import reflex as rx
  9. from tests.integration.shared.state import SharedState
  10. class State(SharedState):
  11. pass
  12. def index() -> rx.Component:
  13. return rx.vstack()
  14. app = rx.App()
  15. app.add_page(index)
  16. @pytest.fixture
  17. def shared_state(
  18. tmp_path_factory,
  19. ) -> Generator[AppHarness, None, None]:
  20. """Start SharedStateApp at tmp_path via AppHarness.
  21. Args:
  22. tmp_path_factory: pytest tmp_path_factory fixture
  23. Yields:
  24. running AppHarness instance
  25. """
  26. with AppHarness.create(
  27. root=tmp_path_factory.mktemp("shared_state"),
  28. app_source=SharedStateApp,
  29. ) as harness:
  30. yield harness
  31. @pytest.fixture
  32. def driver(shared_state: AppHarness) -> Generator[WebDriver, None, None]:
  33. """Get an instance of the browser open to the shared_state app.
  34. Args:
  35. shared_state: harness for SharedStateApp
  36. Yields:
  37. WebDriver instance.
  38. """
  39. assert shared_state.app_instance is not None, "app is not running"
  40. driver = shared_state.frontend()
  41. try:
  42. yield driver
  43. finally:
  44. driver.quit()
  45. def test_shared_state(
  46. shared_state: AppHarness,
  47. driver: WebDriver,
  48. ):
  49. """Test that 2 AppHarness instances can share a state (f.e. from a library).
  50. Args:
  51. shared_state: harness for SharedStateApp.
  52. driver: WebDriver instance.
  53. """
  54. assert shared_state.app_instance is not None