Skip to content

Instantly share code, notes, and snippets.

@TheDutchCoder
Last active July 26, 2017 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheDutchCoder/7260fd66be953046dc1542fd2ba5bf94 to your computer and use it in GitHub Desktop.
Save TheDutchCoder/7260fd66be953046dc1542fd2ba5bf94 to your computer and use it in GitHub Desktop.
// other tests
it('should toggle the component when it\'s open and ESC is pressed', (done) => {
vm = createTest(CpColorPicker, defaultProps)
expect(vm.isOpen).to.equal(false)
vm.isOpen = true
Vue.nextTick(() => {
expect(vm.isOpen).to.equal(true)
// I can't create multiple mocked events here to test the `let key = 'which' in e ? e.which : e.keyCode` case.
// This needs a separate test where I'm just repeating things over and over.
// `if (key === 27 && this.isOpen)` also needs additional tests, so I end up with 4 tests I just want to wrap in one, because they all test the same behaviour.
let event = { keyCode: 27 }
sinon.spy(vm, 'handleKeyUp')
sinon.spy(vm, 'toggle')
vm.handleKeyUp(event)
expect(vm.handleKeyUp.callCount).to.equal(1)
expect(vm.toggle.callCount).to.equal(1)
vm.handleKeyUp.restore()
vm.toggle.restore()
done()
})
})
methods: {
handleKeyUp (e) {
let key = 'which' in e ? e.which : e.keyCode // This branch needs 2 tests
if (key === 27 && this.isOpen) { // This branch needs 2 additional tests
this.toggle()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment