Skip to content

Commit 28a8401

Browse files
committed
test: add tests for toggling node programmatically feature
1 parent 0488813 commit 28a8401

File tree

3 files changed

+143
-38
lines changed

3 files changed

+143
-38
lines changed

__tests__/FixedSizeTree.spec.tsx

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,19 +419,80 @@ describe('FixedSizeTree', () => {
419419
});
420420
});
421421

422-
it('provides a toggle function that changes openness state of the specific node', async () => {
423-
const foo1 = component.state('records')['foo-1']!;
422+
it('opens and closes nodes as specified in opennessState', async () => {
423+
await treeInstance.recomputeTree({
424+
opennessState: {
425+
'foo-2': false,
426+
},
427+
});
428+
429+
component.update(); // Update the wrapper to get the latest changes
430+
431+
let {
432+
order,
433+
records: {'foo-1': foo1, 'foo-2': foo2, 'foo-3': foo3},
434+
}: FixedSizeTreeState<FixedSizeNodeData> = component
435+
.find(FixedSizeList)
436+
.prop('itemData');
437+
438+
expect(order).toEqual(['foo-1', 'foo-2', 'foo-3']);
439+
expect(foo1!.isOpen).toBeTruthy();
440+
expect(foo2!.isOpen).not.toBeTruthy();
441+
expect(foo3!.isOpen).toBeTruthy();
442+
443+
await treeInstance.recomputeTree({
444+
opennessState: {
445+
'foo-2': true,
446+
'foo-3': false,
447+
},
448+
});
449+
450+
component.update(); // Update the wrapper to get the latest changes
424451

425-
treeWalkerSpy.mockClear();
452+
({
453+
order,
454+
records: {'foo-1': foo1, 'foo-2': foo2, 'foo-3': foo3},
455+
} = component.find(FixedSizeList).prop('itemData'));
426456

427-
// Imitate the behavior of Node component where toggle is sent without
428-
// context
429-
const {toggle} = foo1;
430-
await toggle();
457+
expect(order).toEqual(['foo-1', 'foo-2', 'foo-3']);
458+
expect(foo1!.isOpen).toBeTruthy();
459+
expect(foo2!.isOpen).toBeTruthy();
460+
expect(foo3!.isOpen).not.toBeTruthy();
461+
});
462+
463+
it('opennessState is overridden by useDefaultOpenness', async () => {
464+
await treeInstance.recomputeTree({
465+
opennessState: {
466+
'foo-2': false,
467+
},
468+
useDefaultOpenness: true,
469+
});
470+
component.update(); // Update the wrapper to get the latest changes
431471

432-
expect(treeWalkerSpy).toHaveBeenCalledWith(false);
433-
expect(foo1.isOpen).toBeFalsy();
472+
const {
473+
records: {'foo-1': foo1, 'foo-2': foo2, 'foo-3': foo3},
474+
}: FixedSizeTreeState<FixedSizeNodeData> = component
475+
.find(FixedSizeList)
476+
.prop('itemData');
477+
478+
expect(foo1!.isOpen).toBeTruthy();
479+
expect(foo2!.isOpen).toBeTruthy();
480+
expect(foo3!.isOpen).toBeTruthy();
434481
});
435482
});
483+
484+
it('provides a toggle function that changes openness state of the specific node', async () => {
485+
const foo1 = component.state('records')['foo-1']!;
486+
487+
treeWalkerSpy.mockClear();
488+
489+
// Imitate the behavior of Node component where toggle is sent without
490+
// context
491+
const {toggle} = foo1;
492+
await toggle();
493+
494+
expect(treeWalkerSpy).toHaveBeenCalledWith(false);
495+
expect(foo1.isOpen).toBeFalsy();
496+
});
436497
});
437498
});

__tests__/VariableSizeTree.spec.tsx

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -607,23 +607,84 @@ describe('VariableSizeTree', () => {
607607
);
608608
});
609609

