Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(Breadcrumb): move computer of ElementType to method on BreadcrumbSection #2141

Merged
merged 1 commit into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/collections/Breadcrumb/BreadcrumbSection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cx from 'classnames'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React, { Component } from 'react'

Expand Down Expand Up @@ -60,21 +61,21 @@ export default class BreadcrumbSection extends Component {
parent: 'Breadcrumb',
}

handleClick = (e) => {
const { onClick } = this.props
computeElementType = () => {
const { link, onClick } = this.props

if (onClick) onClick(e, this.props)
if (link || onClick) return 'a'
}

handleClick = e => _.invoke(this.props, 'onClick', e, this.props)

render() {
const {
active,
children,
className,
content,
href,
link,
onClick,
} = this.props

const classes = cx(
Expand All @@ -83,9 +84,7 @@ export default class BreadcrumbSection extends Component {
className,
)
const rest = getUnhandledProps(BreadcrumbSection, this.props)
const ElementType = getElementType(BreadcrumbSection, this.props, () => {
if (link || onClick) return 'a'
})
const ElementType = getElementType(BreadcrumbSection, this.props, this.computeElementType)

return (
<ElementType {...rest} className={classes} href={href} onClick={this.handleClick}>
Expand Down
23 changes: 19 additions & 4 deletions test/specs/collections/Breadcrumb/BreadcrumbSection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'

import BreadcrumbSection from 'src/collections/Breadcrumb/BreadcrumbSection'
import * as common from 'test/specs/commonTests'
import { sandbox } from 'test/utils'

describe('BreadcrumbSection', () => {
common.isConformant(BreadcrumbSection)
Expand All @@ -10,28 +11,42 @@ describe('BreadcrumbSection', () => {
common.propKeyOnlyToClassName(BreadcrumbSection, 'active')

it('renders as a div by default', () => {
shallow(<BreadcrumbSection>Home</BreadcrumbSection>)
shallow(<BreadcrumbSection />)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify test.

.should.have.tagName('div')
})

describe('link', () => {
it('is should be `a` when has prop link', () => {
shallow(<BreadcrumbSection link>Home</BreadcrumbSection>)
shallow(<BreadcrumbSection link />)
.should.have.tagName('a')
})
})

describe('href', () => {
it('is not present by default', () => {
shallow(<BreadcrumbSection>Home</BreadcrumbSection>)
shallow(<BreadcrumbSection />)
.should.not.have.attr('href')
})

it('should have attr `href` when has prop', () => {
const section = shallow(<BreadcrumbSection href='http://google.com'>Home</BreadcrumbSection>)
const section = shallow(<BreadcrumbSection href='http://google.com' />)

section.should.have.tagName('a')
section.should.have.attr('href').and.equal('http://google.com')
})
})

describe('onClick', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added missing test 😄

it('is called with (e, props) when clicked', () => {
const onClick = sandbox.spy()
const event = { target: null }
const props = { active: true, content: 'home' }

shallow(<BreadcrumbSection onClick={onClick} {...props} />)
.simulate('click', event)

onClick.should.have.been.calledOnce()
onClick.should.have.been.calledWithMatch(event, props)
})
})
})