test_component.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from typing import Type
  2. import pytest
  3. from pynecone.components.component import Component, ImportDict
  4. from pynecone.event import EventHandler
  5. from pynecone.style import Style
  6. @pytest.fixture
  7. def component1() -> Type[Component]:
  8. """A test component.
  9. Returns:
  10. A test component.
  11. """
  12. class TestComponent1(Component):
  13. def _get_imports(self) -> ImportDict:
  14. return {"react": {"Component"}}
  15. def _get_custom_code(self) -> str:
  16. return "console.log('component1')"
  17. return TestComponent1
  18. @pytest.fixture
  19. def component2() -> Type[Component]:
  20. """A test component.
  21. Returns:
  22. A test component.
  23. """
  24. class TestComponent2(Component):
  25. def _get_imports(self) -> ImportDict:
  26. return {"react-redux": {"connect"}}
  27. def _get_custom_code(self) -> str:
  28. return "console.log('component2')"
  29. return TestComponent2
  30. @pytest.fixture
  31. def on_click1() -> EventHandler:
  32. """A sample on click function.
  33. Returns:
  34. A sample on click function.
  35. """
  36. def on_click1():
  37. pass
  38. return EventHandler(fn=on_click1)
  39. @pytest.fixture
  40. def on_click2() -> EventHandler:
  41. """A sample on click function.
  42. Returns:
  43. A sample on click function.
  44. """
  45. def on_click2():
  46. pass
  47. return EventHandler(fn=on_click2)
  48. def test_set_style_attrs(component1: Type[Component]):
  49. """Test that style attributes are set in the dict.
  50. Args:
  51. component1: A test component.
  52. """
  53. component = component1(color="white", text_align="center")
  54. assert component.style["color"] == "white"
  55. assert component.style["textAlign"] == "center"
  56. def test_create_component(component1: Type[Component]):
  57. """Test that the component is created correctly.
  58. Args:
  59. component1: A test component.
  60. """
  61. children = [component1() for _ in range(3)]
  62. attrs = {"color": "white", "text_align": "center"}
  63. c = component1.create(*children, **attrs)
  64. assert isinstance(c, component1)
  65. assert c.children == children
  66. assert c.style == {"color": "white", "textAlign": "center"}
  67. def test_add_style(component1: Type[Component], component2: Type[Component]):
  68. """Test adding a style to a component.
  69. Args:
  70. component1: A test component.
  71. component2: A test component.
  72. """
  73. style = {
  74. component1: Style({"color": "white"}),
  75. component2: Style({"color": "black"}),
  76. }
  77. c1 = component1().add_style(style) # type: ignore
  78. c2 = component2().add_style(style) # type: ignore
  79. assert c1.style["color"] == "white"
  80. assert c2.style["color"] == "black"
  81. def test_get_imports(component1: Type[Component], component2: Type[Component]):
  82. """Test getting the imports of a component.
  83. Args:
  84. component1: A test component.
  85. component2: A test component.
  86. """
  87. c1 = component1.create()
  88. c2 = component2.create(c1)
  89. assert c1.get_imports() == {"react": {"Component"}}
  90. assert c2.get_imports() == {"react-redux": {"connect"}, "react": {"Component"}}
  91. def test_get_custom_code(component1: Type[Component], component2: Type[Component]):
  92. """Test getting the custom code of a component.
  93. Args:
  94. component1: A test component.
  95. component2: A test component.
  96. """
  97. # Check that the code gets compiled correctly.
  98. c1 = component1.create()
  99. c2 = component2.create()
  100. assert c1.get_custom_code() == {"console.log('component1')"}
  101. assert c2.get_custom_code() == {"console.log('component2')"}
  102. # Check that nesting components compiles both codes.
  103. c1 = component1.create(c2)
  104. assert c1.get_custom_code() == {
  105. "console.log('component1')",
  106. "console.log('component2')",
  107. }
  108. # Check that code is not duplicated.
  109. c1 = component1.create(c2, c2, c1, c1)
  110. assert c1.get_custom_code() == {
  111. "console.log('component1')",
  112. "console.log('component2')",
  113. }