Skip to content

Commit

Permalink
[enzyme-adapter-react-*] [new] wrap: add wrap to all adapters
Browse files Browse the repository at this point in the history
[enzyme-adapter-utils] [new] add `wrapWithSimpleWrapper`
  • Loading branch information
dferber90 authored and ljharb committed Oct 16, 2018
1 parent 3bd7a40 commit 8494d0e
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/enzyme-adapter-react-13/src/ReactThirteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
wrap,
} from 'enzyme-adapter-utils';
import mapNativeEventNames from './ReactThirteenMapNativeEventNames';
import elementToTree from './ReactThirteenElementToTree';
Expand Down Expand Up @@ -242,6 +243,10 @@ class ReactThirteenAdapter extends EnzymeAdapter {
}
}

wrap(element) {
return wrap(element);
}

// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
Expand Down
5 changes: 5 additions & 0 deletions packages/enzyme-adapter-react-14/src/ReactFourteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
wrap,
} from 'enzyme-adapter-utils';

function typeToNodeType(type) {
Expand Down Expand Up @@ -214,6 +215,10 @@ class ReactFourteenAdapter extends EnzymeAdapter {
}
}

wrap(element) {
return wrap(element);
}

// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
wrap,
} from 'enzyme-adapter-utils';
import ifReact from 'enzyme-adapter-react-helper/build/ifReact';

Expand Down Expand Up @@ -249,6 +250,10 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
}
}

wrap(element) {
return wrap(element);
}

// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
Expand Down
5 changes: 5 additions & 0 deletions packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
wrap,
} from 'enzyme-adapter-utils';

function compositeTypeToNodeType(type) {
Expand Down Expand Up @@ -249,6 +250,10 @@ class ReactFifteenAdapter extends EnzymeAdapter {
}
}

wrap(element) {
return wrap(element);
}

// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
propsWithKeysAndRef,
ensureKeyOrUndefined,
simulateError,
wrap,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -440,6 +441,10 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
}
}

wrap(element) {
return wrap(element);
}

// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
propsWithKeysAndRef,
ensureKeyOrUndefined,
simulateError,
wrap,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -442,6 +443,10 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
}
}

wrap(element) {
return wrap(element);
}

// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
propsWithKeysAndRef,
ensureKeyOrUndefined,
simulateError,
wrap,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -422,6 +423,10 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
}
}

wrap(element) {
return wrap(element);
}

// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
Expand Down
5 changes: 5 additions & 0 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
propsWithKeysAndRef,
ensureKeyOrUndefined,
simulateError,
wrap,
} from 'enzyme-adapter-utils';
import findCurrentFiberUsingSlowPath from './findCurrentFiberUsingSlowPath';
import detectFiberTags from './detectFiberTags';
Expand Down Expand Up @@ -468,6 +469,10 @@ class ReactSixteenAdapter extends EnzymeAdapter {
}
}

wrap(element) {
return wrap(element);
}

// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
Expand Down
3 changes: 2 additions & 1 deletion packages/enzyme-adapter-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"dependencies": {
"function.prototype.name": "^1.1.0",
"object.assign": "^4.1.0",
"prop-types": "^15.6.2"
"prop-types": "^15.6.2",
"semver": "^5.6.0"
},
"peerDependencies": {
"react": "0.13.x || 0.14.x || ^15.0.0-0 || ^16.0.0-0"
Expand Down
3 changes: 2 additions & 1 deletion packages/enzyme-adapter-utils/src/Utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import functionName from 'function.prototype.name';
import createMountWrapper from './createMountWrapper';
import createRenderWrapper from './createRenderWrapper';
import wrap from './wrapWithSimpleWrapper';

export { createMountWrapper, createRenderWrapper };
export { createMountWrapper, createRenderWrapper, wrap };

export function mapNativeEventNames(event, {
animation = false, // should be true for React 15+
Expand Down
29 changes: 29 additions & 0 deletions packages/enzyme-adapter-utils/src/wrapWithSimpleWrapper.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { intersects } from 'semver';
import PropTypes from 'prop-types';

const propTypes = {
children: PropTypes.element.isRequired,
};

const Wrapper = (intersects('>= 0.14', React.version)
// eslint-disable-next-line prefer-arrow-callback
? () => Object.assign(function SimpleSFCWrapper({ children }) {
return children;
}, { propTypes })
: () => {
class SimpleClassWrapper extends React.Component {
render() {
const { children } = this.props;
return children;
}
}
SimpleClassWrapper.propTypes = propTypes;
return SimpleClassWrapper;
}
)();

export default function wrap(element) {
console.log(React.version, Wrapper);
return <Wrapper>{element}</Wrapper>;
}
14 changes: 14 additions & 0 deletions packages/enzyme-test-suite/test/Adapter-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,18 @@ describe('Adapter', () => {
expect(adapter.isFragment(<div />)).to.equal(false);
});
});

describe('.wrap()', () => {
it('returns a valid element', () => {
const element = <div a="b" c="d" />;
const wrapped = adapter.wrap(element);
expect(adapter.isValidElement(wrapped)).to.equal(true);
});

it('renders the children provided', () => {
const element = <div a="b" c="d" />;
const wrapped = adapter.wrap(element);
expect(wrapped.props).to.contain.keys({ children: element });
});
});
});

0 comments on commit 8494d0e

Please sign in to comment.