mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-14 09:51:36 +00:00
test(to-hierarchy): increase test coverage
This commit is contained in:
parent
90b96a2fed
commit
a514f284e6
1 changed files with 151 additions and 1 deletions
|
@ -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,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue