Skip to content

Commit

Permalink
add option for childContextTypes of ReactWrapper
Browse files Browse the repository at this point in the history
Fixes #144.
  • Loading branch information
jakubzitny committed Feb 7, 2016
1 parent ac165c8 commit fc07a84
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/api/mount.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('<Foo />', () => {
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

Expand Down
6 changes: 5 additions & 1 deletion src/ReactWrapperComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
28 changes: 28 additions & 0 deletions src/__tests__/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>{this.context.ctx1}{this.context.ctx2}</div>;
},
});
const ComplexComponent = React.createClass({
contextTypes: {
ctx1: React.PropTypes.string.isRequired,
},
render() {
return <div><SimpleComponent /><p>{this.context.ctx1}</p></div>;
},
});

const advancedContext = { ctx1: 'ctx1', ctx2: 'ctx2' };
const childContextTypes = {
ctx1: React.PropTypes.string.isRequired,
ctx2: React.PropTypes.string.isRequired,
};
const wrapper = mount(<ComplexComponent />, { 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() {
Expand Down

0 comments on commit fc07a84

Please sign in to comment.