Skip to content

Commit 7c8ba34

Browse files
authored
Merge pull request #13 from Lodin/feat/props-update-recompute
Recompute on new treeWalker
2 parents 9e40f3d + c323476 commit 7c8ba34

File tree

5 files changed

+367
-301
lines changed

5 files changed

+367
-301
lines changed

__tests__/FixedSizeTree.spec.tsx

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

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

5293
isOpenByDefault = true;
5394

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

9197
component = mount(
9298
<FixedSizeTree<ExtendedData>
@@ -159,6 +165,26 @@ describe('FixedSizeTree', () => {
159165
expect(component.find(FixedSizeList).prop('children')).toBe(rowComponent);
160166
});
161167

168+
it('recomputes on new treeWalker', () => {
169+
treeWalkerSpy = jest.fn(treeWalker);
170+
171+
component.setProps({
172+
treeWalker: treeWalkerSpy,
173+
});
174+
175+
expect(treeWalkerSpy).toHaveBeenCalledWith(true);
176+
});
177+
178+
it('does not recompute if treeWalker is the same', () => {
179+
treeWalkerSpy.mockClear();
180+
181+
component.setProps({
182+
treeWalker: treeWalkerSpy,
183+
});
184+
185+
expect(treeWalkerSpy).not.toHaveBeenCalled();
186+
});
187+
162188
describe('component instance', () => {
163189
let treeInstance: FixedSizeTree<ExtendedData>;
164190

@@ -294,7 +320,7 @@ describe('FixedSizeTree', () => {
294320
});
295321

296322
it('resets current openness to default', async () => {
297-
const {records} = component.state();
323+
const records = component.state('records');
298324

299325
for (const id in records) {
300326
records[id].isOpen = false;
@@ -399,12 +425,12 @@ describe('FixedSizeTree', () => {
399425
});
400426

401427
it('provides a toggle function that changes openness state of the specific node', async () => {
402-
const recomputeTreeSpy = spyOn(treeInstance, 'recomputeTree');
403-
const foo1 = component.state().records['foo-1'];
428+
const foo1 = component.state('records')['foo-1'];
404429

430+
treeWalkerSpy.mockClear();
405431
await foo1.toggle();
406432

407-
expect(recomputeTreeSpy).toHaveBeenCalledWith({refreshNodes: false});
433+
expect(treeWalkerSpy).toHaveBeenCalledWith(false);
408434
expect(foo1.isOpen).toBeFalsy();
409435
});
410436
});

__tests__/VariableSizeTree.spec.tsx

Lines changed: 84 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)