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

Call gDSFP with the right state in react-test-render #13030

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,14 @@ class ReactShallowRenderer {
const {type} = this._element;

if (typeof type.getDerivedStateFromProps === 'function') {
const oldState = this._newState || this._instance.state;
const partialState = type.getDerivedStateFromProps.call(
null,
props,
this._instance.state,
oldState,
);

if (partialState != null) {
const oldState = this._newState || this._instance.state;
const newState = Object.assign({}, oldState, partialState);
this._instance.state = this._newState = newState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,39 @@ describe('ReactShallowRenderer', () => {
expect(result.props.children).toEqual(3);
});

it('should call getDerivedStateFromProps with the state set by setState', () => {
class SimpleComponent extends React.Component {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this based on any existing test in other renderers? If not, can you please find an appropriate existing test and then use it as a base?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look for that!

Copy link
Contributor Author

@fatfisz fatfisz Jun 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only test I could find that is related to this was: "should not override state with stale values if prevState is spread within getDerivedStateFromProps" in react/packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js. I modified it a bit (it involved a parent and a child component, but in the case of shallow rendering this seemed to be a bit too complicated).

state = {count: 1};

static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.updateState) {
return {count: nextProps.incrementBy + prevState.count};
}

return null;
}

render() {
return <div>{this.state.count}</div>;
}
}

const shallowRenderer = createRenderer();
shallowRenderer.render(
<SimpleComponent updateState={false} incrementBy={2} />,
);
let instance = shallowRenderer.getMountedInstance();
instance.setState({count: 2});
expect(instance.state.count).toEqual(2);

shallowRenderer.render(
<SimpleComponent updateState={true} incrementBy={2} />,
);
instance = shallowRenderer.getMountedInstance();
instance.setState({count: 2});
expect(instance.state.count).toEqual(4);
});

it('can setState with an updater function', () => {
let instance;

Expand Down