diff --git a/docs/README.md b/docs/README.md index 1c8a54ff5..715331870 100644 --- a/docs/README.md +++ b/docs/README.md @@ -49,7 +49,7 @@ * [setContext(context)](/docs/api/ShallowWrapper/setContext.md) * [setProps(nextProps)](/docs/api/ShallowWrapper/setProps.md) * [setState(nextState)](/docs/api/ShallowWrapper/setState.md) - * [shallow()](/docs/api/ShallowWrapper/shallow.md) + * [shallow([options])](/docs/api/ShallowWrapper/shallow.md) * [simulate(event[, data])](/docs/api/ShallowWrapper/simulate.md) * [some(selector)](/docs/api/ShallowWrapper/some.md) * [someWhere(predicate)](/docs/api/ShallowWrapper/someWhere.md) diff --git a/docs/api/ShallowWrapper/shallow.md b/docs/api/ShallowWrapper/shallow.md index 4b6c94b58..bc24de457 100644 --- a/docs/api/ShallowWrapper/shallow.md +++ b/docs/api/ShallowWrapper/shallow.md @@ -1,10 +1,15 @@ -# `.shallow() => ShallowWrapper` +# `.shallow([options]) => ShallowWrapper` Shallow renders the current node and returns a shallow wrapper around it. NOTE: can only be called on wrapper of a single node. +#### Arguments + +1. `options` (`Object` [optional]): +- `options.context`: (`Object` [optional]): Context to be passed into the component + #### Returns diff --git a/docs/api/shallow.md b/docs/api/shallow.md index 9c254bba5..6716874ba 100644 --- a/docs/api/shallow.md +++ b/docs/api/shallow.md @@ -97,7 +97,7 @@ Get a wrapper with the direct parent of the current node. #### [`.closest(selector) => ShallowWrapper`](ShallowWrapper/closest.md) Get a wrapper with the first ancestor of the current node to match the provided selector. -#### [`.shallow() => ShallowWrapper`](ShallowWrapper/shallow.md) +#### [`.shallow([options]) => ShallowWrapper`](ShallowWrapper/shallow.md) Shallow renders the current node and returns a shallow wrapper around it. #### [`.render() => CheerioWrapper`](ShallowWrapper/render.md) diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index 832e70605..f711309c4 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -464,10 +464,11 @@ export default class ShallowWrapper { * * NOTE: can only be called on wrapper of a single node. * + * @param options object * @returns {ShallowWrapper} */ - shallow() { - return this.single((n) => new ShallowWrapper(n)); + shallow(options) { + return this.single((n) => new ShallowWrapper(n, null, options)); } /** diff --git a/test/ShallowWrapper-spec.js b/test/ShallowWrapper-spec.js index 3948038a5..6a9e1b3ab 100644 --- a/test/ShallowWrapper-spec.js +++ b/test/ShallowWrapper-spec.js @@ -1828,6 +1828,79 @@ describe('shallow', () => { expect(wrapper.find(Bar).shallow().find('.in-bar')).to.have.length(1); }); + describe('context', () => { + const context = { name: 'foo' }; + + it('can pass in context', () => { + class Bar extends React.Component { + render() { + return
{this.context.name}
; + } + } + Bar.contextTypes = { + name: React.PropTypes.string, + }; + class Foo extends React.Component { + render() { + return ( +
+ +
+ ); + } + } + + const wrapper = shallow(); + expect(wrapper.find(Bar)).to.have.length(1); + expect(wrapper.find(Bar).shallow({ context }).text()).to.equal('foo'); + }); + + it('should not throw if context is passed in but contextTypes is missing', () => { + class Bar extends React.Component { + render() { + return
{this.context.name}
; + } + } + class Foo extends React.Component { + render() { + return ( +
+ +
+ ); + } + } + + const wrapper = shallow(); + expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw(Error); + }); + + it('is instrospectable through context API', () => { + class Bar extends React.Component { + render() { + return
{this.context.name}
; + } + } + Bar.contextTypes = { + name: React.PropTypes.string, + }; + class Foo extends React.Component { + render() { + return ( +
+ +
+ ); + } + } + + const wrapper = shallow().find(Bar).shallow({ context }); + + expect(wrapper.context().name).to.equal(context.name); + expect(wrapper.context('name')).to.equal(context.name); + }); + }); + describeIf(!REACT013, 'stateless function components', () => { it('should return a shallow rendered instance of the current node', () => { const Bar = () => ( @@ -1846,6 +1919,56 @@ describe('shallow', () => { expect(wrapper.find(Bar)).to.have.length(1); expect(wrapper.find(Bar).shallow().find('.in-bar')).to.have.length(1); }); + + describe('context', () => { + const context = { name: 'foo' }; + + it('can pass in context', () => { + const Bar = (props, context) => ( +
{context.name}
+ ); + Bar.contextTypes = { name: React.PropTypes.string }; + const Foo = () => ( +
+ +
+ ); + + const wrapper = shallow(); + expect(wrapper.find(Bar).shallow({ context }).text()).to.equal('foo'); + }); + + it('should not throw if context is passed in but contextTypes is missing', () => { + const Bar = (props, context) => ( +
{context.name}
+ ); + const Foo = () => ( +
+ +
+ ); + + const wrapper = shallow(); + expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw(Error); + }); + + it('is instrospectable through context API', () => { + const Bar = (props, context) => ( +
{context.name}
+ ); + Bar.contextTypes = { name: React.PropTypes.string }; + const Foo = () => ( +
+ +
+ ); + + const wrapper = shallow().find(Bar).shallow({ context }); + + expect(wrapper.context().name).to.equal(context.name); + expect(wrapper.context('name')).to.equal(context.name); + }); + }); }); });