Skip to content

Commit f5671af

Browse files
committed
feat(with-data) Add more specs to prove mount and unmount behavior
1 parent dd4739b commit f5671af

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

src/hocs/withData/index.spec.js

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,15 @@ class Logger {
9595
}
9696

9797
expectRenderCount(count) {
98-
expect(this.getRenders().length).to.equal(count);
98+
this.expectCountByType('render', count);
99+
}
100+
101+
expectCountByType(type, count) {
102+
expect(this.getByType(type).length).to.equal(count);
103+
}
104+
105+
expectCallSequence(sequence) {
106+
expect(this.getCallSequence()).to.deep.equal(sequence);
99107
}
100108
}
101109

@@ -222,6 +230,45 @@ describe('withData', () => {
222230
});
223231

224232
describe('delay', () => {
233+
it('unmounts and remounts view component when no delay is specified on refecth', () => {
234+
const api = build(createConfig());
235+
const { spy, logger } = createSpyComponent();
236+
const { spy: pendingSpy, logger: pendingLogger } = createSpyComponent();
237+
const comp = withData({
238+
resolve: {
239+
user: ({ userId }) => api.user.getUser(userId)
240+
},
241+
pendingComponent: pendingSpy,
242+
delays: {
243+
refetch: 0
244+
}
245+
})(spy);
246+
247+
let stateContainer = null;
248+
249+
render(comp, { userId: 'peter' }, c => { stateContainer = c; }, ({ userId }) => ({ userId }));
250+
251+
return wait().then(() => {
252+
pendingLogger.expectRenderCount(1);
253+
logger.expectRenderCount(1);
254+
stateContainer.setState({ userId: 'gernot' });
255+
logger.expectRenderCount(1);
256+
logger.expectCountByType('componentWillUnmount', 1);
257+
return wait().then(() => {
258+
logger.expectCountByType('componentWillMount', 2);
259+
pendingLogger.expectRenderCount(2);
260+
logger.expectRenderCount(2);
261+
logger.expectCallSequence([
262+
'componentWillMount',
263+
'render',
264+
'componentWillUnmount',
265+
'componentWillMount',
266+
'render'
267+
]);
268+
});
269+
});
270+
});
271+
225272
it('does not show pending state immediately when delay is requested', () => {
226273
const api = build(createConfig());
227274
const { spy, logger } = createSpyComponent();
@@ -251,6 +298,35 @@ describe('withData', () => {
251298
});
252299
});
253300
});
301+
302+
it('does not needlessly unmount immediately when delay is requested', () => {
303+
const api = build(createConfig());
304+
const { spy, logger } = createSpyComponent();
305+
const comp = withData({
306+
resolve: {
307+
user: ({ userId }) => api.user.getUser(userId)
308+
},
309+
delays: {
310+
refetch: 100
311+
}
312+
})(spy);
313+
314+
let stateContainer = null;
315+
316+
render(comp, { userId: 'peter' }, c => { stateContainer = c; }, ({ userId }) => ({ userId }));
317+
318+
return wait().then(() => {
319+
stateContainer.setState({ userId: 'gernot' });
320+
return wait().then(() => {
321+
logger.expectCallSequence([
322+
'componentWillMount',
323+
'render',
324+
'componentWillReceiveProps',
325+
'render'
326+
]);
327+
});
328+
});
329+
});
254330
});
255331

256332
describe('pagination', () => {

0 commit comments

Comments
 (0)