-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from riexn/feature-forEach
added forEach functionality
- Loading branch information
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import treeHandler from '../src/index'; | ||
|
||
describe('forEach nodes traverser', () => { | ||
test('loop across all nodes, and modify their properties', () => { | ||
const dataTree: any = { | ||
id: 'A', | ||
children: [ | ||
{ id: 'B', children: [] }, | ||
{ | ||
id: 'C', | ||
children: [{ id: 'K', children: [{ id: 'J', children: [] }] }], | ||
}, | ||
, | ||
], | ||
}; | ||
|
||
const dataTreeLooped: any = { | ||
id: 'A', | ||
title: 'ABC', | ||
children: [ | ||
{ id: 'B', title: 'ABC', children: [] }, | ||
{ | ||
id: 'C', | ||
title: 'ABC', | ||
children: [ | ||
{ | ||
id: 'K', | ||
title: 'ABC', | ||
children: [{ id: 'J', title: 'ABC', children: [] }], | ||
}, | ||
], | ||
}, | ||
, | ||
], | ||
}; | ||
|
||
const tree = treeHandler.parse(dataTree); | ||
tree.forEach((node) => { | ||
node.model = { ...node.model, title: 'ABC' }; | ||
}); | ||
|
||
expect(tree.model).toStrictEqual(dataTreeLooped); | ||
}); | ||
|
||
test('loop across all nodes, and modify their properties based on a condition', () => { | ||
const dataTree: any = { | ||
id: 'A', | ||
children: [ | ||
{ id: 'B', children: [] }, | ||
{ | ||
id: 'C', | ||
children: [{ id: 'K', children: [{ id: 'J', children: [] }] }], | ||
}, | ||
, | ||
], | ||
}; | ||
|
||
const dataTreeLooped: any = { | ||
id: 'A', | ||
title: 'ABC', | ||
children: [ | ||
{ id: 'B', title: 'ABC', children: [] }, | ||
{ | ||
id: 'C', | ||
title: 'ABC', | ||
children: [ | ||
{ | ||
id: 'K', | ||
title: 'This is K', | ||
children: [{ id: 'J', title: 'ABC', children: [] }], | ||
}, | ||
], | ||
}, | ||
, | ||
], | ||
}; | ||
|
||
const tree = treeHandler.parse(dataTree); | ||
tree.forEach((node) => { | ||
if (node.model.id === 'K') { | ||
node.model = { title: 'This is K' }; | ||
} else { | ||
node.model = { title: 'ABC' }; | ||
} | ||
}); | ||
|
||
expect(tree.model).toStrictEqual(dataTreeLooped); | ||
}); | ||
}); |