Skip to content

Commit 0033663

Browse files
committed
Shallow renderer passes context to componentWillReceiveProps
This parameter was accidentally omitted before. Leland reported it because it impacts Enzyme. I also added a basic lifecycle parameter test for shallow renderer.
1 parent f8062df commit 0033663

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/renderers/testing/ReactShallowRendererEntry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class ReactShallowRenderer {
182182
oldProps !== props &&
183183
typeof this._instance.componentWillReceiveProps === 'function'
184184
) {
185-
this._instance.componentWillReceiveProps(props);
185+
this._instance.componentWillReceiveProps(props, context);
186186
}
187187

188188
// Read state after cWRP in case it calls setState

src/renderers/testing/__tests__/ReactShallowRenderer-test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,83 @@ describe('ReactTestUtils', () => {
333333
expect(result).toEqual(<div />);
334334
});
335335

336+
it('passes expected params to component lifecycle methods', () => {
337+
const componentDidUpdateParams = [];
338+
const componentWillReceivePropsParams = [];
339+
const componentWillUpdateParams = [];
340+
const setStateParams = [];
341+
const shouldComponentUpdateParams = [];
342+
343+
const initialProp = {prop: 'init prop'};
344+
const initialState = {state: 'init state'};
345+
const initialContext = {context: 'init context'};
346+
const updatedState = {state: 'updated state'};
347+
const updatedProp = {prop: 'updated prop'};
348+
const updatedContext = {context: 'updated context'};
349+
350+
class SimpleComponent extends React.Component {
351+
constructor(props, context) {
352+
super(props, context);
353+
this.state = initialState;
354+
}
355+
componentDidUpdate(...args) {
356+
componentDidUpdateParams.push(...args);
357+
}
358+
componentWillReceiveProps(...args) {
359+
componentWillReceivePropsParams.push(...args);
360+
this.setState((...args) => {
361+
setStateParams.push(...args);
362+
return updatedState;
363+
});
364+
}
365+
componentWillUpdate(...args) {
366+
componentWillUpdateParams.push(...args);
367+
}
368+
shouldComponentUpdate(...args) {
369+
shouldComponentUpdateParams.push(...args);
370+
return true;
371+
}
372+
render() {
373+
return null;
374+
}
375+
}
376+
377+
const shallowRenderer = createRenderer();
378+
379+
// No lifecycle hooks should be invoked on initial render
380+
shallowRenderer.render(
381+
React.createElement(SimpleComponent, initialProp),
382+
initialContext,
383+
);
384+
expect(componentDidUpdateParams).toEqual([]);
385+
expect(componentWillReceivePropsParams).toEqual([]);
386+
expect(componentWillUpdateParams).toEqual([]);
387+
expect(setStateParams).toEqual([]);
388+
expect(shouldComponentUpdateParams).toEqual([]);
389+
390+
// Lifecycle hooks should be invoked with the correct prev/next params on update.
391+
shallowRenderer.render(
392+
React.createElement(SimpleComponent, updatedProp),
393+
updatedContext,
394+
);
395+
expect(componentWillReceivePropsParams).toEqual([
396+
updatedProp,
397+
updatedContext,
398+
]);
399+
expect(setStateParams).toEqual([initialState, initialProp]);
400+
expect(shouldComponentUpdateParams).toEqual([
401+
updatedProp,
402+
updatedState,
403+
updatedContext,
404+
]);
405+
expect(componentWillUpdateParams).toEqual([
406+
updatedProp,
407+
updatedState,
408+
updatedContext,
409+
]);
410+
expect(componentDidUpdateParams).toEqual([initialProp, initialState]);
411+
});
412+
336413
it('can shallowly render components with ref as function', () => {
337414
class SimpleComponent extends React.Component {
338415
state = {clicked: false};

0 commit comments

Comments
 (0)