1
0

test_memo.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """Integration tests for rx.memo components."""
  2. from typing import Generator
  3. import pytest
  4. from selenium.webdriver.common.by import By
  5. from reflex.testing import AppHarness
  6. def MemoApp():
  7. """Reflex app with memo components."""
  8. import reflex as rx
  9. class FooComponent(rx.Fragment):
  10. def add_custom_code(self) -> list[str]:
  11. return [
  12. "const foo = 'bar'",
  13. ]
  14. @rx.memo
  15. def foo_component(t: str):
  16. return FooComponent.create(t, rx.Var("foo"))
  17. @rx.memo
  18. def foo_component2(t: str):
  19. return FooComponent.create(t, rx.Var("foo"))
  20. def index() -> rx.Component:
  21. return rx.vstack(
  22. foo_component(t="foo"), foo_component2(t="bar"), id="memo-custom-code"
  23. )
  24. app = rx.App()
  25. app.add_page(index)
  26. @pytest.fixture()
  27. def memo_app(tmp_path) -> Generator[AppHarness, None, None]:
  28. """Start MemoApp app at tmp_path via AppHarness.
  29. Args:
  30. tmp_path: pytest tmp_path fixture
  31. Yields:
  32. running AppHarness instance
  33. """
  34. with AppHarness.create(
  35. root=tmp_path,
  36. app_source=MemoApp,
  37. ) as harness:
  38. yield harness
  39. @pytest.mark.asyncio
  40. async def test_memo_app(memo_app: AppHarness):
  41. """Render various memo'd components and assert on the output.
  42. Args:
  43. memo_app: harness for MemoApp app
  44. """
  45. assert memo_app.app_instance is not None, "app is not running"
  46. driver = memo_app.frontend()
  47. # check that the output matches
  48. memo_custom_code_stack = driver.find_element(By.ID, "memo-custom-code")
  49. assert memo_custom_code_stack.text == "foobarbarbar"