Skip to content

Commit

Permalink
fix(stateless comps): add No Display Name as default component name
Browse files Browse the repository at this point in the history
fixes #11
  • Loading branch information
vvo committed Dec 10, 2015
1 parent ffbfc45 commit dc0f65c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
15 changes: 12 additions & 3 deletions index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
).toEqual(`<NamedStatelessComponent />`);
});

// cannot be done, see https://github.com/algolia/react-element-to-jsx-string/issues/11
it.skip(`reactElementToJSXString(<AnonymousStatelessComponent/>)`, () => {
it(`reactElementToJSXString(<AnonymousStatelessComponent/>)`, () => {
expect(
reactElementToJSXString(<AnonymousStatelessComponent/>)
).toEqual(`<AnonymousStatelessComponent />`);
).toEqual(`<No Display Name />`);
});

it(`reactElementToJSXString(<AnonymousStatelessComponent/>) with a displayName`, () => {
AnonymousStatelessComponent.displayName = 'I have a name!';

expect(
reactElementToJSXString(<AnonymousStatelessComponent/>)
).toEqual(`<I have a name! />`);

delete AnonymousStatelessComponent.displayName;
});

it(`reactElementToJSXString(React.createElement('div'))`, () => {
Expand Down
15 changes: 5 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
'got `' + (typeof ReactElement) + '`');
}

let tagName;

if (ReactElement.type === undefined) {
tagName = 'UnknownElement';
} else {
tagName = ReactElement.type.name ||
ReactElement.type.displayName ||
ReactElement.type.type ||
ReactElement.type;
}
let tagName = ReactElement.type.name || // function name
ReactElement.type.displayName ||
(typeof ReactElement.type === 'function' ? // function without a name, you should provide one
'No Display Name' :
ReactElement.type);

let out = `<${tagName}`;
let props = formatProps(ReactElement.props);
Expand Down

0 comments on commit dc0f65c

Please sign in to comment.