test_tag.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from typing import Dict
  2. import pytest
  3. from pynecone.components.tags import CondTag, Tag
  4. from pynecone.components.tags.tag import PropCond
  5. from pynecone.event import EventChain, EventHandler, EventSpec
  6. from pynecone.var import BaseVar, Var
  7. def mock_event(arg):
  8. pass
  9. @pytest.mark.parametrize(
  10. "prop,formatted",
  11. [
  12. ("string", '"string"'),
  13. ("{wrapped_string}", "{wrapped_string}"),
  14. (True, "{true}"),
  15. (False, "{false}"),
  16. (123, "{123}"),
  17. (3.14, "{3.14}"),
  18. ([1, 2, 3], "{[1, 2, 3]}"),
  19. (["a", "b", "c"], '{["a", "b", "c"]}'),
  20. ({"a": 1, "b": 2, "c": 3}, '{{"a": 1, "b": 2, "c": 3}}'),
  21. (
  22. EventChain(events=[EventSpec(handler=EventHandler(fn=mock_event))]),
  23. '{() => Event([E("mock_event", {})])}',
  24. ),
  25. (
  26. EventChain(
  27. events=[
  28. EventSpec(
  29. handler=EventHandler(fn=mock_event),
  30. local_args=("e",),
  31. args=(("arg", "e.target.value"),),
  32. )
  33. ]
  34. ),
  35. '{(e) => Event([E("mock_event", {arg:e.target.value})])}',
  36. ),
  37. (
  38. PropCond.create(
  39. cond=BaseVar(name="random_var", type_=str),
  40. prop1="true_value",
  41. prop2="false_value",
  42. ),
  43. "{random_var ? 'true_value' : 'false_value'}",
  44. ),
  45. ],
  46. )
  47. def test_format_value(prop: Var, formatted: str):
  48. """Test that the formatted value of an prop is correct.
  49. Args:
  50. prop: The prop to test.
  51. formatted: The expected formatted value.
  52. """
  53. assert Tag.format_prop(prop) == formatted
  54. @pytest.mark.parametrize(
  55. "props,formatted",
  56. [
  57. ({}, ""),
  58. ({"key": 1}, "key={1}"),
  59. ({"key": "value"}, 'key="value"'),
  60. ({"key": True, "key2": "value2"}, 'key={true}\nkey2="value2"'),
  61. ],
  62. )
  63. def test_format_props(props: Dict[str, Var], formatted: str, windows_platform: bool):
  64. """Test that the formatted props are correct.
  65. Args:
  66. props: The props to test.
  67. formatted: The expected formatted props.
  68. windows_platform: Whether the system is windows.
  69. """
  70. assert Tag(props=props).format_props() == (
  71. formatted.replace("\n", "\r\n") if windows_platform else formatted
  72. )
  73. @pytest.mark.parametrize(
  74. "prop,valid",
  75. [
  76. (1, True),
  77. (3.14, True),
  78. ("string", True),
  79. (False, True),
  80. ([], True),
  81. ({}, False),
  82. (None, False),
  83. ],
  84. )
  85. def test_is_valid_prop(prop: Var, valid: bool):
  86. """Test that the prop is valid.
  87. Args:
  88. prop: The prop to test.
  89. valid: The expected validity of the prop.
  90. """
  91. assert Tag.is_valid_prop(prop) == valid
  92. def test_add_props():
  93. """Test that the props are added."""
  94. tag = Tag().add_props(key="value", key2=42, invalid=None, invalid2={})
  95. assert tag.props["key"] == Var.create("value")
  96. assert tag.props["key2"] == Var.create(42)
  97. assert "invalid" not in tag.props
  98. assert "invalid2" not in tag.props
  99. @pytest.mark.parametrize(
  100. "tag,expected",
  101. [
  102. (Tag(), "</>"),
  103. (Tag(name="br"), "<br/>"),
  104. (Tag(contents="hello"), "<>hello</>"),
  105. (Tag(name="h1", contents="hello"), "<h1>hello</h1>"),
  106. (
  107. Tag(name="box", props={"color": "red", "textAlign": "center"}),
  108. '<box color="red"\ntextAlign="center"/>',
  109. ),
  110. (
  111. Tag(
  112. name="box",
  113. props={"color": "red", "textAlign": "center"},
  114. contents="text",
  115. ),
  116. '<box color="red"\ntextAlign="center">text</box>',
  117. ),
  118. ],
  119. )
  120. def test_format_tag(tag: Tag, expected: str, windows_platform: bool):
  121. """Test that the formatted tag is correct.
  122. Args:
  123. tag: The tag to test.
  124. expected: The expected formatted tag.
  125. windows_platform: Whether the system is windows.
  126. """
  127. expected = expected.replace("\n", "\r\n") if windows_platform else expected
  128. assert str(tag) == expected
  129. def test_format_cond_tag():
  130. """Test that the formatted cond tag is correct."""
  131. tag = CondTag(
  132. true_value=str(Tag(name="h1", contents="True content")),
  133. false_value=str(Tag(name="h2", contents="False content")),
  134. cond=BaseVar(name="logged_in", type_=bool),
  135. )
  136. assert str(tag) == "{logged_in ? <h1>True content</h1> : <h2>False content</h2>}"