1
1
import { patchEvent } from '../../src/modules/events'
2
- import { vOnModifiersGuard } from '@vue/runtime-dom'
2
+ import { vOnModifiersGuard , vOnKeysGuard } from '@vue/runtime-dom'
3
3
4
4
function triggerEvent (
5
5
target : Element ,
@@ -17,41 +17,93 @@ function triggerEvent(
17
17
}
18
18
19
19
describe ( 'runtime-dom: v-on directive' , ( ) => {
20
- test ( 'it should support stop and prevent' , async ( ) => {
20
+ test ( 'it should support " stop" and " prevent"' , ( ) => {
21
21
const parent = document . createElement ( 'div' )
22
22
const child = document . createElement ( 'input' )
23
23
parent . appendChild ( child )
24
- const childNextValue = {
25
- handler : vOnModifiersGuard ( jest . fn ( ) , [ 'prevent' , 'stop' ] ) ,
26
- options : { }
27
- }
24
+ const childNextValue = vOnModifiersGuard ( jest . fn ( ) , [ 'prevent' , 'stop' ] )
28
25
patchEvent ( child , 'click' , null , childNextValue , null )
29
- const parentHandler = jest . fn ( )
30
- const parentNextValue = { handler : parentHandler , options : { } }
26
+ const parentNextValue = jest . fn ( )
31
27
patchEvent ( parent , 'click' , null , parentNextValue , null )
32
28
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 ( )
34
41
} )
35
42
36
43
test ( 'it should support key modifiers and system modifiers' , ( ) => {
37
44
const el = document . createElement ( 'div' )
38
45
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 => {
45
52
e . ctrlKey = false
46
53
e . key = 'esc'
47
54
} )
48
55
expect ( fn ) . not . toBeCalled ( )
49
- triggerEvent ( el , 'click ' , e => {
56
+ triggerEvent ( el , 'keyup ' , e => {
50
57
e . ctrlKey = true
51
58
e . key = 'Escape'
52
59
} )
53
60
expect ( fn ) . toBeCalled ( )
54
61
} )
55
62
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
+ } )
57
109
} )
0 commit comments