Skip to content
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
Next Next commit
Call gDSFP with the right state in react-test-render
  • Loading branch information
fatfisz committed Jun 12, 2018
commit fdee5c7a6e7665a6c54073f86747ffb04cfed660
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