diff --git a/packages/enzyme-adapter-react-16/package.json b/packages/enzyme-adapter-react-16/package.json index 0ba1bbb76..97442c350 100644 --- a/packages/enzyme-adapter-react-16/package.json +++ b/packages/enzyme-adapter-react-16/package.json @@ -40,12 +40,12 @@ "object.values": "^1.0.4", "prop-types": "^15.6.0", "react-reconciler": "^0.7.0", - "react-test-renderer": "^16.0.0-0" + "react-test-renderer": "^16.0.0-0 || ^16.3.0-0" }, "peerDependencies": { "enzyme": "^3.0.0", - "react": "^16.0.0-0", - "react-dom": "^16.0.0-0" + "react": "^16.0.0-0 || ^16.3.0-0", + "react-dom": "^16.0.0-0 || ^16.3.0-0" }, "devDependencies": { "babel-cli": "^6.26.0", diff --git a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js index 47e374c68..6adae5eac 100644 --- a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js +++ b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js @@ -29,6 +29,8 @@ const FunctionalComponent = 1; const HostPortal = 4; const HostComponent = 5; const HostText = 6; +const ContextConsumer = 12; +const ContextProvider = 13; function nodeAndSiblingsArray(nodeWithSibling) { const array = []; @@ -72,6 +74,10 @@ function toTree(vnode) { return toTree(node.child); case HostPortal: // 4 return toTree(node.child); + case ContextProvider: + return toTree(node.child); + case ContextConsumer: + return toTree(node.child); case ClassComponent: return { nodeType: 'class', diff --git a/packages/enzyme-test-suite/package.json b/packages/enzyme-test-suite/package.json index eefd20e92..abbce8eb3 100644 --- a/packages/enzyme-test-suite/package.json +++ b/packages/enzyme-test-suite/package.json @@ -49,4 +49,4 @@ "eslint-plugin-jsx-a11y": "^6.0.3", "eslint-plugin-react": "^7.6.1" } -} +} \ No newline at end of file diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index db5419a17..cc48122ac 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -11,7 +11,7 @@ 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, @@ -19,7 +19,7 @@ import { 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); @@ -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 ( + {value => {value}} + ); + } + } + + const wrapper = mount(
); + + expect(wrapper.find('span').text()).to.equal('foo'); + }); + describeIf(!REACT013, 'stateless components', () => { it('can pass in context', () => { const SimpleComponent = (props, context) => ( diff --git a/packages/enzyme-test-suite/test/_helpers/react-compat.js b/packages/enzyme-test-suite/test/_helpers/react-compat.js index 8e7edc694..a63fbc15a 100644 --- a/packages/enzyme-test-suite/test/_helpers/react-compat.js +++ b/packages/enzyme-test-suite/test/_helpers/react-compat.js @@ -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 { @@ -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, }; diff --git a/packages/enzyme-test-suite/test/_helpers/version.js b/packages/enzyme-test-suite/test/_helpers/version.js index d6142c094..ba6d11392 100644 --- a/packages/enzyme-test-suite/test/_helpers/version.js +++ b/packages/enzyme-test-suite/test/_helpers/version.js @@ -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); }