Skip to content

Commit

Permalink
[New] shallow: setProps(): Add callback argument
Browse files Browse the repository at this point in the history
  • Loading branch information
emuraton authored and ljharb committed Jul 22, 2018
1 parent 20bfdf6 commit 34d69ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1296,8 +1296,8 @@ describe('shallow', () => {
wrapper.setProps({ id: 'bar', foo: 'bla' }, () => {
expect(wrapper.find('.bar')).to.have.lengthOf(1);
});
expect(wrapper.find('.bar')).to.have.lengthOf(0);
});
expect(wrapper.find('.foo')).to.have.lengthOf(0);
});

it('should call componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate, and componentDidUpdate with merged newProps', () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
} from './RSTTraversal';
import { buildPredicate, reduceTreesBySelector } from './selectors';

const noop = () => {};

const NODE = sym('__node__');
const NODES = sym('__nodes__');
const RENDERER = sym('__renderer__');
Expand Down Expand Up @@ -376,13 +378,19 @@ class ShallowWrapper {
* NOTE: can only be called on a wrapper instance that is also the root instance.
*
* @param {Object} props object
* @param {Function} cb - callback function
* @returns {ShallowWrapper}
*/
setProps(props) {
setProps(props, callback = noop) {
if (this[ROOT] !== this) {
throw new Error('ShallowWrapper::setProps() can only be called on the root');
}
return this.rerender(props);
if (typeof callback !== 'function') {
throw new TypeError('ShallowWrapper::setProps() expects a function as its second argument');
}
this.rerender(props);
callback();
return this;
}

/**
Expand Down

0 comments on commit 34d69ce

Please sign in to comment.