Skip to content

Commit

Permalink
fix(Button|Menu|List|Step): fix behaviour of onClick when is disabl…
Browse files Browse the repository at this point in the history
…ed (#2006)
  • Loading branch information
layershifter authored and levithomason committed Aug 27, 2017
1 parent b123a10 commit e372781
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/collections/Menu/MenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ export default class MenuItem extends Component {
}

handleClick = (e) => {
const { onClick } = this.props
const { disabled } = this.props

if (onClick) onClick(e, this.props)
if (!disabled) _.invoke(this.props, 'onClick', e, this.props)
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions src/elements/List/ListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class ListItem extends Component {
}

handleClick = (e) => {
const { onClick } = this.props
const { disabled } = this.props

if (onClick) onClick(e, this.props)
if (!disabled) _.invoke(this.props, 'onClick', e, this.props)
}

render() {
Expand Down
5 changes: 3 additions & 2 deletions src/elements/Step/Step.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 @@ -79,9 +80,9 @@ export default class Step extends Component {
static Title = StepTitle

handleClick = (e) => {
const { onClick } = this.props
const { disabled } = this.props

if (onClick) onClick(e, this.props)
if (!disabled) _.invoke(this.props, 'onClick', e, this.props)
}

render() {
Expand Down
23 changes: 13 additions & 10 deletions test/specs/collections/Menu/MenuItem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,24 @@ describe('MenuItem', () => {
})

describe('onClick', () => {
it('omitted when not defined', () => {
const click = () => shallow(<MenuItem />).simulate('click')
expect(click).to.not.throw()
})

it('is called with (e, { name, index }) when clicked', () => {
const spy = sandbox.spy()
it('is called with (e, data) when clicked', () => {
const onClick = sandbox.spy()
const event = { target: null }
const props = { name: 'home', index: 0 }

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

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

it('is not called when is disabled', () => {
const onClick = sandbox.spy()

shallow(<MenuItem disabled onClick={onClick} />)
.simulate('click')
onClick.should.have.callCount(0)
})

it('renders an `a` tag', () => {
Expand Down
18 changes: 9 additions & 9 deletions test/specs/elements/Button/Button-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,22 @@ describe('Button', () => {
})

describe('onClick', () => {
it('is called when clicked', () => {
const handleClick = sandbox.spy()
it('is called with (e, data) when clicked', () => {
const onClick = sandbox.spy()

shallow(<Button type='submit' onClick={handleClick} />)
shallow(<Button onClick={onClick} />)
.simulate('click', syntheticEvent)

handleClick.should.have.been.calledOnce()
onClick.should.have.been.calledOnce()
onClick.should.have.been.calledWithExactly(syntheticEvent, { onClick, as: 'button' })
})

it('is not called when button is disabled', () => {
const handleClick = sandbox.spy()
it('is not called when is disabled', () => {
const onClick = sandbox.spy()

shallow(<Button type='submit' disabled onClick={handleClick} />)
shallow(<Button disabled onClick={onClick} />)
.simulate('click', syntheticEvent)

handleClick.should.not.have.been.calledOnce()
onClick.should.have.callCount(0)
})
})

Expand Down
15 changes: 9 additions & 6 deletions test/specs/elements/List/ListItem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ describe('ListItem', () => {
})

describe('onClick', () => {
it('can be omitted', () => {
const click = () => shallow(<ListItem />).simulate('click')
expect(click).to.not.throw()
})

it('is called with (e, props) when clicked', () => {
it('is called with (e, data) when clicked', () => {
const onClick = sandbox.spy()
const event = { target: null }
const props = { onClick, 'data-foo': 'bar' }
Expand All @@ -38,6 +33,14 @@ describe('ListItem', () => {
onClick.should.have.been.calledOnce()
onClick.should.have.been.calledWithExactly(event, props)
})

it('is not called when is disabled', () => {
const onClick = sandbox.spy()

shallow(<ListItem disabled onClick={onClick} />)
.simulate('click')
onClick.should.have.callCount(0)
})
})

describe('value', () => {
Expand Down
28 changes: 18 additions & 10 deletions test/specs/elements/Step/Step-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,28 @@ describe('Step', () => {
})

describe('onClick prop', () => {
it('omitted when not defined', () => {
const click = () => shallow(<Step />).simulate('click')
expect(click).to.not.throw()
it('is called with (e, data) when clicked', () => {
const event = { target: null }
const onClick = sandbox.spy()

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

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

it('renders <a> and handles click', () => {
const handleClick = sandbox.spy()
const wrapper = mount(<Step onClick={handleClick} />)
it('is not called when is disabled', () => {
const onClick = sandbox.spy()

wrapper.should.have.tagName('a')
wrapper.simulate('click')
shallow(<Step disabled onClick={onClick} />)
.simulate('click')
onClick.should.have.callCount(0)
})

handleClick.should.have.been.calledOnce()
handleClick.should.have.been.calledWithMatch({})
it('renders an `a` tag', () => {
shallow(<Step onClick={() => null} />)
.should.have.tagName('a')
})
})
})
Expand Down

0 comments on commit e372781

Please sign in to comment.