diff --git a/tests/TreeView/toHierarchy.test.ts b/tests/TreeView/toHierarchy.test.ts index 66f946c1..666ed0a7 100644 --- a/tests/TreeView/toHierarchy.test.ts +++ b/tests/TreeView/toHierarchy.test.ts @@ -1,4 +1,4 @@ -import { toHierarchy } from "../../src/utils/toHierarchy"; +import { toHierarchy } from "carbon-components-svelte"; describe("toHierarchy", () => { test("should create a flat hierarchy when no items have parents", () => { @@ -102,4 +102,154 @@ describe("toHierarchy", () => { expect(result[0].nodes?.[0]).not.toHaveProperty("nodes"); }); + + test("should handle empty input array", () => { + const result = toHierarchy< + { id: string | number; parentId?: string | number }, + "parentId" + >([], (item) => item.parentId || null); + expect(result).toEqual([]); + }); + + test("should handle non-existent parent IDs", () => { + const input = [ + { id: 1, name: "Root" }, + { id: 2, name: "Child", pid: 999 }, + ]; + const result = toHierarchy(input, (item) => item.pid); + expect(result).toEqual([ + { id: 1, name: "Root" }, + { id: 2, name: "Child", pid: 999 }, + ]); + }); + + test("should handle deeply nested structures", () => { + const input = [ + { id: 1, name: "Level 1" }, + { id: 2, name: "Level 2", pid: 1 }, + { id: 3, name: "Level 3", pid: 2 }, + { id: 4, name: "Level 4", pid: 3 }, + { id: 5, name: "Level 5", pid: 4 }, + ]; + const result = toHierarchy(input, (item) => item.pid); + expect(result).toEqual([ + { + id: 1, + name: "Level 1", + nodes: [ + { + id: 2, + name: "Level 2", + pid: 1, + nodes: [ + { + id: 3, + name: "Level 3", + pid: 2, + nodes: [ + { + id: 4, + name: "Level 4", + pid: 3, + nodes: [ + { + id: 5, + name: "Level 5", + pid: 4, + }, + ], + }, + ], + }, + ], + }, + ], + }, + ]); + }); + + test("should handle mixed ID types", () => { + const input = [ + { id: "root", name: "Root" }, + { id: 1, name: "Child 1", pid: "root" }, + { id: "2", name: "Child 2", pid: "root" }, + ]; + const result = toHierarchy(input, (item) => item.pid); + expect(result).toEqual([ + { + id: "root", + name: "Root", + nodes: [ + { + id: 1, + name: "Child 1", + pid: "root", + }, + { + id: "2", + name: "Child 2", + pid: "root", + }, + ], + }, + ]); + }); + + test("should preserve additional properties", () => { + const input = [ + { id: 1, name: "Root", extra: "data", meta: { key: "value" } }, + { id: 2, name: "Child", pid: 1, flag: true, count: 42 }, + ]; + const result = toHierarchy(input, (item) => item.pid); + expect(result).toEqual([ + { + id: 1, + name: "Root", + extra: "data", + meta: { key: "value" }, + nodes: [ + { + id: 2, + name: "Child", + pid: 1, + flag: true, + count: 42, + }, + ], + }, + ]); + }); + + test("should handle null/undefined parent IDs", () => { + const input = [ + { id: 1, name: "Root 1" }, + { id: 2, name: "Root 2", pid: null }, + { id: 3, name: "Root 3", pid: undefined }, + { id: 4, name: "Child", pid: 1 }, + ]; + const result = toHierarchy(input, (item) => item.pid); + expect(result).toEqual([ + { + id: 1, + name: "Root 1", + nodes: [ + { + id: 4, + name: "Child", + pid: 1, + }, + ], + }, + { + id: 2, + name: "Root 2", + pid: null, + }, + { + id: 3, + name: "Root 3", + pid: undefined, + }, + ]); + }); });