Skip to content

Commit

Permalink
test(Rating): clean up tests (Semantic-Org#3041)
Browse files Browse the repository at this point in the history
test(Rating): clean up tests
  • Loading branch information
layershifter authored Jul 25, 2018
1 parent 8c532b3 commit 085439c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
31 changes: 12 additions & 19 deletions src/modules/Rating/RatingIcon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cx from 'classnames'
import keyboardKey from 'keyboard-key'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React, { Component } from 'react'

Expand Down Expand Up @@ -55,32 +56,24 @@ export default class RatingIcon extends Component {
}

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

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

handleKeyUp = (e) => {
const { onClick, onKeyUp } = this.props

if (onKeyUp) onKeyUp(e, this.props)

if (onClick) {
switch (keyboardKey.getCode(e)) {
case keyboardKey.Enter:
case keyboardKey.Spacebar:
e.preventDefault()
onClick(e, this.props)
break
default:
}
_.invoke(this.props, 'onKeyUp', e, this.props)

switch (keyboardKey.getCode(e)) {
case keyboardKey.Enter:
case keyboardKey.Spacebar:
e.preventDefault()
_.invoke(this.props, 'onClick', e, this.props)
break
default:
}
}

handleMouseEnter = (e) => {
const { onMouseEnter } = this.props

if (onMouseEnter) onMouseEnter(e, this.props)
_.invoke(this.props, 'onMouseEnter', e, this.props)
}

render() {
Expand Down
47 changes: 24 additions & 23 deletions test/specs/modules/Rating/RatingIcon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,47 @@ describe('RatingIcon', () => {
common.propKeyOnlyToClassName(RatingIcon, 'active')
common.propKeyOnlyToClassName(RatingIcon, 'selected')

describe('onKeyUp', () => {
it('omitted when not defined', () => {
const event = { keyCode: keyboardKey.Enter, preventDefault: sandbox.spy() }
const keypress = () => shallow(<RatingIcon />).simulate('keyup', event)

expect(keypress).to.not.throw()
event.preventDefault.should.not.have.been.called()
})

it('calls onClick with (e, index) when space key is pressed', () => {
const spy = sandbox.spy()
describe('onClick', () => {
it('calls onClick with (e, data) when space key is pressed', () => {
const onClick = sandbox.spy()
const event = { keyCode: keyboardKey.Spacebar, preventDefault: sandbox.spy() }

mount(<RatingIcon index={0} onClick={spy} />).simulate('keyup', event)
mount(<RatingIcon index={0} onClick={onClick} />).simulate('keyup', event)

spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch(event, { index: 0 })
onClick.should.have.been.calledOnce()
onClick.should.have.been.calledWithMatch(event, { index: 0 })
event.preventDefault.should.have.been.calledOnce()
})

it('calls onClick with (e, index) when enter key is pressed', () => {
const spy = sandbox.spy()
it('calls onClick with (e, data) when enter key is pressed', () => {
const onClick = sandbox.spy()
const event = { keyCode: keyboardKey.Enter, preventDefault: sandbox.spy() }

mount(<RatingIcon index={0} onClick={spy} />).simulate('keyup', event)
mount(<RatingIcon index={0} onClick={onClick} />).simulate('keyup', event)

spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch(event, { index: 0 })
onClick.should.have.been.calledOnce()
onClick.should.have.been.calledWithMatch(event, { index: 0 })
event.preventDefault.should.have.been.calledOnce()
})

it('does not call onClick when non space/enter key is pressed', () => {
const spy = sandbox.spy()
const onClick = sandbox.spy()
const event = { keyCode: keyboardKey.A, preventDefault: sandbox.spy() }

const keyup = () => shallow(<RatingIcon onClick={spy} />).simulate('keyup', event)
shallow(<RatingIcon onClick={onClick} />).simulate('keyup', event)

expect(keyup).to.not.throw()
spy.should.not.have.been.called()
onClick.should.not.have.been.called()
event.preventDefault.should.not.have.been.called()
})
})

describe('onKeyUp', () => {
it('calls onKeyUp with (e, data) when key is pressed', () => {
const onKeyUp = sandbox.spy()
mount(<RatingIcon index={0} onKeyUp={onKeyUp} />).simulate('keyup')

onKeyUp.should.have.been.calledOnce()
onKeyUp.should.have.been.calledWithMatch({}, { index: 0 })
})
})
})

0 comments on commit 085439c

Please sign in to comment.