test_cond.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import json
  2. from typing import Any
  3. import pytest
  4. import reflex as rx
  5. from reflex.components.base.fragment import Fragment
  6. from reflex.components.chakra.layout.box import Box
  7. from reflex.components.chakra.typography.text import Text
  8. from reflex.components.core.cond import Cond, cond
  9. from reflex.components.core.responsive import (
  10. desktop_only,
  11. mobile_and_tablet,
  12. mobile_only,
  13. tablet_and_desktop,
  14. tablet_only,
  15. )
  16. from reflex.state import BaseState
  17. from reflex.vars import Var
  18. @pytest.fixture
  19. def cond_state(request):
  20. class CondState(BaseState):
  21. value: request.param["value_type"] = request.param["value"] # noqa
  22. return CondState
  23. @pytest.mark.parametrize(
  24. "cond_state",
  25. [
  26. pytest.param({"value_type": bool, "value": True}),
  27. pytest.param({"value_type": int, "value": 0}),
  28. pytest.param({"value_type": str, "value": "true"}),
  29. ],
  30. indirect=True,
  31. )
  32. def test_validate_cond(cond_state: rx.Var):
  33. """Test if cond can be a rx.Var with any values.
  34. Args:
  35. cond_state: A fixture.
  36. """
  37. cond_component = cond(
  38. cond_state.value,
  39. Text.create("cond is True"),
  40. Text.create("cond is False"),
  41. )
  42. cond_dict = cond_component.render() if type(cond_component) == Fragment else {}
  43. assert cond_dict["name"] == "Fragment"
  44. [condition] = cond_dict["children"]
  45. assert condition["cond_state"] == "isTrue(cond_state.value)"
  46. # true value
  47. true_value = condition["true_value"]
  48. assert true_value["name"] == "Fragment"
  49. [true_value_text] = true_value["children"]
  50. assert true_value_text["name"] == "Text"
  51. assert true_value_text["children"][0]["contents"] == "{`cond is True`}"
  52. # false value
  53. false_value = condition["false_value"]
  54. assert false_value["name"] == "Fragment"
  55. [false_value_text] = false_value["children"]
  56. assert false_value_text["name"] == "Text"
  57. assert false_value_text["children"][0]["contents"] == "{`cond is False`}"
  58. @pytest.mark.parametrize(
  59. "c1, c2",
  60. [
  61. (True, False),
  62. (32, 0),
  63. ("hello", ""),
  64. (2.3, 0.0),
  65. ],
  66. )
  67. def test_prop_cond(c1: Any, c2: Any):
  68. """Test if cond can be a prop.
  69. Args:
  70. c1: truth condition value
  71. c2: false condition value
  72. """
  73. prop_cond = cond(
  74. True,
  75. c1,
  76. c2,
  77. )
  78. assert isinstance(prop_cond, Var)
  79. c1 = json.dumps(c1).replace('"', "`")
  80. c2 = json.dumps(c2).replace('"', "`")
  81. assert str(prop_cond) == f"{{isTrue(true) ? {c1} : {c2}}}"
  82. def test_cond_no_else():
  83. """Test if cond can be used without else."""
  84. # Components should support the use of cond without else
  85. comp = cond(True, Text.create("hello"))
  86. assert isinstance(comp, Fragment)
  87. comp = comp.children[0]
  88. assert isinstance(comp, Cond)
  89. assert comp.cond._decode() is True # type: ignore
  90. assert comp.comp1.render() == Fragment.create(Text.create("hello")).render()
  91. assert comp.comp2 == Fragment.create()
  92. # Props do not support the use of cond without else
  93. with pytest.raises(ValueError):
  94. cond(True, "hello") # type: ignore
  95. def test_mobile_only():
  96. """Test the mobile_only responsive component."""
  97. component = mobile_only("Content")
  98. assert isinstance(component, Box)
  99. def test_tablet_only():
  100. """Test the tablet_only responsive component."""
  101. component = tablet_only("Content")
  102. assert isinstance(component, Box)
  103. def test_desktop_only():
  104. """Test the desktop_only responsive component."""
  105. component = desktop_only("Content")
  106. assert isinstance(component, Box)
  107. def test_tablet_and_desktop():
  108. """Test the tablet_and_desktop responsive component."""
  109. component = tablet_and_desktop("Content")
  110. assert isinstance(component, Box)
  111. def test_mobile_and_tablet():
  112. """Test the mobile_and_tablet responsive component."""
  113. component = mobile_and_tablet("Content")
  114. assert isinstance(component, Box)