Skip to content

Commit

Permalink
[Tests] Utils: add nodeHasType tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 22, 2018
1 parent fc9fadf commit 60a8a23
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/enzyme-test-suite/test/Utils-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
nodeMatches,
displayNameOfNode,
spyMethod,
nodeHasType,
} from 'enzyme/build/Utils';
import {
flatten,
Expand Down Expand Up @@ -614,4 +615,57 @@ describe('Utils', () => {
expect(Object.getOwnPropertyDescriptor(obj, 'method')).to.deep.equal(descriptor);
});
});

describe('nodeHasType', () => {
it('is `false` if either argument is falsy', () => {
expect(nodeHasType(null, {})).to.equal(false);
expect(nodeHasType({}, null)).to.equal(false);
});

it('is `false` if `node` has a falsy `type`', () => {
expect(nodeHasType({}, {})).to.equal(false);
expect(nodeHasType({ type: null }, {})).to.equal(false);
expect(nodeHasType({ type: false }, {})).to.equal(false);
expect(nodeHasType({ type: '' }, {})).to.equal(false);
expect(nodeHasType({ type: 0 }, {})).to.equal(false);
});

it('compares `node.type` to `type` when `node.type` is a non-empty string', () => {
expect(nodeHasType({ type: 'foo' }, 'foo')).to.equal(true);
expect(nodeHasType({ type: 'foo' }, 'bar')).to.equal(false);
});

describe('when only `node.type.displayName` matches `type`', () => {
const x = {};
it('is `true` when `node.type` is an object', () => {
expect(nodeHasType(
{ type: { displayName: x } },
x,
)).to.equal(true);
});

it('is `true` when `node.type` is a function', () => {
expect(nodeHasType(
{ type: Object.assign(() => {}, { displayName: x }) },
x,
)).to.equal(true);
});
});

describe('when only `node.type.name` matches `type`', () => {
const x = {};
it('is `true` when `node.type` is an object', () => {
expect(nodeHasType(
{ type: { name: x } },
x,
)).to.equal(true);
});

it('is `true` when `node.type` is a function', () => {
function namedType() {}

expect(nodeHasType({ type: namedType }, 'namedType')).to.equal(true);
});
});
});
});

0 comments on commit 60a8a23

Please sign in to comment.