test_cond.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import json
  2. from typing import Any
  3. import pytest
  4. import pynecone as pc
  5. from pynecone.components.layout.cond import Cond, cond
  6. from pynecone.components.layout.fragment import Fragment
  7. from pynecone.components.typography.text import Text
  8. from pynecone.var import Var
  9. @pytest.fixture
  10. def cond_state(request):
  11. class CondState(pc.State):
  12. value: request.param["value_type"] = request.param["value"] # noqa
  13. return CondState
  14. @pytest.mark.parametrize(
  15. "cond_state",
  16. [
  17. pytest.param({"value_type": bool, "value": True}),
  18. pytest.param({"value_type": int, "value": 0}),
  19. pytest.param({"value_type": str, "value": "true"}),
  20. ],
  21. indirect=True,
  22. )
  23. def test_validate_cond(cond_state: pc.Var):
  24. """Test if cond can be a pc.Var with any values.
  25. Args:
  26. cond_state: A fixture.
  27. """
  28. cond_component = cond(
  29. cond_state.value,
  30. Text.create("cond is True"),
  31. Text.create("cond is False"),
  32. )
  33. cond_dict = cond_component.render() if type(cond_component) == Fragment else {}
  34. assert cond_dict["name"] == "Fragment"
  35. [condition] = cond_dict["children"]
  36. assert condition["cond_state"] == "isTrue(cond_state.value)"
  37. # true value
  38. true_value = condition["true_value"]
  39. assert true_value["name"] == "Fragment"
  40. [true_value_text] = true_value["children"]
  41. assert true_value_text["name"] == "Text"
  42. assert true_value_text["children"][0]["contents"] == "{`cond is True`}"
  43. # false value
  44. false_value = condition["false_value"]
  45. assert false_value["name"] == "Fragment"
  46. [false_value_text] = false_value["children"]
  47. assert false_value_text["name"] == "Text"
  48. assert false_value_text["children"][0]["contents"] == "{`cond is False`}"
  49. @pytest.mark.parametrize(
  50. "c1, c2",
  51. [
  52. (True, False),
  53. (32, 0),
  54. ("hello", ""),
  55. (2.3, 0.0),
  56. ],
  57. )
  58. def test_prop_cond(c1: Any, c2: Any):
  59. """Test if cond can be a prop.
  60. Args:
  61. c1: truth condition value
  62. c2: false condition value
  63. """
  64. prop_cond = cond(
  65. True,
  66. c1,
  67. c2,
  68. )
  69. assert isinstance(prop_cond, Var)
  70. c1 = json.dumps(c1).replace('"', "`")
  71. c2 = json.dumps(c2).replace('"', "`")
  72. assert str(prop_cond) == f"{{isTrue(true) ? {c1} : {c2}}}"
  73. def test_cond_no_else():
  74. """Test if cond can be used without else."""
  75. # Components should support the use of cond without else
  76. comp = cond(True, Text.create("hello"))
  77. assert isinstance(comp, Fragment)
  78. comp = comp.children[0]
  79. assert isinstance(comp, Cond)
  80. assert comp.cond == True # noqa
  81. assert comp.comp1 == Fragment.create(Text.create("hello"))
  82. assert comp.comp2 == Fragment.create()
  83. # Props do not support the use of cond without else
  84. with pytest.raises(ValueError):
  85. cond(True, "hello")