@@ -10,13 +10,17 @@ describe('arrayToTree', () => {
1010 { id : '1' , parentId : '418' , custom : 'ZZZz' } ,
1111 { id : '418' , parentId : null , custom : 'ü' } ,
1212 ] ) ) . to . deep . equal ( [
13- { data : { id : '4' , parentId : null , custom : 'abc' } , children : [
14- { data : { id : '31' , parentId : '4' , custom : '12' } , children : [ ] } ,
15- ] } ,
16- { data : { id : '418' , parentId : null , custom : 'ü' } , children : [
17- { data : { id : '1941' , parentId : '418' , custom : 'de' } , children : [ ] } ,
18- { data : { id : '1' , parentId : '418' , custom : 'ZZZz' } , children : [ ] } ,
19- ] } ,
13+ {
14+ data : { id : '4' , parentId : null , custom : 'abc' } , children : [
15+ { data : { id : '31' , parentId : '4' , custom : '12' } , children : [ ] } ,
16+ ] ,
17+ } ,
18+ {
19+ data : { id : '418' , parentId : null , custom : 'ü' } , children : [
20+ { data : { id : '1941' , parentId : '418' , custom : 'de' } , children : [ ] } ,
21+ { data : { id : '1' , parentId : '418' , custom : 'ZZZz' } , children : [ ] } ,
22+ ] ,
23+ } ,
2024 ] )
2125 } )
2226
@@ -31,13 +35,40 @@ describe('arrayToTree', () => {
3135 ] as any ) ,
3236 { id : 'num' , parentId : 'ref' } ,
3337 ) ) . to . deep . equal ( [
34- { data : { num : '4' , ref : null , custom : 'abc' } , children : [
35- { data : { num : '31' , ref : '4' , custom : '12' } , children : [ ] } ,
36- ] } ,
37- { data : { num : '418' , ref : null , custom : 'ü' } , children : [
38- { data : { num : '1941' , ref : '418' , custom : 'de' } , children : [ ] } ,
39- { data : { num : '1' , ref : '418' , custom : 'ZZZz' } , children : [ ] } ,
40- ] } ,
38+ {
39+ data : { num : '4' , ref : null , custom : 'abc' } , children : [
40+ { data : { num : '31' , ref : '4' , custom : '12' } , children : [ ] } ,
41+ ] ,
42+ } ,
43+ {
44+ data : { num : '418' , ref : null , custom : 'ü' } , children : [
45+ { data : { num : '1941' , ref : '418' , custom : 'de' } , children : [ ] } ,
46+ { data : { num : '1' , ref : '418' , custom : 'ZZZz' } , children : [ ] } ,
47+ ] ,
48+ } ,
49+ ] )
50+ } )
51+
52+ it ( 'should ignore objects if parentId does not exist' , ( ) => {
53+ expect ( arrayToTree ( [
54+ { id : '4' , parentId : null , custom : 'abc' } ,
55+ { id : '31' , parentId : '4' , custom : '12' } ,
56+ { id : '1941' , parentId : '418' , custom : 'de' } ,
57+ { id : '1' , parentId : '418' , custom : 'ZZZz' } ,
58+ { id : '418' , parentId : null , custom : 'ü' } ,
59+ { id : '1313' , parentId : '13' , custom : 'Not existing' } ,
60+ ] ) ) . to . deep . equal ( [
61+ {
62+ data : { id : '4' , parentId : null , custom : 'abc' } , children : [
63+ { data : { id : '31' , parentId : '4' , custom : '12' } , children : [ ] } ,
64+ ] ,
65+ } ,
66+ {
67+ data : { id : '418' , parentId : null , custom : 'ü' } , children : [
68+ { data : { id : '1941' , parentId : '418' , custom : 'de' } , children : [ ] } ,
69+ { data : { id : '1' , parentId : '418' , custom : 'ZZZz' } , children : [ ] } ,
70+ ] ,
71+ } ,
4172 ] )
4273 } )
4374
0 commit comments