@@ -5,101 +5,95 @@ const timeout = () => new Promise(r => setTimeout(r))
5
5
describe ( `runtime-dom: events patching` , ( ) => {
6
6
it ( 'should assign event handler' , async ( ) => {
7
7
const el = document . createElement ( 'div' )
8
- const event = new Event ( 'click' )
9
8
const fn = jest . fn ( )
10
9
patchProp ( el , 'onClick' , null , fn )
11
- el . dispatchEvent ( event )
10
+ el . dispatchEvent ( new Event ( 'click' ) )
12
11
await timeout ( )
13
- el . dispatchEvent ( event )
12
+ el . dispatchEvent ( new Event ( 'click' ) )
14
13
await timeout ( )
15
- el . dispatchEvent ( event )
14
+ el . dispatchEvent ( new Event ( 'click' ) )
16
15
await timeout ( )
17
16
expect ( fn ) . toHaveBeenCalledTimes ( 3 )
18
17
} )
19
18
20
19
it ( 'should update event handler' , async ( ) => {
21
20
const el = document . createElement ( 'div' )
22
- const event = new Event ( 'click' )
23
21
const prevFn = jest . fn ( )
24
22
const nextFn = jest . fn ( )
25
23
patchProp ( el , 'onClick' , null , prevFn )
26
- el . dispatchEvent ( event )
24
+ el . dispatchEvent ( new Event ( 'click' ) )
27
25
patchProp ( el , 'onClick' , prevFn , nextFn )
28
26
await timeout ( )
29
- el . dispatchEvent ( event )
27
+ el . dispatchEvent ( new Event ( 'click' ) )
30
28
await timeout ( )
31
- el . dispatchEvent ( event )
29
+ el . dispatchEvent ( new Event ( 'click' ) )
32
30
await timeout ( )
33
31
expect ( prevFn ) . toHaveBeenCalledTimes ( 1 )
34
32
expect ( nextFn ) . toHaveBeenCalledTimes ( 2 )
35
33
} )
36
34
37
35
it ( 'should support multiple event handlers' , async ( ) => {
38
36
const el = document . createElement ( 'div' )
39
- const event = new Event ( 'click' )
40
37
const fn1 = jest . fn ( )
41
38
const fn2 = jest . fn ( )
42
39
patchProp ( el , 'onClick' , null , [ fn1 , fn2 ] )
43
- el . dispatchEvent ( event )
40
+ el . dispatchEvent ( new Event ( 'click' ) )
44
41
await timeout ( )
45
42
expect ( fn1 ) . toHaveBeenCalledTimes ( 1 )
46
43
expect ( fn2 ) . toHaveBeenCalledTimes ( 1 )
47
44
} )
48
45
49
46
it ( 'should unassign event handler' , async ( ) => {
50
47
const el = document . createElement ( 'div' )
51
- const event = new Event ( 'click' )
52
48
const fn = jest . fn ( )
53
49
patchProp ( el , 'onClick' , null , fn )
54
50
patchProp ( el , 'onClick' , fn , null )
55
- el . dispatchEvent ( event )
51
+ el . dispatchEvent ( new Event ( 'click' ) )
56
52
await timeout ( )
57
53
expect ( fn ) . not . toHaveBeenCalled ( )
58
54
} )
59
55
60
56
it ( 'should support event option modifiers' , async ( ) => {
61
57
const el = document . createElement ( 'div' )
62
- const event = new Event ( 'click' )
63
58
const fn = jest . fn ( )
64
59
patchProp ( el , 'onClickOnceCapture' , null , fn )
65
- el . dispatchEvent ( event )
60
+ el . dispatchEvent ( new Event ( 'click' ) )
66
61
await timeout ( )
67
- el . dispatchEvent ( event )
62
+ el . dispatchEvent ( new Event ( 'click' ) )
68
63
await timeout ( )
69
64
expect ( fn ) . toHaveBeenCalledTimes ( 1 )
70
65
} )
71
66
72
67
it ( 'should unassign event handler with options' , async ( ) => {
73
68
const el = document . createElement ( 'div' )
74
- const event = new Event ( 'click' )
75
69
const fn = jest . fn ( )
76
70
patchProp ( el , 'onClickCapture' , null , fn )
77
- el . dispatchEvent ( event )
71
+ el . dispatchEvent ( new Event ( 'click' ) )
78
72
await timeout ( )
79
73
expect ( fn ) . toHaveBeenCalledTimes ( 1 )
80
74
81
75
patchProp ( el , 'onClickCapture' , fn , null )
82
- el . dispatchEvent ( event )
76
+ el . dispatchEvent ( new Event ( 'click' ) )
83
77
await timeout ( )
84
- el . dispatchEvent ( event )
78
+ el . dispatchEvent ( new Event ( 'click' ) )
85
79
await timeout ( )
86
80
expect ( fn ) . toHaveBeenCalledTimes ( 1 )
87
81
} )
88
82
89
83
it ( 'should support native onclick' , async ( ) => {
90
84
const el = document . createElement ( 'div' )
91
- const event = new Event ( 'click' )
92
85
93
86
// string should be set as attribute
94
87
const fn = ( ( window as any ) . __globalSpy = jest . fn ( ) )
95
88
patchProp ( el , 'onclick' , null , '__globalSpy(1)' )
96
- el . dispatchEvent ( event )
89
+ el . dispatchEvent ( new Event ( 'click' ) )
97
90
await timeout ( )
98
91
delete ( window as any ) . __globalSpy
99
92
expect ( fn ) . toHaveBeenCalledWith ( 1 )
100
93
101
94
const fn2 = jest . fn ( )
102
95
patchProp ( el , 'onclick' , '__globalSpy(1)' , fn2 )
96
+ const event = new Event ( 'click' )
103
97
el . dispatchEvent ( event )
104
98
await timeout ( )
105
99
expect ( fn ) . toHaveBeenCalledTimes ( 1 )
@@ -108,13 +102,12 @@ describe(`runtime-dom: events patching`, () => {
108
102
109
103
it ( 'should support stopImmediatePropagation on multiple listeners' , async ( ) => {
110
104
const el = document . createElement ( 'div' )
111
- const event = new Event ( 'click' )
112
105
const fn1 = jest . fn ( ( e : Event ) => {
113
106
e . stopImmediatePropagation ( )
114
107
} )
115
108
const fn2 = jest . fn ( )
116
109
patchProp ( el , 'onClick' , null , [ fn1 , fn2 ] )
117
- el . dispatchEvent ( event )
110
+ el . dispatchEvent ( new Event ( 'click' ) )
118
111
await timeout ( )
119
112
expect ( fn1 ) . toHaveBeenCalledTimes ( 1 )
120
113
expect ( fn2 ) . toHaveBeenCalledTimes ( 0 )
@@ -125,35 +118,55 @@ describe(`runtime-dom: events patching`, () => {
125
118
const el1 = document . createElement ( 'div' )
126
119
const el2 = document . createElement ( 'div' )
127
120
128
- const event = new Event ( 'click' )
121
+ // const event = new Event('click')
129
122
const prevFn = jest . fn ( )
130
123
const nextFn = jest . fn ( )
131
124
132
125
patchProp ( el1 , 'onClick' , null , prevFn )
133
126
patchProp ( el2 , 'onClick' , null , prevFn )
134
127
135
- el1 . dispatchEvent ( event )
136
- el2 . dispatchEvent ( event )
128
+ el1 . dispatchEvent ( new Event ( 'click' ) )
129
+ el2 . dispatchEvent ( new Event ( 'click' ) )
137
130
await timeout ( )
138
131
expect ( prevFn ) . toHaveBeenCalledTimes ( 2 )
139
132
expect ( nextFn ) . toHaveBeenCalledTimes ( 0 )
140
133
141
134
patchProp ( el1 , 'onClick' , prevFn , nextFn )
142
135
patchProp ( el2 , 'onClick' , prevFn , nextFn )
143
136
144
- el1 . dispatchEvent ( event )
145
- el2 . dispatchEvent ( event )
137
+ el1 . dispatchEvent ( new Event ( 'click' ) )
138
+ el2 . dispatchEvent ( new Event ( 'click' ) )
146
139
await timeout ( )
147
140
expect ( prevFn ) . toHaveBeenCalledTimes ( 2 )
148
141
expect ( nextFn ) . toHaveBeenCalledTimes ( 2 )
149
142
150
- el1 . dispatchEvent ( event )
151
- el2 . dispatchEvent ( event )
143
+ el1 . dispatchEvent ( new Event ( 'click' ) )
144
+ el2 . dispatchEvent ( new Event ( 'click' ) )
152
145
await timeout ( )
153
146
expect ( prevFn ) . toHaveBeenCalledTimes ( 2 )
154
147
expect ( nextFn ) . toHaveBeenCalledTimes ( 4 )
155
148
} )
156
149
150
+ // vuejs/vue#6566
151
+ it ( 'should not fire handler attached by the event itself' , async ( ) => {
152
+ const el = document . createElement ( 'div' )
153
+ const child = document . createElement ( 'div' )
154
+ el . appendChild ( child )
155
+ document . body . appendChild ( el )
156
+ const childFn = jest . fn ( )
157
+ const parentFn = jest . fn ( )
158
+
159
+ patchProp ( child , 'onClick' , null , ( ) => {
160
+ childFn ( )
161
+ patchProp ( el , 'onClick' , null , parentFn )
162
+ } )
163
+ child . dispatchEvent ( new Event ( 'click' , { bubbles : true } ) )
164
+
165
+ await timeout ( )
166
+ expect ( childFn ) . toHaveBeenCalled ( )
167
+ expect ( parentFn ) . not . toHaveBeenCalled ( )
168
+ } )
169
+
157
170
// #2841
158
171
test ( 'should patch event correctly in web-components' , async ( ) => {
159
172
class TestElement extends HTMLElement {
0 commit comments