Skip to content

Commit

Permalink
[New] Debug: typeName now calls the adapter’s displayNameOfNode
Browse files Browse the repository at this point in the history
… if available
  • Loading branch information
jquense authored and ljharb committed Jul 2, 2018
1 parent e163a35 commit c401fde
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"enzyme": "^3.3.0",
"enzyme-adapter-utils": "^1.3.0",
"jsdom": "^6.5.1",
"mocha-wrap": "^2.1.2",
"object.assign": "^4.1.0",
"prop-types": "^15.6.0",
"semver": "^5.5.0",
Expand Down
67 changes: 48 additions & 19 deletions packages/enzyme-test-suite/test/Debug-spec.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { expect } from 'chai';
import React from 'react';
import wrap from 'mocha-wrap';
import sinon from 'sinon';

import { mount, shallow } from 'enzyme';
import { get } from 'enzyme/build/configuration';
import {
Expand All @@ -23,30 +26,56 @@ const { adapter } = get();
const debugElement = element => debugNode(adapter.elementToNode(element));

describe('debug', () => {
describe('typeName(node)', () => {
it('returns `.type` when not a function', () => {
const type = {};
expect(typeName({ type })).to.equal(type);
});

describe('when `.type` is a function', () => {
it('returns the function’s name', () => {
function Foo() {}
expect(typeName({ type: Foo })).to.equal('Foo');
wrap()
.withOverride(() => adapter, 'displayNameOfNode', () => undefined)
.describe('typeName(node)', () => {
it('returns `.type` when not a function', () => {
const type = {};
expect(typeName({ type })).to.equal(type);
});

it('returns the function’s `.displayName` when present', () => {
function Foo() {}
Foo.displayName = 'Bar';
expect(typeName({ type: Foo })).to.equal('Bar');
describe('when `.type` is a function', () => {
it('returns the function’s name', () => {
function Foo() {}
expect(typeName({ type: Foo })).to.equal('Foo');
});

it('returns the function’s `.displayName` when present', () => {
function Foo() {}
Foo.displayName = 'Bar';
expect(typeName({ type: Foo })).to.equal('Bar');
});

it('returns "Component" when the function is anonymous', () => {
const anon = Object(() => {});
expect(typeName({ type: anon })).to.equal('Component');
});
});

it('returns "Component" when the function is anonymous', () => {
const anon = Object(() => {});
expect(typeName({ type: anon })).to.equal('Component');
});
wrap()
.withOverride(() => adapter, 'displayNameOfNode', () => sinon.stub())
.describe('when the adapter has a `displayNameOfNode` function', () => {
it('calls it, and returns its return value', () => {
const stub = adapter.displayNameOfNode;
const sentinel = {};
stub.returns(sentinel);

const node = {};
expect(typeName(node)).to.equal(sentinel);

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

it('returns "Component" when `adapter.displayNameOfNode` returns something falsy', () => {
const stub = adapter.displayNameOfNode;
stub.returns('');

expect(typeName()).to.equal('Component');
});
});
});
});

describe('spaces(n)', () => {
it('should return n spaces', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/enzyme/src/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import {
propsOfNode,
childrenOfNode,
} from './RSTTraversal';
import { getAdapter } from './Utils';

const booleanValue = Function.bind.call(Function.call, Boolean.prototype.valueOf);

export function typeName(node) {
const adapter = getAdapter();
if (adapter.displayNameOfNode) {
return getAdapter().displayNameOfNode(node) || 'Component';
}
return typeof node.type === 'function'
? (node.type.displayName || functionName(node.type) || 'Component')
: node.type;
Expand Down

0 comments on commit c401fde

Please sign in to comment.