Skip to content

Commit

Permalink
[enzyme-adapter-utils] [refactor] clean up makeValidElementType pro…
Browse files Browse the repository at this point in the history
…pType
  • Loading branch information
ljharb committed Feb 28, 2020
1 parent 1eb020e commit 4c591f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/enzyme-adapter-utils/src/createMountWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ const makeValidElementType = (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) {
function validElementTypeRequired(props, propName, ...args) {
if (!adapter.isValidElementType) {
return stringOrFunction.isRequired(props, propName, ...args);
}
Expand All @@ -30,7 +20,17 @@ const makeValidElementType = (adapter) => {
return null;
}
return new TypeError(`${propName} must be a valid element type!`);
};
}

function validElementType(props, propName, ...args) {
const propValue = props[propName];
if (propValue == null) {
return null;
}
return validElementTypeRequired(props, propName, ...args);
}
validElementType.isRequired = validElementTypeRequired;

return validElementType;
};

Expand Down
15 changes: 15 additions & 0 deletions packages/enzyme-test-suite/test/shared/methods/find.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ export default function describeFind({
);
});

wrap()
.withOverride(() => getAdapter(), 'isValidElementType', () => {})
.it('works when an adapter’s `isValidElementType` does not exist', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = Wrap((
<div>
<Foo className="foo" />
</div>
));

expect(wrapper.find(Foo)).to.have.lengthOf(1);
});

it('finds a component based on a component function name', () => {
class Foo extends React.Component {
render() { return <div />; }
Expand Down

0 comments on commit 4c591f7

Please sign in to comment.