Skip to content

Commit

Permalink
[enzyme-adapter-react-*] [new] add isCustomComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
minznerjosh authored and ljharb committed Jan 2, 2019
1 parent 7b9e478 commit 2e11497
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/enzyme-adapter-react-13/src/ReactThirteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ class ReactThirteenAdapter extends EnzymeAdapter {
return typeof object === 'string' || typeof object === 'function';
}

isCustomComponent(component) {
return typeof component === 'function';
}

createElement(...args) {
return React.createElement(...args);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/enzyme-adapter-react-14/src/ReactFourteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ class ReactFourteenAdapter extends EnzymeAdapter {
return isValidElementType(object);
}

isCustomComponent(component) {
return typeof component === 'function';
}

createElement(...args) {
return React.createElement(...args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
return isValidElementType(object);
}

isCustomComponent(component) {
return typeof component === 'function';
}

createElement(...args) {
return React.createElement(...args);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ class ReactFifteenAdapter extends EnzymeAdapter {
return isValidElementType(object);
}

isCustomComponent(component) {
return typeof component === 'function';
}

createElement(...args) {
return React.createElement(...args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
return isValidElementType(object);
}

isCustomComponent(component) {
return typeof component === 'function';
}

createElement(...args) {
return React.createElement(...args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
return isValidElementType(object);
}

isCustomComponent(component) {
return typeof component === 'function';
}

isFragment(fragment) {
return typeOfNode(fragment) === Fragment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,15 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
return typeOfNode(fragment) === Fragment;
}

isCustomComponent(type) {
return typeof type === 'function' || isForwardRef({ type });
}

isCustomComponentElement(inst) {
if (!inst || !this.isValidElement(inst)) {
return false;
}
return typeof inst.type === 'function' || isForwardRef(inst);
return this.isCustomComponent(inst.type);
}

createElement(...args) {
Expand Down
6 changes: 5 additions & 1 deletion packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,15 @@ class ReactSixteenAdapter extends EnzymeAdapter {
return typeOfNode(fragment) === Fragment;
}

isCustomComponent(type) {
return typeof type === 'function' || isForwardRef({ type });
}

isCustomComponentElement(inst) {
if (!inst || !this.isValidElement(inst)) {
return false;
}
return typeof inst.type === 'function' || isForwardRef(inst);
return this.isCustomComponent(inst.type);
}

createElement(...args) {
Expand Down
25 changes: 25 additions & 0 deletions packages/enzyme-test-suite/test/Adapter-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1092,4 +1092,29 @@ Warning: Failed Adapter-spec type: Invalid Adapter-spec \`foo\` of type \`string
`.trim());
});
});

describe('isCustomComponent', () => {
function FunctionComponent() {
return null;
}
class ClassComponent extends React.Component {
render() {
return null;
}
}

it('returns true for functional/class components', () => {
expect(adapter.isCustomComponent(FunctionComponent)).to.equal(true);
expect(adapter.isCustomComponent(ClassComponent)).to.equal(true);
});

it('returns false for everything else', () => {
expect(adapter.isCustomComponent({})).to.equal(false);
expect(adapter.isCustomComponent(true)).to.equal(false);
});

itIf(is('>=16.3'), 'returns true for forward refs', () => {
expect(adapter.isCustomComponent(React.forwardRef(() => null))).to.equal(true);
});
});
});

0 comments on commit 2e11497

Please sign in to comment.