Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for react context element types, fixes #1509 #1513

Merged
merged 7 commits into from
Mar 30, 2018
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
10 changes: 8 additions & 2 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const FunctionalComponent = 1;
const HostPortal = 4;
const HostComponent = 5;
const HostText = 6;
const Mode = 11;
const ContextConsumer = 12;
const ContextProvider = 13;

function nodeAndSiblingsArray(nodeWithSibling) {
const array = [];
Expand Down Expand Up @@ -82,8 +85,6 @@ function toTree(vnode) {
instance: node.stateNode,
rendered: childrenToTree(node.child),
};
case Fragment: // 10
return childrenToTree(node.child);
case FunctionalComponent: // 1
return {
nodeType: 'function',
Expand Down Expand Up @@ -111,6 +112,11 @@ function toTree(vnode) {
}
case HostText: // 6
return node.memoizedProps;
case Fragment: // 10
case Mode: // 11
case ContextProvider: // 13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O.o ContextProvider and ContextConsumer are new node types, they're not just components?

case ContextConsumer: // 12
return childrenToTree(node.child);
default:
throw new Error(`Enzyme Internal Error: unknown node with tag ${node.tag}`);
}
Expand Down
20 changes: 18 additions & 2 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import {
import { ITERATOR_SYMBOL, sym } from 'enzyme/build/Utils';

import './_helpers/setupAdapters';
import { createClass, createPortal } from './_helpers/react-compat';
import { createClass, createContext, createPortal } from './_helpers/react-compat';
import {
describeWithDOM,
describeIf,
itIf,
itWithData,
generateEmptyRenderData,
} from './_helpers';
import { REACT013, REACT014, REACT16, is } from './_helpers/version';
import { REACT013, REACT014, REACT16, REACT163, is } from './_helpers/version';

const getElementPropSelector = prop => x => x.props[prop];
const getWrapperPropSelector = prop => x => x.prop(prop);
Expand Down Expand Up @@ -165,6 +165,22 @@ describeWithDOM('mount', () => {
expect(wrapper.context('name')).to.equal(context.name);
});

itIf(REACT163, 'should find elements through Context elements', () => {
const { Provider, Consumer } = createContext('');

class Foo extends React.Component {
render() {
return (
<Consumer>{value => <span>{value}</span>}</Consumer>
);
}
}

const wrapper = mount(<Provider value="foo"><div><Foo /></div></Provider>);

expect(wrapper.find('span').text()).to.equal('foo');
});

describeIf(!REACT013, 'stateless components', () => {
it('can pass in context', () => {
const SimpleComponent = (props, context) => (
Expand Down
30 changes: 28 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { shallow, render, ShallowWrapper, mount } from 'enzyme';
import { ITERATOR_SYMBOL, withSetStateAllowed, sym } from 'enzyme/build/Utils';

import './_helpers/setupAdapters';
import { createClass } from './_helpers/react-compat';
import { createClass, createContext } from './_helpers/react-compat';
import { describeIf, itIf, itWithData, generateEmptyRenderData } from './_helpers';
import { REACT013, REACT014, REACT15, REACT150_4, REACT16, is } from './_helpers/version';
import { REACT013, REACT014, REACT15, REACT150_4, REACT16, REACT163, is } from './_helpers/version';

// The shallow renderer in react 16 does not yet support batched updates. When it does,
// we should be able to go un-skip all of the tests that are skipped with this flag.
Expand Down Expand Up @@ -96,6 +96,32 @@ describe('shallow', () => {
expect(wrapper.context('name')).to.equal(context.name);
});

itIf(REACT163, 'should find elements through Context elements', () => {
const { Provider, Consumer } = createContext('');

class Consumes extends React.Component {
render() {
return (
<span>
<Consumer>{value => <span>{value}</span>}</Consumer>
</span>
);
}
}

class Provides extends React.Component {
render() {
return (
<Provider value="foo"><div><Consumes /></div></Provider>
);
}
}

expect(shallow(<Consumes />).find('span')).to.have.length(1);
expect(shallow(<Provides />).find(Consumes)).to.have.length(1);

});

describeIf(!REACT013, 'stateless function components', () => {
it('can pass in context', () => {
const SimpleComponent = (props, context) => (
Expand Down
12 changes: 10 additions & 2 deletions packages/enzyme-test-suite/test/_helpers/react-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { is } from './version';
let createClass;
let renderToString;
let createPortal;
let createContext;

if (is('>=15.5 || ^16.0.0-alpha')) {
if (is('>=15.5 || ^16.0.0-alpha || ^16.3.0-alpha')) {
// eslint-disable-next-line import/no-extraneous-dependencies
createClass = require('create-react-class');
} else {
Expand All @@ -24,14 +25,21 @@ if (is('^0.13.0')) {
({ renderToString } = require('react-dom/server'));
}

if (is('^16.0.0-alpha')) {
if (is('^16.0.0-alpha || ^16.3.0-alpha')) {
({ createPortal } = require('react-dom'));
} else {
createPortal = null;
}

if (is('^16.3.0-0')) {
({ createContext } = require('react'));
} else {
createContext = null;
}

export {
createClass,
renderToString,
createPortal,
createContext,
};
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/test/_helpers/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const REACT15 = major === '15';
export const REACT150_4 = REACT15 && minor < 5;
export const REACT155 = REACT15 && minor >= 5;
export const REACT16 = major === '16';
export const REACT163 = REACT16 && minor >= 3;

export function gt(v) { return semver.gt(VERSION, v); }
export function lt(v) { return semver.lt(VERSION, v); }
Expand Down