test_tag.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from typing import Dict, List
  2. import pytest
  3. from reflex.components.tags import CondTag, Tag, tagless
  4. from reflex.vars import BaseVar, Var
  5. @pytest.mark.parametrize(
  6. "props,test_props",
  7. [
  8. ({}, []),
  9. ({"key": 1}, ["key={1}"]),
  10. ({"key": "value"}, ["key={`value`}"]),
  11. ({"key": True, "key2": "value2"}, ["key={true}", "key2={`value2`}"]),
  12. ],
  13. )
  14. def test_format_props(props: Dict[str, Var], test_props: List):
  15. """Test that the formatted props are correct.
  16. Args:
  17. props: The props to test.
  18. test_props: The expected props.
  19. """
  20. tag_props = Tag(props=props).format_props()
  21. for i, tag_prop in enumerate(tag_props):
  22. assert tag_prop == test_props[i]
  23. @pytest.mark.parametrize(
  24. "prop,valid",
  25. [
  26. (1, True),
  27. (3.14, True),
  28. ("string", True),
  29. (False, True),
  30. ([], True),
  31. ({}, False),
  32. (None, False),
  33. ],
  34. )
  35. def test_is_valid_prop(prop: Var, valid: bool):
  36. """Test that the prop is valid.
  37. Args:
  38. prop: The prop to test.
  39. valid: The expected validity of the prop.
  40. """
  41. assert Tag.is_valid_prop(prop) == valid
  42. def test_add_props():
  43. """Test that the props are added."""
  44. tag = Tag().add_props(key="value", key2=42, invalid=None, invalid2={})
  45. assert tag.props["key"].equals(Var.create("value"))
  46. assert tag.props["key2"].equals(Var.create(42))
  47. assert "invalid" not in tag.props
  48. assert "invalid2" not in tag.props
  49. @pytest.mark.parametrize(
  50. "tag,expected",
  51. [
  52. (Tag(), {"name": "", "contents": "", "props": {}}),
  53. (Tag(name="br"), {"name": "br", "contents": "", "props": {}}),
  54. (Tag(contents="hello"), {"name": "", "contents": "hello", "props": {}}),
  55. (
  56. Tag(name="h1", contents="hello"),
  57. {"name": "h1", "contents": "hello", "props": {}},
  58. ),
  59. (
  60. Tag(name="box", props={"color": "red", "textAlign": "center"}),
  61. {
  62. "name": "box",
  63. "contents": "",
  64. "props": {"color": "red", "textAlign": "center"},
  65. },
  66. ),
  67. (
  68. Tag(
  69. name="box",
  70. props={"color": "red", "textAlign": "center"},
  71. contents="text",
  72. ),
  73. {
  74. "name": "box",
  75. "contents": "text",
  76. "props": {"color": "red", "textAlign": "center"},
  77. },
  78. ),
  79. ],
  80. )
  81. def test_format_tag(tag: Tag, expected: Dict):
  82. """Test that the tag dict is correct.
  83. Args:
  84. tag: The tag to test.
  85. expected: The expected tag dictionary.
  86. """
  87. tag_dict = dict(tag)
  88. assert tag_dict["name"] == expected["name"]
  89. assert tag_dict["contents"] == expected["contents"]
  90. for prop, prop_value in tag_dict["props"].items():
  91. assert prop_value.equals(Var.create_safe(expected["props"][prop]))
  92. def test_format_cond_tag():
  93. """Test that the cond tag dict is correct."""
  94. tag = CondTag(
  95. true_value=dict(Tag(name="h1", contents="True content")),
  96. false_value=dict(Tag(name="h2", contents="False content")),
  97. cond=BaseVar(_var_name="logged_in", _var_type=bool),
  98. )
  99. tag_dict = dict(tag)
  100. cond, true_value, false_value = (
  101. tag_dict["cond"],
  102. tag_dict["true_value"],
  103. tag_dict["false_value"],
  104. )
  105. assert cond._var_name == "logged_in"
  106. assert cond._var_type == bool
  107. assert true_value["name"] == "h1"
  108. assert true_value["contents"] == "True content"
  109. assert false_value["name"] == "h2"
  110. assert false_value["contents"] == "False content"
  111. def test_tagless_string_representation():
  112. """Test that the string representation of a tagless is correct."""
  113. tag = tagless.Tagless(contents="Hello world")
  114. expected_output = "Hello world"
  115. assert str(tag) == expected_output