Skip to content

Commit

Permalink
ShallowRenderer supports batched updates
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Apr 22, 2016
1 parent 91f0f98 commit 270a544
Show file tree
Hide file tree
Showing 3 changed files with 35 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 @@ -25,6 +25,7 @@ import {
import {
createShallowRenderer,
renderToStaticMarkup,
batchedUpdates,
} from './react-compat';

/**
Expand Down Expand Up @@ -352,7 +353,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
3 changes: 3 additions & 0 deletions src/react-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ if (REACT013) {
};
}

const batchedUpdates = require('react/lib/ReactUpdates').batchedUpdates;

const {
mockComponent,
isElement,
Expand Down Expand Up @@ -163,4 +165,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 @@ -974,6 +974,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 270a544

Please sign in to comment.