From 0ee548f0ebd3ec68b02bcc9f2b8d13d322f94f35 Mon Sep 17 00:00:00 2001 From: mykhailo-riabokon Date: Wed, 11 Jul 2018 14:46:39 +0300 Subject: [PATCH] [Tests] `.setProps`: ensure defaultProps are applied Closes #1628 --- .../test/ReactWrapper-spec.jsx | 38 +++++++++++++++++++ .../test/ShallowWrapper-spec.jsx | 38 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index 0ba159866..c2f98bfd9 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -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
; + } + } + + const cWRP = sinon.stub(Foo.prototype, 'componentWillReceiveProps'); + + Foo.defaultProps = { + className: 'default-class', + }; + Foo.contextTypes = { + b: PropTypes.number, + }; + + const wrapper = mount(, { 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() { diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 521e6d960..e21826a18 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -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
; + } + } + + const cWRP = sinon.stub(Foo.prototype, 'componentWillReceiveProps'); + + Foo.defaultProps = { + className: 'default-class', + }; + Foo.contextTypes = { + b: PropTypes.number, + }; + + const wrapper = shallow(, { 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();