@@ -5,101 +5,95 @@ const timeout = () => new Promise(r => setTimeout(r))
55describe ( `runtime-dom: events patching` , ( ) => {
66 it ( 'should assign event handler' , async ( ) => {
77 const el = document . createElement ( 'div' )
8- const event = new Event ( 'click' )
98 const fn = jest . fn ( )
109 patchProp ( el , 'onClick' , null , fn )
11- el . dispatchEvent ( event )
10+ el . dispatchEvent ( new Event ( 'click' ) )
1211 await timeout ( )
13- el . dispatchEvent ( event )
12+ el . dispatchEvent ( new Event ( 'click' ) )
1413 await timeout ( )
15- el . dispatchEvent ( event )
14+ el . dispatchEvent ( new Event ( 'click' ) )
1615 await timeout ( )
1716 expect ( fn ) . toHaveBeenCalledTimes ( 3 )
1817 } )
1918
2019 it ( 'should update event handler' , async ( ) => {
2120 const el = document . createElement ( 'div' )
22- const event = new Event ( 'click' )
2321 const prevFn = jest . fn ( )
2422 const nextFn = jest . fn ( )
2523 patchProp ( el , 'onClick' , null , prevFn )
26- el . dispatchEvent ( event )
24+ el . dispatchEvent ( new Event ( 'click' ) )
2725 patchProp ( el , 'onClick' , prevFn , nextFn )
2826 await timeout ( )
29- el . dispatchEvent ( event )
27+ el . dispatchEvent ( new Event ( 'click' ) )
3028 await timeout ( )
31- el . dispatchEvent ( event )
29+ el . dispatchEvent ( new Event ( 'click' ) )
3230 await timeout ( )
3331 expect ( prevFn ) . toHaveBeenCalledTimes ( 1 )
3432 expect ( nextFn ) . toHaveBeenCalledTimes ( 2 )
3533 } )
3634
3735 it ( 'should support multiple event handlers' , async ( ) => {
3836 const el = document . createElement ( 'div' )
39- const event = new Event ( 'click' )
4037 const fn1 = jest . fn ( )
4138 const fn2 = jest . fn ( )
4239 patchProp ( el , 'onClick' , null , [ fn1 , fn2 ] )
43- el . dispatchEvent ( event )
40+ el . dispatchEvent ( new Event ( 'click' ) )
4441 await timeout ( )
4542 expect ( fn1 ) . toHaveBeenCalledTimes ( 1 )
4643 expect ( fn2 ) . toHaveBeenCalledTimes ( 1 )
4744 } )
4845
4946 it ( 'should unassign event handler' , async ( ) => {
5047 const el = document . createElement ( 'div' )
51- const event = new Event ( 'click' )
5248 const fn = jest . fn ( )
5349 patchProp ( el , 'onClick' , null , fn )
5450 patchProp ( el , 'onClick' , fn , null )
55- el . dispatchEvent ( event )
51+ el . dispatchEvent ( new Event ( 'click' ) )
5652 await timeout ( )
5753 expect ( fn ) . not . toHaveBeenCalled ( )
5854 } )
5955
6056 it ( 'should support event option modifiers' , async ( ) => {
6157 const el = document . createElement ( 'div' )
62- const event = new Event ( 'click' )
6358 const fn = jest . fn ( )
6459 patchProp ( el , 'onClickOnceCapture' , null , fn )
65- el . dispatchEvent ( event )
60+ el . dispatchEvent ( new Event ( 'click' ) )
6661 await timeout ( )
67- el . dispatchEvent ( event )
62+ el . dispatchEvent ( new Event ( 'click' ) )
6863 await timeout ( )
6964 expect ( fn ) . toHaveBeenCalledTimes ( 1 )
7065 } )
7166
7267 it ( 'should unassign event handler with options' , async ( ) => {
7368 const el = document . createElement ( 'div' )
74- const event = new Event ( 'click' )
7569 const fn = jest . fn ( )
7670 patchProp ( el , 'onClickCapture' , null , fn )
77- el . dispatchEvent ( event )
71+ el . dispatchEvent ( new Event ( 'click' ) )
7872 await timeout ( )
7973 expect ( fn ) . toHaveBeenCalledTimes ( 1 )
8074
8175 patchProp ( el , 'onClickCapture' , fn , null )
82- el . dispatchEvent ( event )
76+ el . dispatchEvent ( new Event ( 'click' ) )
8377 await timeout ( )
84- el . dispatchEvent ( event )
78+ el . dispatchEvent ( new Event ( 'click' ) )
8579 await timeout ( )
8680 expect ( fn ) . toHaveBeenCalledTimes ( 1 )
8781 } )
8882
8983 it ( 'should support native onclick' , async ( ) => {
9084 const el = document . createElement ( 'div' )
91- const event = new Event ( 'click' )
9285
9386 // string should be set as attribute
9487 const fn = ( ( window as any ) . __globalSpy = jest . fn ( ) )
9588 patchProp ( el , 'onclick' , null , '__globalSpy(1)' )
96- el . dispatchEvent ( event )
89+ el . dispatchEvent ( new Event ( 'click' ) )
9790 await timeout ( )
9891 delete ( window as any ) . __globalSpy
9992 expect ( fn ) . toHaveBeenCalledWith ( 1 )
10093
10194 const fn2 = jest . fn ( )
10295 patchProp ( el , 'onclick' , '__globalSpy(1)' , fn2 )
96+ const event = new Event ( 'click' )
10397 el . dispatchEvent ( event )
10498 await timeout ( )
10599 expect ( fn ) . toHaveBeenCalledTimes ( 1 )
@@ -108,13 +102,12 @@ describe(`runtime-dom: events patching`, () => {
108102
109103 it ( 'should support stopImmediatePropagation on multiple listeners' , async ( ) => {
110104 const el = document . createElement ( 'div' )
111- const event = new Event ( 'click' )
112105 const fn1 = jest . fn ( ( e : Event ) => {
113106 e . stopImmediatePropagation ( )
114107 } )
115108 const fn2 = jest . fn ( )
116109 patchProp ( el , 'onClick' , null , [ fn1 , fn2 ] )
117- el . dispatchEvent ( event )
110+ el . dispatchEvent ( new Event ( 'click' ) )
118111 await timeout ( )
119112 expect ( fn1 ) . toHaveBeenCalledTimes ( 1 )
120113 expect ( fn2 ) . toHaveBeenCalledTimes ( 0 )
@@ -125,35 +118,55 @@ describe(`runtime-dom: events patching`, () => {
125118 const el1 = document . createElement ( 'div' )
126119 const el2 = document . createElement ( 'div' )
127120
128- const event = new Event ( 'click' )
121+ // const event = new Event('click')
129122 const prevFn = jest . fn ( )
130123 const nextFn = jest . fn ( )
131124
132125 patchProp ( el1 , 'onClick' , null , prevFn )
133126 patchProp ( el2 , 'onClick' , null , prevFn )
134127
135- el1 . dispatchEvent ( event )
136- el2 . dispatchEvent ( event )
128+ el1 . dispatchEvent ( new Event ( 'click' ) )
129+ el2 . dispatchEvent ( new Event ( 'click' ) )
137130 await timeout ( )
138131 expect ( prevFn ) . toHaveBeenCalledTimes ( 2 )
139132 expect ( nextFn ) . toHaveBeenCalledTimes ( 0 )
140133
141134 patchProp ( el1 , 'onClick' , prevFn , nextFn )
142135 patchProp ( el2 , 'onClick' , prevFn , nextFn )
143136
144- el1 . dispatchEvent ( event )
145- el2 . dispatchEvent ( event )
137+ el1 . dispatchEvent ( new Event ( 'click' ) )
138+ el2 . dispatchEvent ( new Event ( 'click' ) )
146139 await timeout ( )
147140 expect ( prevFn ) . toHaveBeenCalledTimes ( 2 )
148141 expect ( nextFn ) . toHaveBeenCalledTimes ( 2 )
149142
150- el1 . dispatchEvent ( event )
151- el2 . dispatchEvent ( event )
143+ el1 . dispatchEvent ( new Event ( 'click' ) )
144+ el2 . dispatchEvent ( new Event ( 'click' ) )
152145 await timeout ( )
153146 expect ( prevFn ) . toHaveBeenCalledTimes ( 2 )
154147 expect ( nextFn ) . toHaveBeenCalledTimes ( 4 )
155148 } )
156149
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+
157170 // #2841
158171 test ( 'should patch event correctly in web-components' , async ( ) => {
159172 class TestElement extends HTMLElement {
0 commit comments