610-
it('provides a resize function that changes height of the specific node', () => {
611-
const listInstance: VariableSizeList = component
610+
it('opens and closes nodes as specified in opennessState', async () => {
611+
await treeInstance.recomputeTree({
612+
opennessState: {
613+
'foo-2': false,
614+
},
615+
});
616+
617+
component.update(); // Update the wrapper to get the latest changes
618+
619+
let {
620+
order,
621+
records: {'foo-1': foo1, 'foo-2': foo2, 'foo-3': foo3},
622+
}: VariableSizeTreeState<VariableSizeNodeData> = component
612623
.find(VariableSizeList)
613-
.instance() as VariableSizeList;
624+
.prop('itemData');
614625

615-
const resetAfterIndexSpy = jest.spyOn(listInstance, 'resetAfterIndex');
616-
const order = component.state('order')!;
617-
const foo3 = component.state('records')['foo-3']!;
626+
expect(order).toEqual(['foo-1', 'foo-2', 'foo-3']);
627+
expect(foo1!.isOpen).toBeTruthy();
628+
expect(foo2!.isOpen).not.toBeTruthy();
629+
expect(foo3!.isOpen).toBeTruthy();
618630

619-
foo3.resize(100, true);
631+
await treeInstance.recomputeTree({
632+
opennessState: {
633+
'foo-2': true,
634+
'foo-3': false,
635+
},
636+
});
620637

621-
expect(resetAfterIndexSpy).toHaveBeenCalledWith(
622-
order.indexOf('foo-3'),
623-
true,
624-
);
625-
expect(foo3.height).toBe(100);
638+
component.update(); // Update the wrapper to get the latest changes
639+
640+
({
641+
order,
642+
records: {'foo-1': foo1, 'foo-2': foo2, 'foo-3': foo3},
643+
} = component.find(VariableSizeList).prop('itemData'));
644+
645+
expect(order).toEqual(['foo-1', 'foo-2', 'foo-3']);
646+
expect(foo1!.isOpen).toBeTruthy();
647+
expect(foo2!.isOpen).toBeTruthy();
648+
expect(foo3!.isOpen).not.toBeTruthy();
626649
});
650+
651+
it('opennessState is overridden by useDefaultOpenness', async () => {
652+
await treeInstance.recomputeTree({
653+
opennessState: {
654+
'foo-2': false,
655+
},
656+
useDefaultOpenness: true,
657+
});
658+
component.update(); // Update the wrapper to get the latest changes
659+
660+
const {
661+
records: {'foo-1': foo1, 'foo-2': foo2, 'foo-3': foo3},
662+
}: VariableSizeTreeState<VariableSizeNodeData> = component
663+
.find(VariableSizeList)
664+
.prop('itemData');
665+
666+
expect(foo1!.isOpen).toBeTruthy();
667+
expect(foo2!.isOpen).toBeTruthy();
668+
expect(foo3!.isOpen).toBeTruthy();
669+
});
670+
});
671+
672+
it('provides a resize function that changes height of the specific node', () => {
673+
const listInstance: VariableSizeList = component
674+
.find(VariableSizeList)
675+
.instance() as VariableSizeList;
676+
677+
const resetAfterIndexSpy = jest.spyOn(listInstance, 'resetAfterIndex');
678+
const order = component.state('order')!;
679+
const foo3 = component.state('records')['foo-3']!;
680+
681+
foo3.resize(100, true);
682+
683+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(
684+
order.indexOf('foo-3'),
685+
true,
686+
);
687+
expect(foo3.height).toBe(100);
627688
});
628689
});
629690
});

src/Tree.tsx

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,6 @@ export type TreeWalker<T> = (
5454
refresh: boolean,
5555
) => Generator<T | string | symbol, void, boolean>;
5656

57-
export type OverridableMethods = Readonly<{
58-
constructRecord: (
59-
data: NodeData,
60-
state: TreeState<any, any, any, any>,
61-
) => NodeRecord<NodeData>;
62-
shouldUpdateRecords: (options: UpdateOptions) => boolean;
63-
updateRecord: (
64-
record: NodeRecord<NodeData>,
65-
recordId: string,
66-
options: UpdateOptions,
67-
) => void;
68-
updateRecordDuringTreeWalk: (
69-
record: NodeRecord<NodeData>,
70-
options: UpdateOptions,
71-
) => void;
72-
}>;
73-
7457
export type TreeProps<
7558
TNodeComponentProps extends NodeComponentProps<TData>,
7659
TData extends NodeData

0 commit comments

Comments
 (0)