Skip to content

Commit

Permalink
Breakout separate module for displayName
Browse files Browse the repository at this point in the history
  • Loading branch information
nhunzaker committed Dec 5, 2016
1 parent a7c2e1c commit a31ef25
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
9 changes: 1 addition & 8 deletions src/addons/with-intent.js
Expand Up @@ -4,20 +4,13 @@

import React from 'react'
import merge from '../merge'
import displayName from '../display-name'

const danger = function (intent, params) {
console.error('Unable to broadcast "%s" with parameters `%s`. withIntent did not receive context.',
intent, JSON.stringify(params))
}

const displayName = function (Component) {
if (typeof Component === 'function') {
return Component.name
}

return Component.constructor.name || Component.displayName
}

export default function withIntent (Component, intent) {

function WithIntent (props, context) {
Expand Down
4 changes: 4 additions & 0 deletions src/display-name.js
@@ -0,0 +1,4 @@
// Thanks: https://github.com/jurassix/react-display-name
export default function (Component) {
return Component.displayName || Component.name || 'Component'
}
15 changes: 0 additions & 15 deletions test/addons/withIntent.test.js
Expand Up @@ -74,19 +74,4 @@ describe('When there is no context (called directly as a function)', function ()
expect(logger.last('error')).toContain('withIntent(Button)')
})

test('uses the component name in the debug message for class components', function () {
const Button = withIntent(class Button extends React.PureComponent {
render () {
var { send } = this.props
return (
<button type="button" onClick={() => send('intent')}>Click me</button>
)
}
})

Button()

expect(logger.last('error')).toContain('withIntent(Button)')
})

})
33 changes: 33 additions & 0 deletions test/display-name.test.js
@@ -0,0 +1,33 @@
import React from 'react'
import displayName from '../src/display-name'

test('gets a stateless component name', function () {
const name = displayName(function Button () {})

expect(name).toBe('Button')
})

test('gets a class component name', function () {
const name = displayName(class Button extends React.PureComponent {})

expect(name).toBe('Button')
})

test('gets a createClass component name', function () {
const Button = React.createClass({
render () {
var { send } = this.props
return (
<button type="button" onClick={() => send('intent')}>Click me</button>
)
}
})

const name = displayName(Button)

expect(name).toBe('Button')
})

test('uses "Component" when there is no name', function () {
expect(displayName(React.createClass({ render () {} }))).toBe('Component')
})

0 comments on commit a31ef25

Please sign in to comment.