Skip to content

Commit db5c343

Browse files
fnlctrlyyx990803
authored andcommitted
test(runtime-dom): add test coverage for v-on runtime guards, fix "exact" guard (vuejs#298)
1 parent 3385480 commit db5c343

File tree

4 files changed

+91
-24
lines changed

4 files changed

+91
-24
lines changed

packages/compiler-dom/__tests__/transforms/vOn.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,17 @@ describe('compiler-dom: transform v-on', () => {
9090
})
9191
})
9292
})
93+
94+
it('should not wrap keys guard if no key modifier is present', () => {
95+
const [prop] = parseVOnProperties(`<div @keyup.exact="test"/>`, {
96+
prefixIdentifiers: true
97+
})
98+
expect(prop).toMatchObject({
99+
type: NodeTypes.JS_PROPERTY,
100+
value: {
101+
callee: V_ON_MODIFIERS_GUARD,
102+
arguments: [{ content: '_ctx.test' }, '["exact"]']
103+
}
104+
})
105+
})
93106
})

packages/compiler-dom/src/transforms/vOn.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
4141
value,
4242
JSON.stringify(runtimeModifiers.filter(m => m in NOT_KEY_MODIFIERS))
4343
])
44+
const keyModifiers = runtimeModifiers.filter(m => !(m in NOT_KEY_MODIFIERS))
4445
if (
46+
keyModifiers.length &&
4547
// if event name is dynamic, always wrap with keys guard
46-
key.type === NodeTypes.COMPOUND_EXPRESSION ||
47-
!key.isStatic ||
48-
key.content.toLowerCase() in KEYBOARD_EVENTS
48+
(key.type === NodeTypes.COMPOUND_EXPRESSION ||
49+
!key.isStatic ||
50+
key.content.toLowerCase() in KEYBOARD_EVENTS)
4951
) {
5052
handler = createCallExpression(context.helper(V_ON_KEYS_GUARD), [
5153
handler,
52-
JSON.stringify(runtimeModifiers.filter(m => !(m in NOT_KEY_MODIFIERS)))
54+
JSON.stringify(keyModifiers)
5355
])
5456
}
5557

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { patchEvent } from '../../src/modules/events'
2-
import { vOnModifiersGuard } from '@vue/runtime-dom'
2+
import { vOnModifiersGuard, vOnKeysGuard } from '@vue/runtime-dom'
33

