Skip to content

Commit

Permalink
[enzyme-adapter-react-{16,16.1,16.2}] [fix] Filter legacy context on …
Browse files Browse the repository at this point in the history
…childContextTypes when necessary

Fixes #1590.
  • Loading branch information
minznerjosh authored and ljharb committed Jan 7, 2019
1 parent 30749f6 commit c7282eb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Expand Up @@ -29,6 +29,7 @@ import {
ensureKeyOrUndefined,
simulateError,
wrap,
getMaskedContext,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -315,7 +316,7 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
let isDOM = false;
let cachedNode = null;
return {
render(el, context) {
render(el, unmaskedContext) {
cachedNode = el;
/* eslint consistent-return: 0 */
if (typeof el.type === 'string') {
Expand All @@ -328,6 +329,7 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
Component.prototype.isReactComponent
|| Array.isArray(Component.__reactAutoBindPairs) // fallback for createClass components
);
const context = getMaskedContext(Component.contextTypes, unmaskedContext);

if (!isStateful && typeof Component === 'function') {
const wrappedEl = Object.assign(
Expand Down
Expand Up @@ -30,6 +30,7 @@ import {
ensureKeyOrUndefined,
simulateError,
wrap,
getMaskedContext,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -317,7 +318,7 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
let isDOM = false;
let cachedNode = null;
return {
render(el, context) {
render(el, unmaskedContext) {
cachedNode = el;
/* eslint consistent-return: 0 */
if (typeof el.type === 'string') {
Expand All @@ -330,6 +331,7 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
Component.prototype.isReactComponent
|| Array.isArray(Component.__reactAutoBindPairs) // fallback for createClass components
);
const context = getMaskedContext(Component.contextTypes, unmaskedContext);

if (!isStateful && typeof Component === 'function') {
const wrappedEl = Object.assign(
Expand Down
4 changes: 3 additions & 1 deletion packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Expand Up @@ -39,6 +39,7 @@ import {
ensureKeyOrUndefined,
simulateError,
wrap,
getMaskedContext,
} from 'enzyme-adapter-utils';
import findCurrentFiberUsingSlowPath from './findCurrentFiberUsingSlowPath';
import detectFiberTags from './detectFiberTags';
Expand Down Expand Up @@ -343,7 +344,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
let isDOM = false;
let cachedNode = null;
return {
render(el, context) {
render(el, unmaskedContext) {
cachedNode = el;
/* eslint consistent-return: 0 */
if (typeof el.type === 'string') {
Expand All @@ -357,6 +358,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
|| Array.isArray(Component.__reactAutoBindPairs) // fallback for createClass components
);

const context = getMaskedContext(Component.contextTypes, unmaskedContext);
if (!isStateful && typeof Component === 'function') {
const wrappedEl = Object.assign(
(...args) => Component(...args), // eslint-disable-line new-cap
Expand Down
24 changes: 24 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -4480,6 +4480,30 @@ describe('shallow', () => {
expect(wrapper.context().name).to.equal(context.name);
expect(wrapper.context('name')).to.equal(context.name);
});

it('filters context to childContextTypes', () => {
class Bar extends React.Component {
render() {
return <div />;
}
}
Bar.contextTypes = {
name: PropTypes.string,
};
class Foo extends React.Component {
render() {
return (
<div>
<Bar />
</div>
);
}
}

const context = { name: 'foo', hello: 'world' };
const wrapper = shallow(<Foo />, { context });
expect(wrapper.find(Bar).dive().context()).to.eql({ name: 'foo' });
});
});

describeIf(is('> 0.13'), 'stateless function components', () => {
Expand Down

0 comments on commit c7282eb

Please sign in to comment.