Skip to content

Commit

Permalink
[Tests] .setProps: ensure defaultProps are applied
Browse files Browse the repository at this point in the history
Closes #1628
  • Loading branch information
mykhailo-riabokon authored and ljharb committed Jul 11, 2018
1 parent 8d9efda commit 0ee548f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,44 @@ describeWithDOM('mount', () => {
expect(wrapper.props().d).to.equal('e');
});

it('should use defaultProps if new props includes undefined values', () => {
const initialState = { a: 42 };
const context = { b: 7 };
class Foo extends React.Component {
constructor(...args) {
super(...args);
this.state = initialState;
}

componentWillReceiveProps() {}

render() {
return <div className={this.props.className} />;
}
}

const cWRP = sinon.stub(Foo.prototype, 'componentWillReceiveProps');

Foo.defaultProps = {
className: 'default-class',
};
Foo.contextTypes = {
b: PropTypes.number,
};

const wrapper = mount(<Foo className="original" />, { context });

// Set undefined in order to use defaultProps if any
wrapper.setProps({ className: undefined });

expect(cWRP).to.have.property('callCount', 1);
const [args] = cWRP.args;
expect(args).to.eql([
{ className: Foo.defaultProps.className },
context,
]);
});

itIf(!REACT16, 'should throw if an exception occurs during render', () => {
class Trainwreck extends React.Component {
render() {
Expand Down
38 changes: 38 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,44 @@ describe('shallow', () => {
expect(wrapper.first('div').text()).to.equal('yolo');
});

it('should use defaultProps if new props includes undefined values', () => {
const initialState = { a: 42 };
const context = { b: 7 };
class Foo extends React.Component {
constructor(...args) {
super(...args);
this.state = initialState;
}

componentWillReceiveProps() {}

render() {
return <div className={this.props.className} />;
}
}

const cWRP = sinon.stub(Foo.prototype, 'componentWillReceiveProps');

Foo.defaultProps = {
className: 'default-class',
};
Foo.contextTypes = {
b: PropTypes.number,
};

const wrapper = shallow(<Foo className="original" />, { context });

// Set undefined in order to use defaultProps if any
wrapper.setProps({ className: undefined });

expect(cWRP).to.have.property('callCount', 1);
const [args] = cWRP.args;
expect(args).to.eql([
{ className: Foo.defaultProps.className },
context,
]);
});

it('should call componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate, and componentDidUpdate with merged newProps', () => {
const spy = sinon.spy();

Expand Down

0 comments on commit 0ee548f

Please sign in to comment.