Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShallowRenderer.simulate supports batched updates #342

Merged
merged 2 commits into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
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