Skip to content

Commit

Permalink
Shallow renderer passes context to componentWillReceiveProps
Browse files Browse the repository at this point in the history
This parameter was accidentally omitted before. Leland reported it because it impacts Enzyme.
I also added a basic lifecycle parameter test for shallow renderer.
  • Loading branch information
bvaughn committed Aug 1, 2017
1 parent f8062df commit 0033663
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/renderers/testing/ReactShallowRendererEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class ReactShallowRenderer {
oldProps !== props &&
typeof this._instance.componentWillReceiveProps === 'function'
) {
this._instance.componentWillReceiveProps(props);
this._instance.componentWillReceiveProps(props, context);
}

// Read state after cWRP in case it calls setState
Expand Down
77 changes: 77 additions & 0 deletions src/renderers/testing/__tests__/ReactShallowRenderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,83 @@ describe('ReactTestUtils', () => {
expect(result).toEqual(<div />);
});

it('passes expected params to component lifecycle methods', () => {
const componentDidUpdateParams = [];
const componentWillReceivePropsParams = [];
const componentWillUpdateParams = [];
const setStateParams = [];
const shouldComponentUpdateParams = [];

const initialProp = {prop: 'init prop'};
const initialState = {state: 'init state'};
const initialContext = {context: 'init context'};
const updatedState = {state: 'updated state'};
const updatedProp = {prop: 'updated prop'};
const updatedContext = {context: 'updated context'};

class SimpleComponent extends React.Component {
constructor(props, context) {
super(props, context);
this.state = initialState;
}
componentDidUpdate(...args) {
componentDidUpdateParams.push(...args);
}
componentWillReceiveProps(...args) {
componentWillReceivePropsParams.push(...args);
this.setState((...args) => {
setStateParams.push(...args);
return updatedState;
});
}
componentWillUpdate(...args) {
componentWillUpdateParams.push(...args);
}
shouldComponentUpdate(...args) {
shouldComponentUpdateParams.push(...args);
return true;
}
render() {
return null;
}
}

const shallowRenderer = createRenderer();

// No lifecycle hooks should be invoked on initial render
shallowRenderer.render(
React.createElement(SimpleComponent, initialProp),
initialContext,
);
expect(componentDidUpdateParams).toEqual([]);
expect(componentWillReceivePropsParams).toEqual([]);
expect(componentWillUpdateParams).toEqual([]);
expect(setStateParams).toEqual([]);
expect(shouldComponentUpdateParams).toEqual([]);

// Lifecycle hooks should be invoked with the correct prev/next params on update.
shallowRenderer.render(
React.createElement(SimpleComponent, updatedProp),
updatedContext,
);
expect(componentWillReceivePropsParams).toEqual([
updatedProp,
updatedContext,
]);
expect(setStateParams).toEqual([initialState, initialProp]);
expect(shouldComponentUpdateParams).toEqual([
updatedProp,
updatedState,
updatedContext,
]);
expect(componentWillUpdateParams).toEqual([
updatedProp,
updatedState,
updatedContext,
]);
expect(componentDidUpdateParams).toEqual([initialProp, initialState]);
});

it('can shallowly render components with ref as function', () => {
class SimpleComponent extends React.Component {
state = {clicked: false};
Expand Down

0 comments on commit 0033663

Please sign in to comment.