From fc07a8423175c9c7dac6d8fb23f29ce04eb86bdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BDitn=C3=BD?= Date: Thu, 4 Feb 2016 15:18:34 -0500 Subject: [PATCH] add option for childContextTypes of `ReactWrapper` Fixes #144. --- docs/api/mount.md | 1 + src/ReactWrapperComponent.jsx | 6 +++++- src/__tests__/ReactWrapper-spec.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/api/mount.md b/docs/api/mount.md index 8a6ae6468..d862cc313 100644 --- a/docs/api/mount.md +++ b/docs/api/mount.md @@ -45,6 +45,7 @@ describe('', () => { 2. `options` (`Object` [optional]): - `options.context`: (`Object` [optional]): Context to be passed into the component - `options.attachTo`: (`DOMElement` [optional]): DOM Element to attach the component to. +- `options.childContextTypes`: (`Object` [optional]): Merged contextTypes for all children of the wrapper. #### Returns diff --git a/src/ReactWrapperComponent.jsx b/src/ReactWrapperComponent.jsx index a599be69d..04b4eb9bd 100644 --- a/src/ReactWrapperComponent.jsx +++ b/src/ReactWrapperComponent.jsx @@ -75,8 +75,12 @@ export default function createWrapperComponent(node, options = {}) { // For full rendering, we are using this wrapper component to provide context if it is // specified in both the options AND the child component defines `contextTypes` statically. // In that case, we define both a `getChildContext()` function and a `childContextTypes` prop. + let childContextTypes = node.type.contextTypes; + if (options.childContextTypes) { + childContextTypes = options.childContextTypes; + } objectAssign(spec, { - childContextTypes: node.type.contextTypes, + childContextTypes, getChildContext() { return this.state.context; }, diff --git a/src/__tests__/ReactWrapper-spec.js b/src/__tests__/ReactWrapper-spec.js index f322a79ea..43a8295a0 100644 --- a/src/__tests__/ReactWrapper-spec.js +++ b/src/__tests__/ReactWrapper-spec.js @@ -28,6 +28,34 @@ describeWithDOM('mount', () => { expect(wrapper.text()).to.equal('foo'); }); + it('can pass advanced context to the child of mounted component', () => { + const SimpleComponent = React.createClass({ + contextTypes: { + ctx1: React.PropTypes.string.isRequired, + ctx2: React.PropTypes.string.isRequired, + }, + render() { + return
{this.context.ctx1}{this.context.ctx2}
; + }, + }); + const ComplexComponent = React.createClass({ + contextTypes: { + ctx1: React.PropTypes.string.isRequired, + }, + render() { + return

{this.context.ctx1}

; + }, + }); + + const advancedContext = { ctx1: 'ctx1', ctx2: 'ctx2' }; + const childContextTypes = { + ctx1: React.PropTypes.string.isRequired, + ctx2: React.PropTypes.string.isRequired, + }; + const wrapper = mount(, { context: advancedContext, childContextTypes }); + expect(wrapper.find(SimpleComponent)).to.have.length(1); + }); + it('should not throw if context is passed in but contextTypes is missing', () => { const SimpleComponent = React.createClass({ render() {