@@ -40,6 +40,48 @@ describe('VariableSizeTree', () => {
4040 let defaultHeight : number ;
4141 let isOpenByDefault : boolean ;
4242
43+ function * treeWalker (
44+ refresh : boolean ,
45+ ) : Generator <
46+ VariableSizeNodeData < ExtendedData > | string | symbol ,
47+ void ,
48+ boolean
49+ > {
50+ const stack : StackElement [ ] = [ ] ;
51+
52+ stack . push ( {
53+ nestingLevel : 0 ,
54+ node : tree ,
55+ } ) ;
56+
57+ while ( stack . length !== 0 ) {
58+ const { node, nestingLevel} = stack . pop ( ) ! ;
59+ const id = node . id . toString ( ) ;
60+
61+ const childrenCount = node . children ? node . children . length : 0 ;
62+
63+ const isOpened = yield refresh
64+ ? {
65+ defaultHeight,
66+ id,
67+ isOpenByDefault,
68+ name : node . name ,
69+ nestingLevel,
70+ }
71+ : id ;
72+
73+ if ( childrenCount && isOpened ) {
74+ // tslint:disable-next-line:increment-decrement
75+ for ( let i = childrenCount - 1 ; i >= 0 ; i -- ) {
76+ stack . push ( {
77+ nestingLevel : nestingLevel + 1 ,
78+ node : node . children ! [ i ] ,
79+ } ) ;
80+ }
81+ }
82+ }
83+ }
84+
4385 beforeEach ( ( ) => {
4486 tree = {
4587 children : [
@@ -53,43 +95,7 @@ describe('VariableSizeTree', () => {
5395 defaultHeight = 30 ;
5496 isOpenByDefault = true ;
5597
56- treeWalkerSpy = jest . fn ( function * (
57- refresh : boolean ,
58- ) : IterableIterator < VariableSizeNodeData < ExtendedData > | string | symbol > {
59- const stack : StackElement [ ] = [ ] ;
60-
61- stack . push ( {
62- nestingLevel : 0 ,
63- node : tree ,
64- } ) ;
65-
66- while ( stack . length !== 0 ) {
67- const { node, nestingLevel} = stack . pop ( ) ! ;
68- const id = node . id . toString ( ) ;
69-
70- const childrenCount = node . children ? node . children . length : 0 ;
71-
72- const isOpened = yield refresh
73- ? {
74- defaultHeight,
75- id,
76- isOpenByDefault,
77- name : node . name ,
78- nestingLevel,
79- }
80- : id ;
81-
82- if ( childrenCount && isOpened ) {
83- // tslint:disable-next-line:increment-decrement
84- for ( let i = childrenCount - 1 ; i >= 0 ; i -- ) {
85- stack . push ( {
86- nestingLevel : nestingLevel + 1 ,
87- node : node . children ! [ i ] ,
88- } ) ;
89- }
90- }
91- }
92- } ) ;
98+ treeWalkerSpy = jest . fn ( treeWalker ) ;
9399
94100 component = mount (
95101 < VariableSizeTree < ExtendedData >
@@ -169,6 +175,26 @@ describe('VariableSizeTree', () => {
169175 ) ;
170176 } ) ;
171177
178+ it ( 'recomputes on new treeWalker' , ( ) => {
179+ treeWalkerSpy = jest . fn ( treeWalker ) ;
180+
181+ component . setProps ( {
182+ treeWalker : treeWalkerSpy ,
183+ } ) ;
184+
185+ expect ( treeWalkerSpy ) . toHaveBeenCalledWith ( true ) ;
186+ } ) ;
187+
188+ it ( 'does not recompute if treeWalker is the same' , ( ) => {
189+ treeWalkerSpy . mockClear ( ) ;
190+
191+ component . setProps ( {
192+ treeWalker : treeWalkerSpy ,
193+ } ) ;
194+
195+ expect ( treeWalkerSpy ) . not . toHaveBeenCalled ( ) ;
196+ } ) ;
197+
172198 describe ( 'component instance' , ( ) => {
173199 let treeInstance : VariableSizeTree < ExtendedData > ;
174200
@@ -447,26 +473,28 @@ describe('VariableSizeTree', () => {
447473 } ) ;
448474
449475 it ( 'provides a toggle function that changes openness state of the specific node' , async ( ) => {
450- const recomputeTreeSpy = spyOn ( treeInstance , 'recomputeTree' ) ;
451- const foo1 = component . state ( ) . records [ 'foo-1' ] ;
476+ const foo1 = component . state ( 'records' ) [ 'foo-1' ] ;
477+
478+ foo1 . height = 50 ;
452479
480+ treeWalkerSpy . mockClear ( ) ;
453481 await foo1 . toggle ( ) ;
454482
455- expect ( recomputeTreeSpy ) . toHaveBeenCalledWith ( {
456- refreshNodes : false ,
457- useDefaultHeight : true ,
458- } ) ;
483+ expect ( treeWalkerSpy ) . toHaveBeenCalledWith ( false ) ;
484+ expect ( foo1 . height ) . toBe ( defaultHeight ) ;
459485 expect ( foo1 . isOpen ) . toBeFalsy ( ) ;
460486 } ) ;
461487
462488 it ( 'resets current height to default' , async ( ) => {
489+ const records = component . state ( 'records' ) ;
490+
463491 // Imitate changing height for the foo-1 node
464492 component . setState ( {
465493 order : [ 'foo-1' ] ,
466494 records : {
467- ...component . state ( ) . records ,
495+ ...records ,
468496 'foo-1' : {
469- ...component . state ( ) . records [ 'foo-1' ] ,
497+ ...records [ 'foo-1' ] ,
470498 height : 60 ,
471499 } ,
472500 } ,
@@ -581,12 +609,20 @@ describe('VariableSizeTree', () => {
581609 } ) ;
582610
583611 it ( 'provides a resize function that changes height of the specific node' , ( ) => {
584- const resetAfterIdSpy = spyOn ( treeInstance , 'resetAfterId' ) ;
585- const foo3 = component . state ( ) . records [ 'foo-3' ] ;
612+ const listInstance : VariableSizeList = component
613+ . find ( VariableSizeList )
614+ . instance ( ) as VariableSizeList ;
615+
616+ const resetAfterIndexSpy = spyOn ( listInstance , 'resetAfterIndex' ) ;
617+ const order = component . state ( 'order' ) ! ;
618+ const foo3 = component . state ( 'records' ) [ 'foo-3' ] ;
586619
587620 foo3 . resize ( 100 , true ) ;
588621
589- expect ( resetAfterIdSpy ) . toHaveBeenCalledWith ( 'foo-3' , true ) ;
622+ expect ( resetAfterIndexSpy ) . toHaveBeenCalledWith (
623+ order . indexOf ( 'foo-3' ) ,
624+ true ,
625+ ) ;
590626 expect ( foo3 . height ) . toBe ( 100 ) ;
591627 } ) ;
592628 } ) ;
0 commit comments