Skip to content

Commit 047be68

Browse files
committed
test: add tests for re-computation on new props
1 parent 6dac21a commit 047be68

File tree

2 files changed

+95
-73
lines changed

2 files changed

+95
-73
lines changed

__tests__/FixedSizeTree.spec.tsx

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,43 @@ describe('FixedSizeTree', () => {
3939
let treeWalkerSpy: jest.Mock;
4040
let isOpenByDefault: boolean;
4141

42+
function* treeWalker(
43+
refresh: boolean,
44+
): IterableIterator<FixedSizeNodeData<ExtendedData> | string | symbol> {
45+
const stack: StackElement[] = [];
46+
47+
stack.push({
48+
nestingLevel: 0,
49+
node: tree,
50+
});
51+
52+
while (stack.length !== 0) {
53+
const {node, nestingLevel} = stack.pop()!;
54+
const id = node.id.toString();
55+
56+
const childrenCount = node.children ? node.children.length : 0;
57+
58+
const isOpened = yield refresh
59+
? {
60+
id,
61+
isOpenByDefault,
62+
name: node.name,
63+
nestingLevel,
64+
}
65+
: id;
66+
67+
if (childrenCount && isOpened) {
68+
// tslint:disable-next-line:increment-decrement
69+
for (let i = childrenCount - 1; i >= 0; i--) {
70+
stack.push({
71+
nestingLevel: nestingLevel + 1,
72+
node: node.children![i],
73+
});
74+
}
75+
}
76+
}
77+
}
78+
4279
beforeEach(() => {
4380
tree = {
4481
children: [
@@ -51,42 +88,7 @@ describe('FixedSizeTree', () => {
5188

5289
isOpenByDefault = true;
5390

54-
treeWalkerSpy = jest.fn(function*(
55-
refresh: boolean,
56-
): IterableIterator<FixedSizeNodeData<ExtendedData> | string | symbol> {
57-
const stack: StackElement[] = [];
58-
59-
stack.push({
60-
nestingLevel: 0,
61-
node: tree,
62-
});
63-
64-
while (stack.length !== 0) {
65-
const {node, nestingLevel} = stack.pop()!;
66-
const id = node.id.toString();
67-
68-
const childrenCount = node.children ? node.children.length : 0;
69-
70-
const isOpened = yield refresh
71-
? {
72-
id,
73-
isOpenByDefault,
74-
name: node.name,
75-
nestingLevel,
76-
}
77-
: id;
78-
79-
if (childrenCount && isOpened) {
80-
// tslint:disable-next-line:increment-decrement
81-
for (let i = childrenCount - 1; i >= 0; i--) {
82-
stack.push({
83-
nestingLevel: nestingLevel + 1,
84-
node: node.children![i],
85-
});
86-
}
87-
}
88-
}
89-
});
91+
treeWalkerSpy = jest.fn(treeWalker);
9092

9193
component = mount(
9294
<FixedSizeTree<ExtendedData>
@@ -159,6 +161,15 @@ describe('FixedSizeTree', () => {
159161
expect(component.find(FixedSizeList).prop('children')).toBe(rowComponent);
160162
});
161163

164+
it('recomputes on new props', () => {
165+
treeWalkerSpy = jest.fn(treeWalker);
166+
component.setProps({
167+
treeWalker: treeWalkerSpy,
168+
});
169+
170+
expect(treeWalkerSpy).toHaveBeenCalledWith(true);
171+
});
172+
162173
describe('component instance', () => {
163174
let treeInstance: FixedSizeTree<ExtendedData>;
164175

__tests__/VariableSizeTree.spec.tsx

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,44 @@ describe('VariableSizeTree', () => {
4040
let defaultHeight: number;
4141
let isOpenByDefault: boolean;
4242

43+
function* treeWalker(
44+
refresh: boolean,
45+
): IterableIterator<VariableSizeNodeData<ExtendedData> | string | symbol> {
46+
const stack: StackElement[] = [];
47+
48+
stack.push({
49+
nestingLevel: 0,
50+
node: tree,
51+
});
52+
53+
while (stack.length !== 0) {
54+
const {node, nestingLevel} = stack.pop()!;
55+
const id = node.id.toString();
56+
57+
const childrenCount = node.children ? node.children.length : 0;
58+
59+
const isOpened = yield refresh
60+
? {
61+
defaultHeight,
62+
id,
63+
isOpenByDefault,
64+
name: node.name,
65+
nestingLevel,
66+
}
67+
: id;
68+
69+
if (childrenCount && isOpened) {
70+
// tslint:disable-next-line:increment-decrement
71+
for (let i = childrenCount - 1; i >= 0; i--) {
72+
stack.push({
73+
nestingLevel: nestingLevel + 1,
74+
node: node.children![i],
75+
});
76+
}
77+
}
78+
}
79+
}
80+
4381
beforeEach(() => {
4482
tree = {
4583
children: [
@@ -53,43 +91,7 @@ describe('VariableSizeTree', () => {
5391
defaultHeight = 30;
5492
isOpenByDefault = true;
5593

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-
});
94+
treeWalkerSpy = jest.fn(treeWalker);
9395

9496
component = mount(
9597
<VariableSizeTree<ExtendedData>
@@ -169,6 +171,15 @@ describe('VariableSizeTree', () => {
169171
);
170172
});
171173

174+
it('recomputes on new props', () => {
175+
treeWalkerSpy = jest.fn(treeWalker);
176+
component.setProps({
177+
treeWalker: treeWalkerSpy,
178+
});
179+
180+
expect(treeWalkerSpy).toHaveBeenCalledWith(true);
181+
});
182+
172183
describe('component instance', () => {
173184
let treeInstance: VariableSizeTree<ExtendedData>;
174185

0 commit comments

Comments
 (0)