1
- import { patchEvent } from '../../src/modules/events '
1
+ import { patchProp } from '../../src/patchProp '
2
2
3
3
const timeout = ( ) => new Promise ( r => setTimeout ( r ) )
4
4
@@ -7,7 +7,7 @@ describe(`events`, () => {
7
7
const el = document . createElement ( 'div' )
8
8
const event = new Event ( 'click' )
9
9
const fn = jest . fn ( )
10
- patchEvent ( el , 'onClick' , null , fn , null )
10
+ patchProp ( el , 'onClick' , null , fn )
11
11
el . dispatchEvent ( event )
12
12
await timeout ( )
13
13
el . dispatchEvent ( event )
@@ -22,9 +22,9 @@ describe(`events`, () => {
22
22
const event = new Event ( 'click' )
23
23
const prevFn = jest . fn ( )
24
24
const nextFn = jest . fn ( )
25
- patchEvent ( el , 'onClick' , null , prevFn , null )
25
+ patchProp ( el , 'onClick' , null , prevFn )
26
26
el . dispatchEvent ( event )
27
- patchEvent ( el , 'onClick' , prevFn , nextFn , null )
27
+ patchProp ( el , 'onClick' , prevFn , nextFn )
28
28
await timeout ( )
29
29
el . dispatchEvent ( event )
30
30
await timeout ( )
@@ -39,7 +39,7 @@ describe(`events`, () => {
39
39
const event = new Event ( 'click' )
40
40
const fn1 = jest . fn ( )
41
41
const fn2 = jest . fn ( )
42
- patchEvent ( el , 'onClick' , null , [ fn1 , fn2 ] , null )
42
+ patchProp ( el , 'onClick' , null , [ fn1 , fn2 ] )
43
43
el . dispatchEvent ( event )
44
44
await timeout ( )
45
45
expect ( fn1 ) . toHaveBeenCalledTimes ( 1 )
@@ -50,8 +50,8 @@ describe(`events`, () => {
50
50
const el = document . createElement ( 'div' )
51
51
const event = new Event ( 'click' )
52
52
const fn = jest . fn ( )
53
- patchEvent ( el , 'onClick' , null , fn , null )
54
- patchEvent ( el , 'onClick' , fn , null , null )
53
+ patchProp ( el , 'onClick' , null , fn )
54
+ patchProp ( el , 'onClick' , fn , null )
55
55
el . dispatchEvent ( event )
56
56
await timeout ( )
57
57
expect ( fn ) . not . toHaveBeenCalled ( )
@@ -67,7 +67,7 @@ describe(`events`, () => {
67
67
once : true
68
68
}
69
69
}
70
- patchEvent ( el , 'onClick' , null , nextValue , null )
70
+ patchProp ( el , 'onClick' , null , nextValue )
71
71
el . dispatchEvent ( event )
72
72
await timeout ( )
73
73
el . dispatchEvent ( event )
@@ -86,8 +86,8 @@ describe(`events`, () => {
86
86
once : true
87
87
}
88
88
}
89
- patchEvent ( el , 'onClick' , null , prevFn , null )
90
- patchEvent ( el , 'onClick' , prevFn , nextValue , null )
89
+ patchProp ( el , 'onClick' , null , prevFn )
90
+ patchProp ( el , 'onClick' , prevFn , nextValue )
91
91
el . dispatchEvent ( event )
92
92
await timeout ( )
93
93
el . dispatchEvent ( event )
@@ -106,30 +106,32 @@ describe(`events`, () => {
106
106
once : true
107
107
}
108
108
}
109
- patchEvent ( el , 'onClick' , null , nextValue , null )
110
- patchEvent ( el , 'onClick' , nextValue , null , null )
109
+ patchProp ( el , 'onClick' , null , nextValue )
110
+ patchProp ( el , 'onClick' , nextValue , null )
111
111
el . dispatchEvent ( event )
112
112
await timeout ( )
113
113
el . dispatchEvent ( event )
114
114
await timeout ( )
115
115
expect ( fn ) . not . toHaveBeenCalled ( )
116
116
} )
117
117
118
- it ( 'should assign native onclick attribute ' , async ( ) => {
118
+ it ( 'should support native onclick' , async ( ) => {
119
119
const el = document . createElement ( 'div' )
120
120
const event = new Event ( 'click' )
121
- const fn = ( ( window as any ) . _nativeClickSpy = jest . fn ( ) )
122
121
123
- patchEvent ( el , 'onclick' , null , '_nativeClickSpy()' as any )
122
+ // string should be set as attribute
123
+ const fn = ( ( window as any ) . __globalSpy = jest . fn ( ) )
124
+ patchProp ( el , 'onclick' , null , '__globalSpy(1)' )
124
125
el . dispatchEvent ( event )
125
126
await timeout ( )
126
- expect ( fn ) . toHaveBeenCalledTimes ( 1 )
127
+ delete ( window as any ) . __globalSpy
128
+ expect ( fn ) . toHaveBeenCalledWith ( 1 )
127
129
128
130
const fn2 = jest . fn ( )
129
- patchEvent ( el , 'onclick' , null , fn2 )
131
+ patchProp ( el , 'onclick' , '__globalSpy(1)' , fn2 )
130
132
el . dispatchEvent ( event )
131
133
await timeout ( )
132
134
expect ( fn ) . toHaveBeenCalledTimes ( 1 )
133
- expect ( fn2 ) . toHaveBeenCalledTimes ( 1 )
135
+ expect ( fn2 ) . toHaveBeenCalledWith ( event )
134
136
} )
135
137
} )
0 commit comments