Skip to content

Commit bd296ef

Browse files
committed
Add callback argument for setProps()
1 parent 263bd2e commit bd296ef

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,9 +1272,11 @@ describe('shallow', () => {
12721272
expect(wrapper.find('.foo')).to.have.lengthOf(1);
12731273

12741274
wrapper[sym('__renderer__')].batchedUpdates(() => {
1275-
wrapper.setProps({ id: 'bar', foo: 'bla' });
1276-
expect(wrapper.find('.bar')).to.have.lengthOf(1);
1275+
wrapper.setProps({ id: 'bar', foo: 'bla' }, () => {
1276+
expect(wrapper.find('.bar')).to.have.lengthOf(1);
1277+
});
12771278
});
1279+
expect(wrapper.find('.foo')).to.have.lengthOf(0);
12781280
});
12791281

12801282
it('should call componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate, and componentDidUpdate with merged newProps', () => {

packages/enzyme/src/ShallowWrapper.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,19 @@ class ShallowWrapper {
376376
* NOTE: can only be called on a wrapper instance that is also the root instance.
377377
*
378378
* @param {Object} props object
379+
* @param {Function} cb - callback function
379380
* @returns {ShallowWrapper}
380381
*/
381-
setProps(props) {
382+
setProps(props, callback = noop) {
382383
if (this[ROOT] !== this) {
383384
throw new Error('ShallowWrapper::setProps() can only be called on the root');
384385
}
385-
return this.rerender(props);
386+
if (typeof callback !== 'function') {
387+
throw new TypeError('ShallowWrapper::setProps() expects a function as its second argument');
388+
}
389+
this.rerender(props);
390+
callback();
391+
return this;
386392
}
387393

388394
/**

0 commit comments

Comments
 (0)