Skip to content

Commit c8ee18f

Browse files
koba04gaearon
authored andcommitted
ShallowRenderer should filter context by contextTypes (#11922)
1 parent 63791ae commit c8ee18f

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/ReactShallowRenderer.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ class ReactShallowRenderer {
7070

7171
this._rendering = true;
7272
this._element = element;
73-
this._context = context;
73+
this._context = getMaskedContext(element.type.contextTypes, context);
7474

7575
if (this._instance) {
76-
this._updateClassComponent(element.type, element.props, context);
76+
this._updateClassComponent(element.type, element.props, this._context);
7777
} else {
7878
if (shouldConstruct(element.type)) {
7979
this._instance = new element.type(
8080
element.props,
81-
context,
81+
this._context,
8282
this._updater,
8383
);
8484

@@ -87,7 +87,7 @@ class ReactShallowRenderer {
8787

8888
checkPropTypes(
8989
element.type.contextTypes,
90-
context,
90+
this._context,
9191
'context',
9292
getName(element.type, this._instance),
9393
getStackAddendum,
@@ -96,9 +96,9 @@ class ReactShallowRenderer {
9696
currentlyValidatingElement = null;
9797
}
9898

99-
this._mountClassComponent(element.props, context);
99+
this._mountClassComponent(element.props, this._context);
100100
} else {
101-
this._rendered = element.type(element.props, context);
101+
this._rendered = element.type(element.props, this._context);
102102
}
103103
}
104104

@@ -290,4 +290,15 @@ function shouldConstruct(Component) {
290290
return !!(Component.prototype && Component.prototype.isReactComponent);
291291
}
292292

293+
function getMaskedContext(contextTypes, unmaskedContext) {
294+
if (!contextTypes) {
295+
return emptyObject;
296+
}
297+
const context = {};
298+
for (let key in contextTypes) {
299+
context[key] = unmaskedContext[key];
300+
}
301+
return context;
302+
}
303+
293304
export default ReactShallowRenderer;

src/__tests__/ReactShallowRenderer-test.js

+24
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ describe('ReactShallowRenderer', () => {
216216
</div>
217217
);
218218
}
219+
SomeComponent.contextTypes = {
220+
bar: PropTypes.string,
221+
};
219222

220223
const shallowRenderer = createRenderer();
221224
const result = shallowRenderer.render(<SomeComponent foo={'FOO'} />, {
@@ -428,6 +431,9 @@ describe('ReactShallowRenderer', () => {
428431
super(props, context);
429432
this.state = initialState;
430433
}
434+
static contextTypes = {
435+
context: PropTypes.string,
436+
};
431437
componentDidUpdate(...args) {
432438
componentDidUpdateParams.push(...args);
433439
}
@@ -770,6 +776,24 @@ describe('ReactShallowRenderer', () => {
770776
expect(result).toEqual(<div>foo:baz</div>);
771777
});
772778

779+
it('should filter context by contextTypes', () => {
780+
class SimpleComponent extends React.Component {
781+
static contextTypes = {
782+
foo: PropTypes.string,
783+
};
784+
render() {
785+
return <div>{`${this.context.foo}:${this.context.bar}`}</div>;
786+
}
787+
}
788+
789+
const shallowRenderer = createRenderer();
790+
let result = shallowRenderer.render(<SimpleComponent />, {
791+
foo: 'foo',
792+
bar: 'bar',
793+
});
794+
expect(result).toEqual(<div>foo:undefined</div>);
795+
});
796+
773797
it('can fail context when shallowly rendering', () => {
774798
class SimpleComponent extends React.Component {
775799
static contextTypes = {

0 commit comments

Comments
 (0)