test_tag.py 3.9 KB

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