test_imports.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import pytest
  2. from reflex.utils.imports import (
  3. ImportDict,
  4. ImportVar,
  5. ParsedImportDict,
  6. merge_imports,
  7. parse_imports,
  8. )
  9. @pytest.mark.parametrize(
  10. "import_var, expected_name",
  11. [
  12. (
  13. ImportVar(tag="BaseTag"),
  14. "BaseTag",
  15. ),
  16. (
  17. ImportVar(tag="BaseTag", alias="AliasTag"),
  18. "BaseTag as AliasTag",
  19. ),
  20. (
  21. ImportVar(tag="BaseTag", is_default=True),
  22. "BaseTag",
  23. ),
  24. (
  25. ImportVar(tag="BaseTag", is_default=True, alias="AliasTag"),
  26. "AliasTag",
  27. ),
  28. (
  29. ImportVar(tag="BaseTag", is_default=False),
  30. "BaseTag",
  31. ),
  32. (
  33. ImportVar(tag="BaseTag", is_default=False, alias="AliasTag"),
  34. "BaseTag as AliasTag",
  35. ),
  36. ],
  37. )
  38. def test_import_var(import_var, expected_name):
  39. """Test that the import var name is computed correctly.
  40. Args:
  41. import_var: The import var.
  42. expected_name: The expected name.
  43. """
  44. assert import_var.name == expected_name
  45. @pytest.mark.parametrize(
  46. "input_1, input_2, output",
  47. [
  48. (
  49. {"react": {"Component"}},
  50. {"react": {"Component"}, "react-dom": {"render"}},
  51. {"react": {ImportVar("Component")}, "react-dom": {ImportVar("render")}},
  52. ),
  53. (
  54. {"react": {"Component"}, "next/image": {"Image"}},
  55. {"react": {"Component"}, "react-dom": {"render"}},
  56. {
  57. "react": {ImportVar("Component")},
  58. "react-dom": {ImportVar("render")},
  59. "next/image": {ImportVar("Image")},
  60. },
  61. ),
  62. (
  63. {"react": {"Component"}},
  64. {"": {"some/custom.css"}},
  65. {"react": {ImportVar("Component")}, "": {ImportVar("some/custom.css")}},
  66. ),
  67. ],
  68. )
  69. def test_merge_imports(input_1, input_2, output):
  70. """Test that imports are merged correctly.
  71. Args:
  72. input_1: The first dict to merge.
  73. input_2: The second dict to merge.
  74. output: The expected output dict after merging.
  75. """
  76. res = merge_imports(input_1, input_2)
  77. assert res.keys() == output.keys()
  78. for key in output:
  79. assert set(res[key]) == set(output[key])
  80. @pytest.mark.parametrize(
  81. "input, output",
  82. [
  83. ({}, {}),
  84. (
  85. {"react": "Component"},
  86. {"react": [ImportVar(tag="Component")]},
  87. ),
  88. (
  89. {"react": ["Component"]},
  90. {"react": [ImportVar(tag="Component")]},
  91. ),
  92. (
  93. {"react": ["Component", ImportVar(tag="useState")]},
  94. {"react": [ImportVar(tag="Component"), ImportVar(tag="useState")]},
  95. ),
  96. (
  97. {"react": ["Component"], "foo": "anotherFunction"},
  98. {
  99. "react": [ImportVar(tag="Component")],
  100. "foo": [ImportVar(tag="anotherFunction")],
  101. },
  102. ),
  103. ],
  104. )
  105. def test_parse_imports(input: ImportDict, output: ParsedImportDict):
  106. assert parse_imports(input) == output