From e98c8305caa8d85899c6074a1ad084795e78ee07 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Fri, 22 Apr 2016 18:42:46 +0900 Subject: [PATCH 1/2] ShallowRenderer supports batched updates --- src/ShallowWrapper.js | 5 ++++- src/react-compat.js | 2 ++ test/ShallowWrapper-spec.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index fc78ba6f4..6d9011ca4 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -29,6 +29,7 @@ import { import { createShallowRenderer, renderToStaticMarkup, + batchedUpdates, } from './react-compat'; /** @@ -468,7 +469,9 @@ export default class ShallowWrapper { withSetStateAllowed(() => { // TODO(lmr): create/use synthetic events // TODO(lmr): emulate React's event propagation - handler(...args); + batchedUpdates(() => { + handler(...args); + }); this.root.update(); }); } diff --git a/src/react-compat.js b/src/react-compat.js index 059aa3e7f..f434853e3 100644 --- a/src/react-compat.js +++ b/src/react-compat.js @@ -12,6 +12,7 @@ let renderWithOptions; let unmountComponentAtNode; const React = require('react'); +const batchedUpdates = require('react/lib/ReactUpdates').batchedUpdates; if (REACT013) { renderToStaticMarkup = React.renderToStaticMarkup; @@ -163,4 +164,5 @@ export { childrenToArray, renderWithOptions, unmountComponentAtNode, + batchedUpdates, }; diff --git a/test/ShallowWrapper-spec.js b/test/ShallowWrapper-spec.js index 7b80ce12d..fcf007827 100644 --- a/test/ShallowWrapper-spec.js +++ b/test/ShallowWrapper-spec.js @@ -1022,6 +1022,34 @@ describe('shallow', () => { }); }); + it('should be batched updates', () => { + let renderCount = 0; + class Foo extends React.Component { + constructor(props) { + super(props); + this.state = { + count: 0, + }; + this.onClick = this.onClick.bind(this); + } + onClick() { + this.setState({ count: this.state.count + 1 }); + this.setState({ count: this.state.count + 1 }); + } + render() { + ++renderCount; + return ( + {this.state.count} + ); + } + } + + const wrapper = shallow(); + wrapper.simulate('click'); + expect(wrapper.text()).to.equal('1'); + expect(renderCount).to.equal(2); + }); + }); describe('.setState(newState)', () => { From bd5249c8590874056ca63c5ba908de067c5b5e90 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Wed, 11 May 2016 00:43:11 +0900 Subject: [PATCH 2/2] Use public API for batchedUpdates --- src/react-compat.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/react-compat.js b/src/react-compat.js index f434853e3..18e8b1e13 100644 --- a/src/react-compat.js +++ b/src/react-compat.js @@ -10,9 +10,9 @@ let findDOMNode; let childrenToArray; let renderWithOptions; let unmountComponentAtNode; +let batchedUpdates; const React = require('react'); -const batchedUpdates = require('react/lib/ReactUpdates').batchedUpdates; if (REACT013) { renderToStaticMarkup = React.renderToStaticMarkup; @@ -21,6 +21,7 @@ if (REACT013) { unmountComponentAtNode = React.unmountComponentAtNode; /* eslint-enable react/no-deprecated */ TestUtils = require('react/addons').addons.TestUtils; + batchedUpdates = require('react/addons').addons.batchedUpdates; const ReactContext = require('react/lib/ReactContext'); // Shallow rendering in 0.13 did not properly support context. This function provides a shim @@ -77,6 +78,7 @@ if (REACT013) { renderToStaticMarkup = require('react-dom/server').renderToStaticMarkup; findDOMNode = ReactDOM.findDOMNode; unmountComponentAtNode = ReactDOM.unmountComponentAtNode; + batchedUpdates = ReactDOM.unstable_batchedUpdates; // We require the testutils, but they don't come with 0.14 out of the box, so we // require them here through this node module. The bummer is that we are not able // to list this as a dependency in package.json and have 0.13 work properly.