test_propcond.py 862 B

12345678910111213141516171819202122232425262728293031323334
  1. from typing import Any
  2. import pytest
  3. from pynecone.components.tags.tag import PropCond
  4. from pynecone.utils import wrap
  5. from pynecone.var import BaseVar
  6. @pytest.mark.parametrize(
  7. "prop1,prop2",
  8. [
  9. (1, 3),
  10. (1, "text"),
  11. ("text1", "text2"),
  12. ],
  13. )
  14. def test_validate_propcond(prop1: Any, prop2: Any):
  15. """Test the creation of conditional props.
  16. Args:
  17. prop1: truth condition value
  18. prop2: false condition value
  19. """
  20. prop_cond = PropCond.create(
  21. cond=BaseVar(name="cond_state.value", type_=str), prop1=prop1, prop2=prop2
  22. )
  23. expected_prop1 = wrap(prop1, "'") if isinstance(prop1, str) else prop1
  24. expected_prop2 = wrap(prop2, "'") if isinstance(prop2, str) else prop2
  25. assert str(prop_cond) == (
  26. "{cond_state.value ? " f"{expected_prop1} : " f"{expected_prop2}" "}"
  27. )