hooks.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { describe, it, expect } from "vitest";
  2. import { flatToTree } from "./hooks";
  3. describe("flatToTree", () => {
  4. it("converts array of elements to tree structure", () => {
  5. const elements = [
  6. { key: "container", type: "stack", props: {}, parentKey: null },
  7. {
  8. key: "text1",
  9. type: "text",
  10. props: { content: "Hello" },
  11. parentKey: "container",
  12. },
  13. {
  14. key: "text2",
  15. type: "text",
  16. props: { content: "World" },
  17. parentKey: "container",
  18. },
  19. ];
  20. const tree = flatToTree(elements);
  21. expect(tree.root).toBe("container");
  22. expect(Object.keys(tree.elements)).toHaveLength(3);
  23. expect(tree.elements["container"]).toBeDefined();
  24. expect(tree.elements["text1"]).toBeDefined();
  25. expect(tree.elements["text2"]).toBeDefined();
  26. });
  27. it("builds parent-child relationships", () => {
  28. const elements = [
  29. { key: "root", type: "stack", props: {}, parentKey: null },
  30. { key: "child1", type: "text", props: {}, parentKey: "root" },
  31. { key: "child2", type: "text", props: {}, parentKey: "root" },
  32. ];
  33. const tree = flatToTree(elements);
  34. expect(tree.elements["root"].children).toHaveLength(2);
  35. expect(tree.elements["root"].children).toContain("child1");
  36. expect(tree.elements["root"].children).toContain("child2");
  37. });
  38. it("handles single root element", () => {
  39. const elements = [
  40. {
  41. key: "only",
  42. type: "text",
  43. props: { content: "Single" },
  44. parentKey: null,
  45. },
  46. ];
  47. const tree = flatToTree(elements);
  48. expect(tree.root).toBe("only");
  49. expect(Object.keys(tree.elements)).toHaveLength(1);
  50. });
  51. it("handles deeply nested elements", () => {
  52. const elements = [
  53. { key: "level0", type: "stack", props: {}, parentKey: null },
  54. { key: "level1", type: "stack", props: {}, parentKey: "level0" },
  55. { key: "level2", type: "stack", props: {}, parentKey: "level1" },
  56. { key: "level3", type: "text", props: {}, parentKey: "level2" },
  57. ];
  58. const tree = flatToTree(elements);
  59. expect(tree.root).toBe("level0");
  60. expect(tree.elements["level0"].children).toContain("level1");
  61. expect(tree.elements["level1"].children).toContain("level2");
  62. expect(tree.elements["level2"].children).toContain("level3");
  63. });
  64. it("preserves element props", () => {
  65. const elements = [
  66. {
  67. key: "btn",
  68. type: "button",
  69. props: { label: "Click me", variant: "primary" },
  70. parentKey: null,
  71. },
  72. ];
  73. const tree = flatToTree(elements);
  74. expect(tree.elements["btn"].props).toEqual({
  75. label: "Click me",
  76. variant: "primary",
  77. });
  78. });
  79. it("preserves visibility conditions", () => {
  80. const elements = [
  81. {
  82. key: "conditional",
  83. type: "text",
  84. props: {},
  85. parentKey: null,
  86. visible: { path: "/isVisible" },
  87. },
  88. ];
  89. const tree = flatToTree(elements);
  90. expect(tree.elements["conditional"].visible).toEqual({
  91. path: "/isVisible",
  92. });
  93. });
  94. it("handles elements with undefined parentKey as root", () => {
  95. const elements = [
  96. { key: "root", type: "stack", props: {} } as {
  97. key: string;
  98. type: string;
  99. props: Record<string, unknown>;
  100. parentKey?: string | null;
  101. },
  102. ];
  103. const tree = flatToTree(elements);
  104. // Elements without parentKey should not become root (only null parentKey)
  105. // This tests the edge case
  106. expect(tree.elements["root"]).toBeDefined();
  107. });
  108. it("handles empty elements array", () => {
  109. const tree = flatToTree([]);
  110. expect(tree.root).toBe("");
  111. expect(Object.keys(tree.elements)).toHaveLength(0);
  112. });
  113. it("handles multiple children correctly", () => {
  114. const elements = [
  115. { key: "parent", type: "grid", props: {}, parentKey: null },
  116. { key: "a", type: "card", props: {}, parentKey: "parent" },
  117. { key: "b", type: "card", props: {}, parentKey: "parent" },
  118. { key: "c", type: "card", props: {}, parentKey: "parent" },
  119. { key: "d", type: "card", props: {}, parentKey: "parent" },
  120. ];
  121. const tree = flatToTree(elements);
  122. expect(tree.elements["parent"].children).toHaveLength(4);
  123. expect(tree.elements["parent"].children).toEqual(["a", "b", "c", "d"]);
  124. });
  125. });