Skip to content

Commit

Permalink
test(runtime-dom): improve v-on system key modifiers test (#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
motao314 authored Jul 19, 2020
1 parent 61b02d8 commit b8db7ab
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions packages/runtime-dom/__tests__/directives/vOn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,39 @@ describe('runtime-dom: v-on directive', () => {
})

test('it should support key modifiers and system modifiers', () => {
const el = document.createElement('div')
const fn = jest.fn()
// <div @keyup.ctrl.esc="test"/>
const nextValue = withKeys(withModifiers(fn, ['ctrl']), [
'esc',
'arrow-left'
])
patchEvent(el, 'onKeyup', null, nextValue, null)

triggerEvent(el, 'keyup', e => (e.key = 'a'))
expect(fn).not.toBeCalled()

triggerEvent(el, 'keyup', e => {
e.ctrlKey = false
e.key = 'esc'
})
expect(fn).not.toBeCalled()
const keyNames = ["ctrl","shift","meta","alt"]

triggerEvent(el, 'keyup', e => {
e.ctrlKey = true
e.key = 'Escape'
})
expect(fn).toBeCalledTimes(1)

triggerEvent(el, 'keyup', e => {
e.ctrlKey = true
e.key = 'ArrowLeft'
})
expect(fn).toBeCalledTimes(2)
keyNames.forEach(keyName=>{
const el = document.createElement('div')
const fn = jest.fn()
// <div @keyup[keyName].esc="test"/>
const nextValue = withKeys(withModifiers(fn, [keyName]), [
'esc',
'arrow-left'
])
patchEvent(el, 'onKeyup', null, nextValue, null)

triggerEvent(el, 'keyup', e => (e.key = 'a'))
expect(fn).not.toBeCalled()

triggerEvent(el, 'keyup', e => {
e[`${keyName}Key`] = false
e.key = 'esc'
})
expect(fn).not.toBeCalled()

triggerEvent(el, 'keyup', e => {
e[`${keyName}Key`] = true
e.key = 'Escape'
})
expect(fn).toBeCalledTimes(1)

triggerEvent(el, 'keyup', e => {
e[`${keyName}Key`] = true
e.key = 'ArrowLeft'
})
expect(fn).toBeCalledTimes(2)
});
})

test('it should support "exact" modifier', () => {
Expand Down

0 comments on commit b8db7ab

Please sign in to comment.