import {expect} from 'chai'
import keyboardClickable from '../keyboardClickable'

describe('keyboardClickable', () => {
  context('when given a spacebar key event', () => {
    it('fires a click event on target', () => {
      const spaceDownEvent = {
        keyCode: 13,
        target: {
          click: sinon.spy()
        },
        stopPropagation: sinon.spy(),
        preventDefault: sinon.spy()
      }

      keyboardClickable(spaceDownEvent)
      expect(spaceDownEvent.target.click.callCount).to.eq(1)
      expect(spaceDownEvent.stopPropagation.callCount).to.eq(1)
      expect(spaceDownEvent.preventDefault.callCount).to.eq(1)
    })
  })

  context('when given a non spacebar or enter key event', () => {
    it('does not fire a click event on target', () => {
      const shiftDownEvent = {
        keyCode: 16,
        target: {
          click: sinon.spy()
        },
        stopPropagation: sinon.spy(),
        preventDefault: sinon.spy()
      }

      keyboardClickable(shiftDownEvent)
      expect(shiftDownEvent.target.click.callCount).to.eq(0)
      expect(shiftDownEvent.stopPropagation.callCount).to.eq(0)
      expect(shiftDownEvent.preventDefault.callCount).to.eq(0)
    })
  })
})
