Skip to content

Commit

Permalink
Remove all React dependencies from enzyme
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandrichardson committed Aug 20, 2017
1 parent 0edf10f commit 1364f2e
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 159 deletions.
7 changes: 3 additions & 4 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ const additionalDirsToRemove = [
'node_modules/.bin/npm.cmd',
];

const rmrfArgs = []
const rmrfs = []
.concat(packagesToRemove)
.concat(additionalDirsToRemove)
.join(' ');
.concat(additionalDirsToRemove);

Promise.resolve()
.then(() => primraf(rmrfArgs))
.then(() => Promise.all(rmrfs.map(s => primraf(s))))
.then(() => run('npm i'))
.then(() => Promise.all([
getJSON(adapterPackageJsonPath),
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-13/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"enzyme-adapter-utils": "2.9.1",
"lodash": "^4.17.4",
"object.assign": "^4.0.4",
"object.values": "^1.0.4",
"prop-types": "^15.5.10"
},
Expand Down
46 changes: 29 additions & 17 deletions packages/enzyme-adapter-react-13/src/ReactThirteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import React from 'react';
import ReactAddons from 'react/addons';
// eslint-disable-next-line import/no-unresolved, import/extensions
import ReactContext from 'react/lib/ReactContext';
import PropTypes from 'prop-types';
import values from 'object.values';
import { EnzymeAdapter } from 'enzyme';
import {
propFromEvent,
withSetStateAllowed,
assertDomAvailable,
createRenderWrapper,
createMountWrapper,
} from 'enzyme-adapter-utils';
import mapNativeEventNames from './ReactThirteenMapNativeEventNames';
import elementToTree from './ReactThirteenElementToTree';
Expand Down Expand Up @@ -93,31 +94,34 @@ function instanceToTree(inst) {
throw new Error('Enzyme Internal Error: unknown instance encountered');
}

class SimpleWrapper extends React.Component {
render() {
return this.props.node || null;
}
}

SimpleWrapper.propTypes = { node: PropTypes.node.isRequired };

class ReactThirteenAdapter extends EnzymeAdapter {
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
let instance = null;
return {
render(el/* , context */) {
const wrappedEl = React.createElement(SimpleWrapper, {
node: el,
});
instance = React.render(wrappedEl, domNode);
render(el, context, callback) {
if (instance === null) {
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, {
Component: el.type,
props: el.props,
context,
});
instance = React.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
}
} else {
instance.setChildProps(el.props, context, callback);
}
},
unmount() {
React.unmountComponentAtNode(domNode);
instance = null;
},
getNode() {
return instanceToTree(instance._reactInternalInstance._renderedComponent);
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event);
Expand Down Expand Up @@ -186,9 +190,17 @@ class ReactThirteenAdapter extends EnzymeAdapter {
};
}

createStringRenderer(/* options */) {
createStringRenderer(options) {
return {
render(el /* , context */) {
render(el, context) {
if (options.context && (el.type.contextTypes || options.childContextTypes)) {
const childContextTypes = {
...(el.type.contextTypes || {}),
...options.childContextTypes,
};
const ContextWrapper = createRenderWrapper(el, context, childContextTypes);
return React.renderToStaticMarkup(React.createElement(ContextWrapper));
}
return React.renderToStaticMarkup(el);
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-14/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"enzyme-adapter-utils": "2.9.1",
"lodash": "^4.17.4",
"object.assign": "^4.0.4",
"object.values": "^1.0.4",
"prop-types": "^15.5.10"
},
Expand Down
46 changes: 29 additions & 17 deletions packages/enzyme-adapter-react-14/src/ReactFourteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ReactDOM from 'react-dom';
import ReactDOMServer from 'react-dom/server';
// eslint-disable-next-line import/no-unresolved, import/extensions
import TestUtils from 'react-addons-test-utils';
import PropTypes from 'prop-types';
import values from 'object.values';
import { EnzymeAdapter } from 'enzyme';
import {
Expand All @@ -13,6 +12,8 @@ import {
propFromEvent,
withSetStateAllowed,
assertDomAvailable,
createRenderWrapper,
createMountWrapper,
} from 'enzyme-adapter-utils';

function typeToNodeType(type) {
Expand Down Expand Up @@ -65,31 +66,34 @@ function instanceToTree(inst) {
throw new Error('Enzyme Internal Error: unknown instance encountered');
}

class SimpleWrapper extends React.Component {
render() {
return this.props.node || null;
}
}

SimpleWrapper.propTypes = { node: PropTypes.node.isRequired };

class ReactFifteenAdapter extends EnzymeAdapter {
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
let instance = null;
return {
render(el/* , context */) {
const wrappedEl = React.createElement(SimpleWrapper, {
node: el,
});
instance = ReactDOM.render(wrappedEl, domNode);
render(el, context, callback) {
if (instance === null) {
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, {
Component: el.type,
props: el.props,
context,
});
instance = ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
}
} else {
instance.setChildProps(el.props, context, callback);
}
},
unmount() {
ReactDOM.unmountComponentAtNode(domNode);
instance = null;
},
getNode() {
return instanceToTree(instance._reactInternalInstance._renderedComponent);
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event);
Expand Down Expand Up @@ -157,9 +161,17 @@ class ReactFifteenAdapter extends EnzymeAdapter {
};
}

createStringRenderer(/* options */) {
createStringRenderer(options) {
return {
render(el /* , context */) {
render(el, context) {
if (options.context && (el.type.contextTypes || options.childContextTypes)) {
const childContextTypes = {
...(el.type.contextTypes || {}),
...options.childContextTypes,
};
const ContextWrapper = createRenderWrapper(el, context, childContextTypes);
return ReactDOMServer.renderToStaticMarkup(React.createElement(ContextWrapper));
}
return ReactDOMServer.renderToStaticMarkup(el);
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-15.4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"enzyme-adapter-utils": "2.9.1",
"lodash": "^4.17.4",
"object.assign": "^4.0.4",
"object.values": "^1.0.4",
"prop-types": "^15.5.10"
},
Expand Down
46 changes: 29 additions & 17 deletions packages/enzyme-adapter-react-15.4/src/ReactFifteenFourAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ReactDOM from 'react-dom';
import ReactDOMServer from 'react-dom/server';
// eslint-disable-next-line import/no-unresolved, import/extensions
import TestUtils from 'react-addons-test-utils';
import PropTypes from 'prop-types';
import values from 'object.values';
import { EnzymeAdapter } from 'enzyme';
import {
Expand All @@ -13,6 +12,8 @@ import {
propFromEvent,
withSetStateAllowed,
assertDomAvailable,
createRenderWrapper,
createMountWrapper,
} from 'enzyme-adapter-utils';

function compositeTypeToNodeType(type) {
Expand Down Expand Up @@ -73,31 +74,34 @@ function instanceToTree(inst) {
throw new Error('Enzyme Internal Error: unknown instance encountered');
}

class SimpleWrapper extends React.Component {
render() {
return this.props.node || null;
}
}

SimpleWrapper.propTypes = { node: PropTypes.node.isRequired };

class ReactFifteenFourAdapter extends EnzymeAdapter {
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
let instance = null;
return {
render(el/* , context */) {
const wrappedEl = React.createElement(SimpleWrapper, {
node: el,
});
instance = ReactDOM.render(wrappedEl, domNode);
render(el, context, callback) {
if (instance === null) {
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, {
Component: el.type,
props: el.props,
context,
});
instance = ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
}
} else {
instance.setChildProps(el.props, context, callback);
}
},
unmount() {
ReactDOM.unmountComponentAtNode(domNode);
instance = null;
},
getNode() {
return instanceToTree(instance._reactInternalInstance._renderedComponent);
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event);
Expand Down Expand Up @@ -165,9 +169,17 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
};
}

createStringRenderer(/* options */) {
createStringRenderer(options) {
return {
render(el /* , context */) {
render(el, context) {
if (options.context && (el.type.contextTypes || options.childContextTypes)) {
const childContextTypes = {
...(el.type.contextTypes || {}),
...options.childContextTypes,
};
const ContextWrapper = createRenderWrapper(el, context, childContextTypes);
return ReactDOMServer.renderToStaticMarkup(React.createElement(ContextWrapper));
}
return ReactDOMServer.renderToStaticMarkup(el);
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-15/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"enzyme-adapter-utils": "2.9.1",
"lodash": "^4.17.4",
"object.assign": "^4.0.4",
"object.values": "^1.0.4",
"prop-types": "^15.5.10"
},
Expand Down
46 changes: 29 additions & 17 deletions packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ReactDOM from 'react-dom';
import ReactDOMServer from 'react-dom/server';
// eslint-disable-next-line import/no-unresolved, import/extensions
import TestUtils from 'react-dom/test-utils';
import PropTypes from 'prop-types';
import values from 'object.values';
import { EnzymeAdapter } from 'enzyme';
import {
Expand All @@ -13,6 +12,8 @@ import {
propFromEvent,
withSetStateAllowed,
assertDomAvailable,
createRenderWrapper,
createMountWrapper,
} from 'enzyme-adapter-utils';

function compositeTypeToNodeType(type) {
Expand Down Expand Up @@ -73,31 +74,34 @@ function instanceToTree(inst) {
throw new Error('Enzyme Internal Error: unknown instance encountered');
}

class SimpleWrapper extends React.Component {
render() {
return this.props.node || null;
}
}

SimpleWrapper.propTypes = { node: PropTypes.node.isRequired };

class ReactFifteenAdapter extends EnzymeAdapter {
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
let instance = null;
return {
render(el/* , context */) {
const wrappedEl = React.createElement(SimpleWrapper, {
node: el,
});
instance = ReactDOM.render(wrappedEl, domNode);
render(el, context, callback) {
if (instance === null) {
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, {
Component: el.type,
props: el.props,
context,
});
instance = ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
}
} else {
instance.setChildProps(el.props, context, callback);
}
},
unmount() {
ReactDOM.unmountComponentAtNode(domNode);
instance = null;
},
getNode() {
return instanceToTree(instance._reactInternalInstance._renderedComponent);
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event);
Expand Down Expand Up @@ -165,9 +169,17 @@ class ReactFifteenAdapter extends EnzymeAdapter {
};
}

createStringRenderer(/* options */) {
createStringRenderer(options) {
return {
render(el /* , context */) {
render(el, context) {
if (options.context && (el.type.contextTypes || options.childContextTypes)) {
const childContextTypes = {
...(el.type.contextTypes || {}),
...options.childContextTypes,
};
const ContextWrapper = createRenderWrapper(el, context, childContextTypes);
return ReactDOMServer.renderToStaticMarkup(React.createElement(ContextWrapper));
}
return ReactDOMServer.renderToStaticMarkup(el);
},
};
Expand Down
Loading

0 comments on commit 1364f2e

Please sign in to comment.