Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shallow renderer passes context to componentWillReceiveProps #10342

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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((...innerArgs) => {
setStateParams.push(...innerArgs);
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