Skip to content

Commit

Permalink
[New] Utils: 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 f6c2fe1 commit 0ef951d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/enzyme-test-suite/test/Utils-spec.jsx
Expand Up @@ -13,8 +13,10 @@ import {
makeOptions,
isEmptyValue,
renderedDive,
isCustomComponent,
} from 'enzyme/build/Utils';
import getAdapter from 'enzyme/build/getAdapter';
import EnzymeAdapter from 'enzyme/build/EnzymeAdapter';
import {
flatten,
mapNativeEventNames,
Expand Down Expand Up @@ -1035,4 +1037,42 @@ describe('Utils', () => {
});
});
});

describe('isCustomComponent', () => {
class TestAdapter extends EnzymeAdapter {
isCustomComponent() {}
}

it('delegates to the adapter has method', () => {
const component = {};
const result = {};
const adapter = new TestAdapter();
sinon.stub(adapter, 'isCustomComponent').returns(result);

const actual = isCustomComponent(component, adapter);

expect(actual).to.equal(!!result);

expect(adapter.isCustomComponent).to.have.property('callCount', 1);
const [args] = adapter.isCustomComponent.args;
expect(args).to.eql([component]);
});

it('returns "is a function" when adapter lacks method', () => {
const component = {};
const result = {};
const adapter = new TestAdapter();
adapter.isCustomComponent = null;

expect(isCustomComponent({}, adapter)).to.equal(false);
expect(isCustomComponent(() => {}, adapter)).to.equal(true);
});

it('throws without a valid adapter', () => {
expect(() => isCustomComponent({})).to.throw(Error);
expect(() => isCustomComponent({}, null)).to.throw(Error);
expect(() => isCustomComponent({}, false)).to.throw(Error);
expect(() => isCustomComponent({}, {})).to.throw(Error);
});
});
});
9 changes: 9 additions & 0 deletions packages/enzyme/src/Utils.js
Expand Up @@ -10,6 +10,7 @@ import trim from 'string.prototype.trim';
import { get } from './configuration';
import { childrenOfNode } from './RSTTraversal';
import realGetAdapter from './getAdapter';
import validateAdapter from './validateAdapter';

export const ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;

Expand Down Expand Up @@ -49,6 +50,14 @@ export function makeOptions(options) {
};
}

export function isCustomComponent(component, adapter) {
validateAdapter(adapter);
if (adapter.isCustomComponent) {
return !!adapter.isCustomComponent(component);
}
return typeof component === 'function';
}

export function isCustomComponentElement(inst, adapter) {
if (adapter.isCustomComponentElement) {
return !!adapter.isCustomComponentElement(inst);
Expand Down

0 comments on commit 0ef951d

Please sign in to comment.