From e21b7b506ae2cc8aff8cac233564e74a605291b2 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Mon, 5 Feb 2018 11:17:30 -0500 Subject: [PATCH 1/7] Add support for react context element types, fixes #1509 --- packages/enzyme-adapter-react-16/package.json | 6 +++--- .../src/ReactSixteenAdapter.js | 6 ++++++ packages/enzyme-test-suite/package.json | 2 +- .../test/ReactWrapper-spec.jsx | 20 +++++++++++++++++-- .../test/_helpers/react-compat.js | 12 +++++++++-- .../test/_helpers/version.js | 1 + 6 files changed, 39 insertions(+), 8 deletions(-) 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); } From 9d01e420dc1a5747a5103f18d805a61721729e58 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Fri, 9 Feb 2018 10:54:50 -0500 Subject: [PATCH 2/7] Update package.json --- packages/enzyme-test-suite/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/enzyme-test-suite/package.json b/packages/enzyme-test-suite/package.json index abbce8eb3..eefd20e92 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 +} From 81167fb4a4f5dfc6324f6154662eaa9656e6e048 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Mon, 5 Mar 2018 11:34:06 -0500 Subject: [PATCH 3/7] use children to tree --- .../src/ReactSixteenAdapter.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js index 6adae5eac..bcc1f48aa 100644 --- a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js +++ b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js @@ -29,6 +29,7 @@ const FunctionalComponent = 1; const HostPortal = 4; const HostComponent = 5; const HostText = 6; +const Mode = 11; const ContextConsumer = 12; const ContextProvider = 13; @@ -74,10 +75,6 @@ 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', @@ -88,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', @@ -117,6 +112,11 @@ function toTree(vnode) { } case HostText: // 6 return node.memoizedProps; + case Fragment: // 10 + case Mode: // 11 + case ContextProvider: // 13 + case ContextConsumer: // 12 + return childrenToTree(node.child); default: throw new Error(`Enzyme Internal Error: unknown node with tag ${node.tag}`); } From 419780b1bb3a2f0580472ac702e996da254fb226 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Fri, 16 Mar 2018 11:17:30 -0400 Subject: [PATCH 4/7] Add shallow tests --- packages/enzyme-test-suite/package.json | 2 +- .../test/ShallowWrapper-spec.jsx | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) 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/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 22e14d897..d442ee4ad 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -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. @@ -96,6 +96,22 @@ describe('shallow', () => { 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 function components', () => { it('can pass in context', () => { const SimpleComponent = (props, context) => ( From f3618c133057388d91c281ac4d08fceddbce17a7 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Fri, 16 Mar 2018 11:42:20 -0400 Subject: [PATCH 5/7] Add real shallow tests --- .../test/ShallowWrapper-spec.jsx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index d442ee4ad..8aaf196e1 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -99,17 +99,27 @@ describe('shallow', () => { itIf(REACT163, 'should find elements through Context elements', () => { const { Provider, Consumer } = createContext(''); - class Foo extends React.Component { + class Consumes extends React.Component { + render() { + return ( + + {value => {value}} + + ); + } + } + + class Provides extends React.Component { render() { return ( - {value => {value}} +
); } } - const wrapper = mount(
); + expect(shallow().find('span')).to.have.length(1); + expect(shallow().find(Consumes)).to.have.length(1); - expect(wrapper.find('span').text()).to.equal('foo'); }); describeIf(!REACT013, 'stateless function components', () => { From 9fb423e68a66c867b88520cf8612be053e4cbe55 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Thu, 22 Mar 2018 12:23:57 -0400 Subject: [PATCH 6/7] Update package.json --- packages/enzyme-test-suite/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/enzyme-test-suite/package.json b/packages/enzyme-test-suite/package.json index abbce8eb3..eefd20e92 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 +} From 5253067e934aa84ceb5a9bf039cf388fcf194d3d Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Thu, 29 Mar 2018 17:10:02 -0400 Subject: [PATCH 7/7] move back to normal range --- packages/enzyme-adapter-react-16/package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/enzyme-adapter-react-16/package.json b/packages/enzyme-adapter-react-16/package.json index 97442c350..0ba1bbb76 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 || ^16.3.0-0" + "react-test-renderer": "^16.0.0-0" }, "peerDependencies": { "enzyme": "^3.0.0", - "react": "^16.0.0-0 || ^16.3.0-0", - "react-dom": "^16.0.0-0 || ^16.3.0-0" + "react": "^16.0.0-0", + "react-dom": "^16.0.0-0" }, "devDependencies": { "babel-cli": "^6.26.0",