mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
feat: add toHierarchy
utility for TreeView
, RecursiveList
(#2072)
Co-authored-by: Bram <bramhavers@gmail.com>
This commit is contained in:
parent
f1a27ec855
commit
48afd18e5e
19 changed files with 413 additions and 23 deletions
105
tests/TreeView/toHierarchy.test.ts
Normal file
105
tests/TreeView/toHierarchy.test.ts
Normal file
|
@ -0,0 +1,105 @@
|
|||
import { toHierarchy } from "../../src/utils/toHierarchy";
|
||||
|
||||
describe("toHierarchy", () => {
|
||||
test("should create a flat hierarchy when no items have parents", () => {
|
||||
const input = [
|
||||
{ id: 1, name: "Item 1" },
|
||||
{ id: 2, name: "Item 2", parentId: "invalid" },
|
||||
];
|
||||
const result = toHierarchy(input, (item) => item.parentId);
|
||||
|
||||
expect(result).toEqual([
|
||||
{ id: 1, name: "Item 1" },
|
||||
{ id: 2, name: "Item 2", parentId: "invalid" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("should create a nested hierarchy with parent-child relationships", () => {
|
||||
const input = [
|
||||
{ id: 1, name: "Parent" },
|
||||
{ id: 2, name: "Child", pid: 1, randomKey: "randomValue" },
|
||||
{ id: 3, name: "Grandchild", pid: 2 },
|
||||
];
|
||||
const result = toHierarchy(input, (item) => item.pid);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
name: "Parent",
|
||||
nodes: [
|
||||
{
|
||||
id: 2,
|
||||
name: "Child",
|
||||
pid: 1,
|
||||
nodes: [
|
||||
{
|
||||
id: 3,
|
||||
name: "Grandchild",
|
||||
pid: 2,
|
||||
},
|
||||
],
|
||||
randomKey: "randomValue",
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test("should handle multiple root nodes with children", () => {
|
||||
const input = [
|
||||
{ id: 1, name: "Root 1" },
|
||||
{ id: 2, name: "Root 2" },
|
||||
{ id: 3, name: "Child 1", pid: 1 },
|
||||
{ id: 4, name: "Child 2", pid: 2 },
|
||||
];
|
||||
const result = toHierarchy(input, (item) => item.pid);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
name: "Root 1",
|
||||
nodes: [
|
||||
{
|
||||
id: 3,
|
||||
name: "Child 1",
|
||||
pid: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Root 2",
|
||||
nodes: [
|
||||
{
|
||||
id: 4,
|
||||
name: "Child 2",
|
||||
pid: 2,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test("should remove empty nodes arrays", () => {
|
||||
const input = [
|
||||
{ id: 1, name: "Root" },
|
||||
{ id: 2, name: "Leaf", pid: 1 },
|
||||
];
|
||||
const result = toHierarchy(input, (item) => item.pid);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
name: "Root",
|
||||
nodes: [
|
||||
{
|
||||
id: 2,
|
||||
name: "Leaf",
|
||||
pid: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
expect(result[0].nodes?.[0]).not.toHaveProperty("nodes");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue