Skip to content

Commit

Permalink
Merge pull request #342 from koba04/simulate-supports-batched-updates
Browse files Browse the repository at this point in the history
ShallowRenderer.simulate supports batched updates
  • Loading branch information
aweary committed May 16, 2016
2 parents 952044e + bd5249c commit 49f30b2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import {
createShallowRenderer,
renderToStaticMarkup,
batchedUpdates,
} from './react-compat';

/**
Expand Down Expand Up @@ -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();
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/react-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let findDOMNode;
let childrenToArray;
let renderWithOptions;
let unmountComponentAtNode;
let batchedUpdates;

const React = require('react');

Expand All @@ -20,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
Expand Down Expand Up @@ -76,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.
Expand Down Expand Up @@ -163,4 +166,5 @@ export {
childrenToArray,
renderWithOptions,
unmountComponentAtNode,
batchedUpdates,
};
28 changes: 28 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<a onClick={this.onClick}>{this.state.count}</a>
);
}
}

const wrapper = shallow(<Foo />);
wrapper.simulate('click');
expect(wrapper.text()).to.equal('1');
expect(renderCount).to.equal(2);
});

});

describe('.setState(newState)', () => {
Expand Down

0 comments on commit 49f30b2

Please sign in to comment.