Skip to content

Commit

Permalink
prioritize displayName over name (enzymejs#332)
Browse files Browse the repository at this point in the history
React provides the ability to override a component's display name by
using the `displayName` property. When `displayName` is provided it
should take precedence over `name`. Prior to this pull request,
`displayName` would only ever be used if `name` is not present but it
should have been the other way around.
  • Loading branch information
toddw authored and dustinsanders committed Apr 30, 2016
1 parent cce0ec1 commit bc9cb76
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ShallowTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function getTextFromNode(node) {
}

if (node.type && typeof node.type === 'function') {
return `<${node.type.name || node.type.displayName} />`;
return `<${node.type.displayName || node.type.name} />`;
}

return childrenOfNode(node).map(getTextFromNode).join('').replace(/\s+/, ' ');
Expand Down
31 changes: 31 additions & 0 deletions test/ShallowTraversal-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
treeForEach,
treeFilter,
pathToNode,
getTextFromNode,
} from '../src/ShallowTraversal';

describe('ShallowTraversal', () => {
Expand Down Expand Up @@ -253,4 +254,34 @@ describe('ShallowTraversal', () => {

});

describe('getTextFromNode', () => {
it('should return displayName for functions that provides one', () => {
class Subject extends React.Component {
render() {
return (
<div />
);
}
}
Subject.displayName = 'CustomSubject';
const node = <Subject />;
const result = getTextFromNode(node);
expect(result).to.equal('<CustomSubject />');
});

it('should return function name if displayName is not provided', () => {
class Subject extends React.Component {
render() {
return (
<div />
);
}
}
const node = <Subject />;
const result = getTextFromNode(node);
expect(result).to.equal('<Subject />');
});

});

});

0 comments on commit bc9cb76

Please sign in to comment.