test_cond.py 3.8 KB

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