Skip to content

Commit

Permalink
[New] Utils: nodeHasType: call into adapter’s displayNameOfNode
Browse files Browse the repository at this point in the history
…, if present
  • Loading branch information
jquense authored and ljharb committed Jul 2, 2018
1 parent 3db6d2c commit 02f6f21
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 42 deletions.
115 changes: 73 additions & 42 deletions packages/enzyme-test-suite/test/Utils-spec.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import React from 'react';
import { expect } from 'chai';
import wrap from 'mocha-wrap';
import sinon from 'sinon';
import {
childrenToSimplifiedArray,
nodeEqual,
nodeMatches,
displayNameOfNode,
spyMethod,
nodeHasType,
getAdapter,
} from 'enzyme/build/Utils';
import {
flatten,
Expand Down Expand Up @@ -616,56 +619,84 @@ describe('Utils', () => {
});
});

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);
});
wrap()
.withOverride(() => getAdapter(), 'displayNameOfNode', () => undefined)
.describe('nodeHasType', () => {
it('is `false` if either argument is falsy', () => {
expect(nodeHasType(null, {})).to.equal(false);
expect(nodeHasType({}, null)).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 `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('is `true` when `node.type` is a function', () => {
expect(nodeHasType(
{ type: Object.assign(() => {}, { displayName: x }) },
x,
)).to.equal(true);
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.name` matches `type`', () => {
const x = {};
it('is `true` when `node.type` is an object', () => {
expect(nodeHasType(
{ type: { name: x } },
x,
)).to.equal(true);
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);
});
});

it('is `true` when `node.type` is a function', () => {
function namedType() {}
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);
expect(nodeHasType({ type: namedType }, 'namedType')).to.equal(true);
});
});

wrap()
.withOverride(() => getAdapter(), 'displayNameOfNode', () => sinon.stub())
.describe('when the adapter has a `displayNameOfNode` function', () => {
it('is `true` when `displayNameOfNode` matches `type`', () => {
const stub = getAdapter().displayNameOfNode;
const sentinel = {};
stub.returns(sentinel);

const node = {};
expect(nodeHasType(node, sentinel)).to.equal(true);

expect(stub).to.have.property('callCount', 1);
const { args } = stub.firstCall;
expect(args).to.eql([node]);
});

it('is `false` when `displayNameOfNode` does not match `type`', () => {
const stub = getAdapter().displayNameOfNode;
const sentinel = {};
stub.returns(sentinel);

const node = {};
expect(nodeHasType(node, {})).to.equal(false);
});
});
});
});
});
7 changes: 7 additions & 0 deletions packages/enzyme/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export function typeOfNode(node) {

export function nodeHasType(node, type) {
if (!type || !node) return false;

const adapter = getAdapter();
if (adapter.displayNameOfNode) {
const displayName = adapter.displayNameOfNode(node);
return displayName === type;
}

if (!node.type) return false;
if (typeof node.type === 'string') return node.type === type;
return (
Expand Down

0 comments on commit 02f6f21

Please sign in to comment.