@samitouri / QOS-React / commits / 0bf24cc83e

setState returning null and undefined is no-op on the ShallowRenderer (#12756)

Toru Kobayashi committed May 8, 2018 at 09:31 UTC 0bf24cc83eb8a899c73a96f00c2e63d2db680129
2 files changed +33
packages/react-test-renderer/src/ReactShallowRenderer.js
+5
@@ -301,6 +301,11 @@ class Updater {
301 partialState = partialState(currentState, publicInstance.props);
302 }
303
304 + // Null and undefined are treated as no-ops.
305 + if (partialState === null || partialState === undefined) {
306 + return;
307 + }
308 +
309 this._renderer._newState = {
310 ...currentState,
311 ...partialState,
packages/react-test-renderer/src/__tests__/ReactShallowRenderer-test.js
+28
@@ -1305,4 +1305,32 @@ describe('ReactShallowRenderer', () => {
1305 'UNSAFE_componentWillUpdate',
1306 ]);
1307 });
1308 +
1309 + it('should stop the upade when setState returns null or undefined', () => {
1310 + const log = [];
1311 + let instance;
1312 + class Component extends React.Component {
1313 + constructor(props) {
1314 + super(props);
1315 + this.state = {
1316 + count: 0,
1317 + };
1318 + }
1319 + render() {
1320 + log.push('render');
1321 + instance = this;
1322 + return null;
1323 + }
1324 + }
1325 + const shallowRenderer = createRenderer();
1326 + shallowRenderer.render(<Component />);
1327 + log.length = 0;
1328 + instance.setState(() => null);
1329 + instance.setState(() => undefined);
1330 + instance.setState(null);
1331 + instance.setState(undefined);
1332 + expect(log).toEqual([]);
1333 + instance.setState(state => ({count: state.count + 1}));
1334 + expect(log).toEqual(['render']);
1335 + });
1336 });