Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1628 - componentWillReceiveProps() receives undefined instead of default #1711

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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