Skip to content

Commit 038c894

Browse files
jquenseljharb
authored andcommitted
[New] Utils: nodeHasType: call into adapter’s displayNameOfNode, if present
1 parent fbd06ee commit 038c894

File tree

2 files changed

+80
-42
lines changed

2 files changed

+80
-42
lines changed

packages/enzyme-test-suite/test/Utils-spec.jsx

Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React from 'react';
22
import { expect } from 'chai';
3+
import wrap from 'mocha-wrap';
4+
import sinon from 'sinon';
35
import {
46
childrenToSimplifiedArray,
57
nodeEqual,
68
nodeMatches,
79
displayNameOfNode,
810
spyMethod,
911
nodeHasType,
12+
getAdapter,
1013
} from 'enzyme/build/Utils';
1114
import {
1215
flatten,
@@ -616,56 +619,84 @@ describe('Utils', () => {
616619
});
617620
});
618621

619-
describe('nodeHasType', () => {
620-
it('is `false` if either argument is falsy', () => {
621-
expect(nodeHasType(null, {})).to.equal(false);
622-
expect(nodeHasType({}, null)).to.equal(false);
623-
});
624-
625-
it('is `false` if `node` has a falsy `type`', () => {
626-
expect(nodeHasType({}, {})).to.equal(false);
627-
expect(nodeHasType({ type: null }, {})).to.equal(false);
628-
expect(nodeHasType({ type: false }, {})).to.equal(false);
629-
expect(nodeHasType({ type: '' }, {})).to.equal(false);
630-
expect(nodeHasType({ type: 0 }, {})).to.equal(false);
631-
});
632-
633-
it('compares `node.type` to `type` when `node.type` is a non-empty string', () => {
634-
expect(nodeHasType({ type: 'foo' }, 'foo')).to.equal(true);
635-
expect(nodeHasType({ type: 'foo' }, 'bar')).to.equal(false);
636-
});
622+
wrap()
623+
.withOverride(() => getAdapter(), 'displayNameOfNode', () => undefined)
624+
.describe('nodeHasType', () => {
625+
it('is `false` if either argument is falsy', () => {
626+
expect(nodeHasType(null, {})).to.equal(false);
627+
expect(nodeHasType({}, null)).to.equal(false);
628+
});
637629

638-
describe('when only `node.type.displayName` matches `type`', () => {
639-
const x = {};
640-
it('is `true` when `node.type` is an object', () => {
641-
expect(nodeHasType(
642-
{ type: { displayName: x } },
643-
x,
644-
)).to.equal(true);
630+
it('is `false` if `node` has a falsy `type`', () => {
631+
expect(nodeHasType({}, {})).to.equal(false);
632+
expect(nodeHasType({ type: null }, {})).to.equal(false);
633+
expect(nodeHasType({ type: false }, {})).to.equal(false);
634+
expect(nodeHasType({ type: '' }, {})).to.equal(false);
635+
expect(nodeHasType({ type: 0 }, {})).to.equal(false);
645636
});
646637

647-
it('is `true` when `node.type` is a function', () => {
648-
expect(nodeHasType(
649-
{ type: Object.assign(() => {}, { displayName: x }) },
650-
x,
651-
)).to.equal(true);
638+
it('compares `node.type` to `type` when `node.type` is a non-empty string', () => {
639+
expect(nodeHasType({ type: 'foo' }, 'foo')).to.equal(true);
640+
expect(nodeHasType({ type: 'foo' }, 'bar')).to.equal(false);
652641
});
653-
});
654642

655-
describe('when only `node.type.name` matches `type`', () => {
656-
const x = {};
657-
it('is `true` when `node.type` is an object', () => {
658-
expect(nodeHasType(
659-
{ type: { name: x } },
660-
x,
661-
)).to.equal(true);
643+
describe('when only `node.type.displayName` matches `type`', () => {
644+
const x = {};
645+
it('is `true` when `node.type` is an object', () => {
646+
expect(nodeHasType(
647+
{ type: { displayName: x } },
648+
x,
649+
)).to.equal(true);
650+
});
651+
652+
it('is `true` when `node.type` is a function', () => {
653+
expect(nodeHasType(
654+
{ type: Object.assign(() => {}, { displayName: x }) },
655+
x,
656+
)).to.equal(true);
657+
});
662658
});
663659

664-
it('is `true` when `node.type` is a function', () => {
665-
function namedType() {}
660+
describe('when only `node.type.name` matches `type`', () => {
661+
const x = {};
662+
it('is `true` when `node.type` is an object', () => {
663+
expect(nodeHasType(
664+
{ type: { name: x } },
665+
x,
666+
)).to.equal(true);
667+
});
668+
669+
it('is `true` when `node.type` is a function', () => {
670+
function namedType() {}
666671

667-
expect(nodeHasType({ type: namedType }, 'namedType')).to.equal(true);
672+
expect(nodeHasType({ type: namedType }, 'namedType')).to.equal(true);
673+
});
668674
});
675+
676+
wrap()
677+
.withOverride(() => getAdapter(), 'displayNameOfNode', () => sinon.stub())
678+
.describe('when the adapter has a `displayNameOfNode` function', () => {
679+
it('is `true` when `displayNameOfNode` matches `type`', () => {
680+
const stub = getAdapter().displayNameOfNode;
681+
const sentinel = {};
682+
stub.returns(sentinel);
683+
684+
const node = {};
685+
expect(nodeHasType(node, sentinel)).to.equal(true);
686+
687+
expect(stub).to.have.property('callCount', 1);
688+
const { args } = stub.firstCall;
689+
expect(args).to.eql([node]);
690+
});
691+
692+
it('is `false` when `displayNameOfNode` does not match `type`', () => {
693+
const stub = getAdapter().displayNameOfNode;
694+
const sentinel = {};
695+
stub.returns(sentinel);
696+
697+
const node = {};
698+
expect(nodeHasType(node, {})).to.equal(false);
699+
});
700+
});
669701
});
670-
});
671702
});

packages/enzyme/src/Utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ export function typeOfNode(node) {
6161

6262
export function nodeHasType(node, type) {
6363
if (!type || !node) return false;
64+
65+
const adapter = getAdapter();
66+
if (adapter.displayNameOfNode) {
67+
const displayName = adapter.displayNameOfNode(node);
68+
return displayName === type;
69+
}
70+
6471
if (!node.type) return false;
6572
if (typeof node.type === 'string') return node.type === type;
6673
return (

0 commit comments

Comments
 (0)