Skip to content

Commit

Permalink
[enzyme-adapter-utils] createMountWrapper: when the adapter is prov…
Browse files Browse the repository at this point in the history
…ided, defer to its definition of a valid element type.
  • Loading branch information
jquense authored and ljharb committed Mar 23, 2018
1 parent b396e5c commit ea9906f
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion packages/enzyme-adapter-utils/src/createMountWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@ import PropTypes from 'prop-types';

/* eslint react/forbid-prop-types: 0 */

const stringOrFunction = PropTypes.oneOfType([PropTypes.func, PropTypes.string]);
const makeValidElementType = (adapter) => {
if (!adapter) {
return stringOrFunction;
}

function validElementType(props, propName, ...args) {
if (!adapter.isValidElementType) {
return stringOrFunction(props, propName, ...args);
}
const propValue = props[propName];
if (propValue == null || adapter.isValidElementType(propValue)) {
return null;
}
return new TypeError(`${propName} must be a valid element type!`);
}
validElementType.isRequired = function validElementTypeRequired(props, propName, ...args) {
if (!adapter.isValidElementType) {
return stringOrFunction.isRequired(props, propName, ...args);
}
const propValue = props[propName]; // eslint-disable-line react/destructuring-assignment
if (adapter.isValidElementType(propValue)) {
return null;
}
return new TypeError(`${propName} must be a valid element type!`);
};
return validElementType;
};

/**
* This is a utility component to wrap around the nodes we are
* passing in to `mount()`. Theoretically, you could do everything
Expand All @@ -12,6 +41,8 @@ import PropTypes from 'prop-types';
* pass new props in.
*/
export default function createMountWrapper(node, options = {}) {
const { adapter } = options;

class WrapperComponent extends React.Component {
constructor(...args) {
super(...args);
Expand Down Expand Up @@ -62,7 +93,7 @@ export default function createMountWrapper(node, options = {}) {
}
}
WrapperComponent.propTypes = {
Component: PropTypes.oneOfType([PropTypes.func, PropTypes.string]).isRequired,
Component: makeValidElementType(adapter).isRequired,
props: PropTypes.object.isRequired,
context: PropTypes.object,
};
Expand Down

0 comments on commit ea9906f

Please sign in to comment.