Commit 4d8e9cac authored by HaNdTriX's avatar HaNdTriX Committed by Tim Neutkens

Improve getDisplayName util (#4944)

- [x] Add unit test
- [x] Allow to get the display name of a native component (e.g.: `div`) without throwing
- [ ] Remove displayName in production mode
parent 9319c158
...@@ -46,6 +46,10 @@ export function printAndExit (message, code = 1) { ...@@ -46,6 +46,10 @@ export function printAndExit (message, code = 1) {
} }
export function getDisplayName (Component) { export function getDisplayName (Component) {
if (typeof Component === 'string') {
return Component
}
return Component.displayName || Component.name || 'Unknown' return Component.displayName || Component.name || 'Unknown'
} }
......
/* global describe, it, expect */
import { Component } from 'react'
import { getDisplayName } from '../../dist/lib/utils'
describe('getDisplayName', () => {
it('gets the proper display name of a component', () => {
class ComponentOne extends Component {
render () {
return null
}
}
class ComponentTwo extends Component {
static displayName = 'CustomDisplayName'
render () {
return null
}
}
function FunctionalComponent () {
return null
}
expect(getDisplayName(ComponentOne)).toBe('ComponentOne')
expect(getDisplayName(ComponentTwo)).toBe('CustomDisplayName')
expect(getDisplayName(FunctionalComponent)).toBe('FunctionalComponent')
expect(getDisplayName(() => null)).toBe('Unknown')
expect(getDisplayName('div')).toBe('div')
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment