Skip to content

Commit 270a544

Browse files
committed
ShallowRenderer supports batched updates
1 parent 91f0f98 commit 270a544

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/ShallowWrapper.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import {
2626
createShallowRenderer,
2727
renderToStaticMarkup,
28+
batchedUpdates,
2829
} from './react-compat';
2930

3031
/**
@@ -352,7 +353,9 @@ export default class ShallowWrapper {
352353
withSetStateAllowed(() => {
353354
// TODO(lmr): create/use synthetic events
354355
// TODO(lmr): emulate React's event propagation
355-
handler(...args);
356+
batchedUpdates(() => {
357+
handler(...args);
358+
});
356359
this.root.update();
357360
});
358361
}

src/react-compat.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ if (REACT013) {
134134
};
135135
}
136136

137+
const batchedUpdates = require('react/lib/ReactUpdates').batchedUpdates;
138+
137139
const {
138140
mockComponent,
139141
isElement,
@@ -163,4 +165,5 @@ export {
163165
childrenToArray,
164166
renderWithOptions,
165167
unmountComponentAtNode,
168+
batchedUpdates,
166169
};

test/ShallowWrapper-spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,34 @@ describe('shallow', () => {
974974
});
975975
});
976976

977+
it('should be batched updates', () => {
978+
let renderCount = 0;
979+
class Foo extends React.Component {
980+
constructor(props) {
981+
super(props);
982+
this.state = {
983+
count: 0,
984+
};
985+
this.onClick = this.onClick.bind(this);
986+
}
987+
onClick() {
988+
this.setState({ count: this.state.count + 1 });
989+
this.setState({ count: this.state.count + 1 });
990+
}
991+
render() {
992+
++renderCount;
993+
return (
994+
<a onClick={this.onClick}>{this.state.count}</a>
995+
);
996+
}
997+
}
998+
999+
const wrapper = shallow(<Foo />);
1000+
wrapper.simulate('click');
1001+
expect(wrapper.text()).to.equal('1');
1002+
expect(renderCount).to.equal(2);
1003+
});
1004+
9771005
});
9781006

9791007
describe('.setState(newState)', () => {

0 commit comments

Comments
 (0)