2019-10-14 12:33:23 +08:00
|
|
|
import { patchEvent } from '../../src/modules/events'
|
2019-10-19 04:20:45 +08:00
|
|
|
import { withModifiers, withKeys } from '@vue/runtime-dom'
|
2019-10-14 12:33:23 +08:00
|
|
|
|
|
|
|
function triggerEvent(
|
|
|
|
target: Element,
|
|
|
|
event: string,
|
|
|
|
process?: (e: any) => any
|
|
|
|
) {
|
|
|
|
const e = document.createEvent('HTMLEvents')
|
|
|
|
e.initEvent(event, true, true)
|
|
|
|
if (event === 'click') {
|
|
|
|
;(e as any).button = 0
|
|
|
|
}
|
|
|
|
if (process) process(e)
|
|
|
|
target.dispatchEvent(e)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('runtime-dom: v-on directive', () => {
|
2019-10-16 05:08:44 +08:00
|
|
|
test('it should support "stop" and "prevent"', () => {
|
2019-10-14 12:33:23 +08:00
|
|
|
const parent = document.createElement('div')
|
|
|
|
const child = document.createElement('input')
|
|
|
|
parent.appendChild(child)
|
2019-10-19 04:20:45 +08:00
|
|
|
const childNextValue = withModifiers(jest.fn(), ['prevent', 'stop'])
|
2020-04-07 23:41:48 +08:00
|
|
|
patchEvent(child, 'onClick', null, childNextValue, null)
|
2019-10-16 05:08:44 +08:00
|
|
|
const parentNextValue = jest.fn()
|
2020-04-07 23:41:48 +08:00
|
|
|
patchEvent(parent, 'onClick', null, parentNextValue, null)
|
2019-10-14 12:33:23 +08:00
|
|
|
expect(triggerEvent(child, 'click').defaultPrevented).toBe(true)
|
2019-10-16 05:08:44 +08:00
|
|
|
expect(parentNextValue).not.toBeCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('it should support "self"', () => {
|
|
|
|
const parent = document.createElement('div')
|
|
|
|
const child = document.createElement('input')
|
|
|
|
parent.appendChild(child)
|
|
|
|
const fn = jest.fn()
|
2019-10-19 04:20:45 +08:00
|
|
|
const handler = withModifiers(fn, ['self'])
|
2020-04-07 23:41:48 +08:00
|
|
|
patchEvent(parent, 'onClick', null, handler, null)
|
2019-10-16 05:08:44 +08:00
|
|
|
triggerEvent(child, 'click')
|
|
|
|
expect(fn).not.toBeCalled()
|
2019-10-14 12:33:23 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('it should support key modifiers and system modifiers', () => {
|
2021-07-20 06:24:18 +08:00
|
|
|
const keyNames = ['ctrl', 'shift', 'meta', 'alt']
|
2019-11-09 06:50:59 +08:00
|
|
|
|
2021-07-20 06:24:18 +08:00
|
|
|
keyNames.forEach(keyName => {
|
2020-07-19 10:55:39 +08:00
|
|
|
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)
|
2021-07-20 06:24:18 +08:00
|
|
|
|
2020-07-19 10:55:39 +08:00
|
|
|
triggerEvent(el, 'keyup', e => (e.key = 'a'))
|
|
|
|
expect(fn).not.toBeCalled()
|
2021-07-20 06:24:18 +08:00
|
|
|
|
2020-07-19 10:55:39 +08:00
|
|
|
triggerEvent(el, 'keyup', e => {
|
|
|
|
e[`${keyName}Key`] = false
|
|
|
|
e.key = 'esc'
|
|
|
|
})
|
|
|
|
expect(fn).not.toBeCalled()
|
2021-07-20 06:24:18 +08:00
|
|
|
|
2020-07-19 10:55:39 +08:00
|
|
|
triggerEvent(el, 'keyup', e => {
|
|
|
|
e[`${keyName}Key`] = true
|
|
|
|
e.key = 'Escape'
|
|
|
|
})
|
|
|
|
expect(fn).toBeCalledTimes(1)
|
2021-07-20 06:24:18 +08:00
|
|
|
|
2020-07-19 10:55:39 +08:00
|
|
|
triggerEvent(el, 'keyup', e => {
|
|
|
|
e[`${keyName}Key`] = true
|
|
|
|
e.key = 'ArrowLeft'
|
|
|
|
})
|
|
|
|
expect(fn).toBeCalledTimes(2)
|
2021-07-20 06:24:18 +08:00
|
|
|
})
|
2019-10-14 12:33:23 +08:00
|
|
|
})
|
|
|
|
|
2019-10-16 05:08:44 +08:00
|
|
|
test('it should support "exact" modifier', () => {
|
|
|
|
const el = document.createElement('div')
|
|
|
|
// Case 1: <div @keyup.exact="test"/>
|
|
|
|
const fn1 = jest.fn()
|
2019-10-19 04:20:45 +08:00
|
|
|
const next1 = withModifiers(fn1, ['exact'])
|
2020-04-07 23:41:48 +08:00
|
|
|
patchEvent(el, 'onKeyup', null, next1, null)
|
2019-10-16 05:08:44 +08:00
|
|
|
triggerEvent(el, 'keyup')
|
|
|
|
expect(fn1.mock.calls.length).toBe(1)
|
|
|
|
triggerEvent(el, 'keyup', e => (e.ctrlKey = true))
|
|
|
|
expect(fn1.mock.calls.length).toBe(1)
|
|
|
|
// Case 2: <div @keyup.ctrl.a.exact="test"/>
|
|
|
|
const fn2 = jest.fn()
|
2019-10-19 04:20:45 +08:00
|
|
|
const next2 = withKeys(withModifiers(fn2, ['ctrl', 'exact']), ['a'])
|
2020-04-07 23:41:48 +08:00
|
|
|
patchEvent(el, 'onKeyup', null, next2, null)
|
2019-10-16 05:08:44 +08:00
|
|
|
triggerEvent(el, 'keyup', e => (e.key = 'a'))
|
|
|
|
expect(fn2).not.toBeCalled()
|
|
|
|
triggerEvent(el, 'keyup', e => {
|
|
|
|
e.key = 'a'
|
|
|
|
e.ctrlKey = true
|
|
|
|
})
|
|
|
|
expect(fn2.mock.calls.length).toBe(1)
|
|
|
|
triggerEvent(el, 'keyup', e => {
|
|
|
|
// should not trigger if has other system modifiers
|
|
|
|
e.key = 'a'
|
|
|
|
e.ctrlKey = true
|
|
|
|
e.altKey = true
|
|
|
|
})
|
|
|
|
expect(fn2.mock.calls.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support mouse modifiers', () => {
|
|
|
|
const buttons = ['left', 'middle', 'right'] as const
|
|
|
|
const buttonCodes = { left: 0, middle: 1, right: 2 }
|
|
|
|
buttons.forEach(button => {
|
|
|
|
const el = document.createElement('div')
|
|
|
|
const fn = jest.fn()
|
2019-10-19 04:20:45 +08:00
|
|
|
const handler = withModifiers(fn, [button])
|
2020-04-07 23:41:48 +08:00
|
|
|
patchEvent(el, 'onMousedown', null, handler, null)
|
2021-07-20 06:24:18 +08:00
|
|
|
buttons
|
|
|
|
.filter(b => b !== button)
|
|
|
|
.forEach(button => {
|
|
|
|
triggerEvent(el, 'mousedown', e => (e.button = buttonCodes[button]))
|
|
|
|
})
|
2019-10-16 05:08:44 +08:00
|
|
|
expect(fn).not.toBeCalled()
|
|
|
|
triggerEvent(el, 'mousedown', e => (e.button = buttonCodes[button]))
|
|
|
|
expect(fn).toBeCalled()
|
|
|
|
})
|
|
|
|
})
|
2020-05-18 22:11:39 +08:00
|
|
|
|
|
|
|
it('should handle multiple arguments when using modifiers', () => {
|
|
|
|
const el = document.createElement('div')
|
|
|
|
const fn = jest.fn()
|
|
|
|
const handler = withModifiers(fn, ['ctrl'])
|
|
|
|
const event = triggerEvent(el, 'click', e => (e.ctrlKey = true))
|
|
|
|
handler(event, 'value', true)
|
|
|
|
expect(fn).toBeCalledWith(event, 'value', true)
|
|
|
|
})
|
2019-10-14 12:33:23 +08:00
|
|
|
})
|