1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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
This commit is contained in:
HaNdTriX 2018-08-13 20:03:59 +02:00 committed by Tim Neutkens
parent 9319c158a5
commit 4d8e9cacdd
2 changed files with 34 additions and 0 deletions

View file

@ -46,6 +46,10 @@ export function printAndExit (message, code = 1) {
}
export function getDisplayName (Component) {
if (typeof Component === 'string') {
return Component
}
return Component.displayName || Component.name || 'Unknown'
}

View file

@ -0,0 +1,30 @@
/* 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')
})
})