Skip to content

Commit

Permalink
setState returning null and undefined is no-op on the ShallowRenderer (
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 authored and Tejas Kumar committed Aug 26, 2018
1 parent 15a414f commit 5495a35
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ class Updater {
partialState = partialState(currentState, publicInstance.props);
}

// Null and undefined are treated as no-ops.
if (partialState === null || partialState === undefined) {
return;
}

this._renderer._newState = {
...currentState,
...partialState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1305,4 +1305,32 @@ describe('ReactShallowRenderer', () => {
'UNSAFE_componentWillUpdate',
]);
});

it('should stop the upade when setState returns null or undefined', () => {
const log = [];
let instance;
class Component extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
}
render() {
log.push('render');
instance = this;
return null;
}
}
const shallowRenderer = createRenderer();
shallowRenderer.render(<Component />);
log.length = 0;
instance.setState(() => null);
instance.setState(() => undefined);
instance.setState(null);
instance.setState(undefined);
expect(log).toEqual([]);
instance.setState(state => ({count: state.count + 1}));
expect(log).toEqual(['render']);
});
});

0 comments on commit 5495a35

Please sign in to comment.