Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 9 additions & 3 deletions src/ReactWrapperComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,18 @@ export default function createWrapperComponent(node, options = {}) {
},
};

if (options.context && node.type.contextTypes) {
if (options.context && (node.type.contextTypes || options.childContextTypes)) {
// 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.
// specified in both the options AND the child component defines `contextTypes` statically
// OR the merged context types for all children (the node component or deeper children) are
// specified in options parameter under childContextTypes.
// In that case, we define both a `getChildContext()` function and a `childContextTypes` prop.
const childContextTypes = node.type.contextTypes || {};
if (options.childContextTypes) {
objectAssign(childContextTypes, options.childContextTypes);
}
objectAssign(spec, {
childContextTypes: node.type.contextTypes,
childContextTypes,
getChildContext() {
return this.state.context;
},
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ describeWithDOM('mount', () => {
expect(wrapper.text()).to.equal('foo');
});

it('can pass context to the child of mounted component', () => {
const SimpleComponent = React.createClass({
contextTypes: {
name: React.PropTypes.string,
},
render() {
return <div>{this.context.name}</div>;
},
});
const ComplexComponent = React.createClass({
render() {
return <div><SimpleComponent /></div>;
},
});

const childContextTypes = {
name: React.PropTypes.string.isRequired,
};
const wrapper = mount(<ComplexComponent />, { context, 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