44
function triggerEvent(
55
target: Element,
@@ -17,41 +17,93 @@ function triggerEvent(
1717
}
1818

1919
describe('runtime-dom: v-on directive', () => {
20-
test('it should support stop and prevent', async () => {
20+
test('it should support "stop" and "prevent"', () => {
2121
const parent = document.createElement('div')
2222
const child = document.createElement('input')
2323
parent.appendChild(child)
24-
const childNextValue = {
25-
handler: vOnModifiersGuard(jest.fn(), ['prevent', 'stop']),
26-
options: {}
27-
}
24+
const childNextValue = vOnModifiersGuard(jest.fn(), ['prevent', 'stop'])
2825
patchEvent(child, 'click', null, childNextValue, null)
29-
const parentHandler = jest.fn()
30-
const parentNextValue = { handler: parentHandler, options: {} }
26+
const parentNextValue = jest.fn()
3127
patchEvent(parent, 'click', null, parentNextValue, null)
3228
expect(triggerEvent(child, 'click').defaultPrevented).toBe(true)
33-
expect(parentHandler).not.toBeCalled()
29+
expect(parentNextValue).not.toBeCalled()
30+
})
31+
32+
test('it should support "self"', () => {
33+
const parent = document.createElement('div')
34+
const child = document.createElement('input')
35+
parent.appendChild(child)
36+
const fn = jest.fn()
37+
const handler = vOnModifiersGuard(fn, ['self'])
38+
patchEvent(parent, 'click', null, handler, null)
39+
triggerEvent(child, 'click')
40+
expect(fn).not.toBeCalled()
3441
})
3542

3643
test('it should support key modifiers and system modifiers', () => {
3744
const el = document.createElement('div')
3845
const fn = jest.fn()
39-
const nextValue = {
40-
handler: vOnModifiersGuard(fn, ['ctrl', 'esc']),
41-
options: {}
42-
}
43-
patchEvent(el, 'click', null, nextValue, null)
44-
triggerEvent(el, 'click', e => {
46+
// <div @keyup.ctrl.esc="test"/>
47+
const nextValue = vOnKeysGuard(vOnModifiersGuard(fn, ['ctrl']), ['esc'])
48+
patchEvent(el, 'keyup', null, nextValue, null)
49+
triggerEvent(el, 'keyup', e => (e.key = 'a'))
50+
expect(fn).not.toBeCalled()
51+
triggerEvent(el, 'keyup', e => {
4552
e.ctrlKey = false
4653
e.key = 'esc'
4754
})
4855
expect(fn).not.toBeCalled()
49-
triggerEvent(el, 'click', e => {
56+
triggerEvent(el, 'keyup', e => {
5057
e.ctrlKey = true
5158
e.key = 'Escape'
5259
})
5360
expect(fn).toBeCalled()
5461
})
5562

56-
test('it should support "exact" modifier', () => {})
63+
test('it should support "exact" modifier', () => {
64+
const el = document.createElement('div')
65+
// Case 1: <div @keyup.exact="test"/>
66+
const fn1 = jest.fn()
67+
const next1 = vOnModifiersGuard(fn1, ['exact'])
68+
patchEvent(el, 'keyup', null, next1, null)
69+
triggerEvent(el, 'keyup')
70+
expect(fn1.mock.calls.length).toBe(1)
71+
triggerEvent(el, 'keyup', e => (e.ctrlKey = true))
72+
expect(fn1.mock.calls.length).toBe(1)
73+
// Case 2: <div @keyup.ctrl.a.exact="test"/>
74+
const fn2 = jest.fn()
75+
const next2 = vOnKeysGuard(vOnModifiersGuard(fn2, ['ctrl', 'exact']), ['a'])
76+
patchEvent(el, 'keyup', null, next2, null)
77+
triggerEvent(el, 'keyup', e => (e.key = 'a'))
78+
expect(fn2).not.toBeCalled()
79+
triggerEvent(el, 'keyup', e => {
80+
e.key = 'a'
81+
e.ctrlKey = true
82+
})
83+
expect(fn2.mock.calls.length).toBe(1)
84+
triggerEvent(el, 'keyup', e => {
85+
// should not trigger if has other system modifiers
86+
e.key = 'a'
87+
e.ctrlKey = true
88+
e.altKey = true
89+
})
90+
expect(fn2.mock.calls.length).toBe(1)
91+
})
92+
93+
it('should support mouse modifiers', () => {
94+
const buttons = ['left', 'middle', 'right'] as const
95+
const buttonCodes = { left: 0, middle: 1, right: 2 }
96+
buttons.forEach(button => {
97+
const el = document.createElement('div')
98+
const fn = jest.fn()
99+
const handler = vOnModifiersGuard(fn, [button])
100+
patchEvent(el, 'mousedown', null, handler, null)
101+
buttons.filter(b => b !== button).forEach(button => {
102+
triggerEvent(el, 'mousedown', e => (e.button = buttonCodes[button]))
103+
})
104+
expect(fn).not.toBeCalled()
105+
triggerEvent(el, 'mousedown', e => (e.button = buttonCodes[button]))
106+
expect(fn).toBeCalled()
107+
})
108+
})
57109
})

packages/runtime-dom/src/directives/vOn.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const systemModifiers = new Set(['ctrl', 'shift', 'alt', 'meta'])
1+
const systemModifiers = ['ctrl', 'shift', 'alt', 'meta']
22

33
type KeyedEvent = KeyboardEvent | MouseEvent | TouchEvent;
44

@@ -16,8 +16,8 @@ const modifierGuards: Record<
1616
left: e => 'button' in e && (e as MouseEvent).button !== 0,
1717
middle: e => 'button' in e && (e as MouseEvent).button !== 1,
1818
right: e => 'button' in e && (e as MouseEvent).button !== 2,
19-
exact: (e, modifiers) =>
20-
modifiers!.some(m => systemModifiers.has(m) && (e as any)[`${m}Key`])
19+
exact: (e, modifiers: string[]) =>
20+
systemModifiers.some(m => (e as any)[`${m}Key`] && !modifiers.includes(m))
2121
}
2222

2323
export const vOnModifiersGuard = (fn: Function, modifiers: string[]) => {

0 commit comments

Comments
 (0)