Skip to content

Commit 0b7c31d

Browse files
Merge pull request #171 from jakubzitny/master
add option for childContextTypes of `ReactWrapper`
2 parents 4bf5b71 + 877b08e commit 0b7c31d

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

docs/api/mount.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('<Foo />', () => {
4545
2. `options` (`Object` [optional]):
4646
- `options.context`: (`Object` [optional]): Context to be passed into the component
4747
- `options.attachTo`: (`DOMElement` [optional]): DOM Element to attach the component to.
48+
- `options.childContextTypes`: (`Object` [optional]): Merged contextTypes for all children of the wrapper.
4849

4950
#### Returns
5051

src/ReactWrapperComponent.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ export default function createWrapperComponent(node, options = {}) {
7171
},
7272
};
7373

74-
if (options.context && node.type.contextTypes) {
74+
if (options.context && (node.type.contextTypes || options.childContextTypes)) {
7575
// For full rendering, we are using this wrapper component to provide context if it is
76-
// specified in both the options AND the child component defines `contextTypes` statically.
76+
// specified in both the options AND the child component defines `contextTypes` statically
77+
// OR the merged context types for all children (the node component or deeper children) are
78+
// specified in options parameter under childContextTypes.
7779
// In that case, we define both a `getChildContext()` function and a `childContextTypes` prop.
80+
const childContextTypes = node.type.contextTypes || {};
81+
if (options.childContextTypes) {
82+
objectAssign(childContextTypes, options.childContextTypes);
83+
}
7884
objectAssign(spec, {
79-
childContextTypes: node.type.contextTypes,
85+
childContextTypes,
8086
getChildContext() {
8187
return this.state.context;
8288
},

src/__tests__/ReactWrapper-spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ describeWithDOM('mount', () => {
2828
expect(wrapper.text()).to.equal('foo');
2929
});
3030

31+
it('can pass context to the child of mounted component', () => {
32+
const SimpleComponent = React.createClass({
33+
contextTypes: {
34+
name: React.PropTypes.string,
35+
},
36+
render() {
37+
return <div>{this.context.name}</div>;
38+
},
39+
});
40+
const ComplexComponent = React.createClass({
41+
render() {
42+
return <div><SimpleComponent /></div>;
43+
},
44+
});
45+
46+
const childContextTypes = {
47+
name: React.PropTypes.string.isRequired,
48+
};
49+
const wrapper = mount(<ComplexComponent />, { context, childContextTypes });
50+
expect(wrapper.find(SimpleComponent)).to.have.length(1);
51+
});
52+
3153
it('should not throw if context is passed in but contextTypes is missing', () => {
3254
const SimpleComponent = React.createClass({
3355
render() {

0 commit comments

Comments
 (0)