From 2ef8895af2497349f25f86b274fb1c9858e01924 Mon Sep 17 00:00:00 2001 From: blackpost38 Date: Fri, 13 Jan 2017 10:12:49 +0900 Subject: [PATCH] Fix #770 merge options --- src/ShallowWrapper.js | 5 +++-- test/ShallowWrapper-spec.jsx | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index b10eb9401..bc64775d1 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -3,6 +3,7 @@ import flatten from 'lodash/flatten'; import unique from 'lodash/uniq'; import compact from 'lodash/compact'; import cheerio from 'cheerio'; +import assign from 'object.assign'; import ComplexSelector from './ComplexSelector'; import { @@ -1022,7 +1023,7 @@ class ShallowWrapper { * @param options object * @returns {ShallowWrapper} */ - dive(options) { + dive(options = {}) { const name = 'dive'; return this.single(name, (n) => { if (isDOMComponentElement(n)) { @@ -1031,7 +1032,7 @@ class ShallowWrapper { if (!isCustomComponentElement(n)) { throw new TypeError(`ShallowWrapper::${name}() can only be called on components`); } - return new ShallowWrapper(n, null, options); + return new ShallowWrapper(n, null, assign({}, this.options, options)); }); } } diff --git a/test/ShallowWrapper-spec.jsx b/test/ShallowWrapper-spec.jsx index 07a7dc312..663c57a94 100644 --- a/test/ShallowWrapper-spec.jsx +++ b/test/ShallowWrapper-spec.jsx @@ -3842,11 +3842,18 @@ describe('shallow', () => { return ; } } + WrapsRendersDOM.contextTypes = { foo: React.PropTypes.string }; class DoubleWrapsRendersDOM extends React.Component { render() { return ; } } + class ContextWrapsRendersDOM extends React.Component { + render() { + return ; + } + } + ContextWrapsRendersDOM.contextTypes = { foo: React.PropTypes.string }; it('throws on a DOM node', () => { const wrapper = shallow(); @@ -3883,6 +3890,17 @@ describe('shallow', () => { const underwater = wrapper.dive(); expect(underwater.is(RendersDOM)).to.equal(true); }); + + it('should merge and pass options through', () => { + const wrapper = shallow(, { context: { foo: 'hello' } }); + expect(wrapper.context()).to.deep.equal({ foo: 'hello' }); + + let underwater = wrapper.dive(); + expect(underwater.context()).to.deep.equal({ foo: 'hello' }); + + underwater = wrapper.dive({ context: { foo: 'enzyme!' } }); + expect(underwater.context()).to.deep.equal({ foo: 'enzyme!' }); + }); }); describeIf(ITERATOR_SYMBOL, '@@iterator', () => {