Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/arrayToTree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ describe('arrayToTree', () => {
])
})

it('should work with nested objects with dataField set to null and some parentIds not existing', () => {
expect(arrayToTree(
([
{ id: '4', custom: 'abc' },
{ id: '31', parentId: '4', custom: '12' },
{ id: '1941', parentId: '418', custom: 'de' },
{ id: '1', parentId: '418', custom: 'ZZZz' },
{ id: '418', custom: 'ü' },
]),
{ id: 'id', parentId: 'parentId', dataField: null },
)).to.deep.equal([
{
id: '4', custom: 'abc', children: [
{ id: '31', parentId: '4', custom: '12', children: [] },
],
},
{
id: '418', custom: 'ü', children: [
{ id: '1941', parentId: '418', custom: 'de', children: [] },
{ id: '1', parentId: '418', custom: 'ZZZz', children: [] },
],
},
])
})

it('should work with nested objects and custom keys with dataField set to null', () => {
expect(arrayToTree(
([
Expand Down
2 changes: 1 addition & 1 deletion src/arrayToTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function arrayToTree (items: Item[], config: Partial<Config> = {}): TreeI

const TreeItem = lookup[itemId]

if (parentId === null) {
if (!parentId) {
// is a root item
rootItems.push(TreeItem)
} else {
Expand Down