1
- // @ts -nocheck
2
1
import $ from 'jquery'
3
2
import _ from 'lodash'
4
3
import $dom from '../dom'
5
4
import $elements from '../dom/elements'
6
- import $Keyboard from './keyboard'
5
+ import $Keyboard , { ModifiersEventOptions } from './keyboard'
7
6
import $selection from '../dom/selection'
8
7
import debugFn from 'debug'
9
8
@@ -16,7 +15,7 @@ const debug = debugFn('cypress:driver:mouse')
16
15
* @property {Document } doc
17
16
*/
18
17
19
- const getLastHoveredEl = ( state ) => {
18
+ const getLastHoveredEl = ( state ) : HTMLElement | null => {
20
19
let lastHoveredEl = state ( 'mouseLastHoveredEl' )
21
20
const lastHoveredElAttached = lastHoveredEl && $elements . isAttachedEl ( lastHoveredEl )
22
21
@@ -42,6 +41,12 @@ const getMouseCoords = (state) => {
42
41
return state ( 'mouseCoords' )
43
42
}
44
43
44
+ type DefaultMouseOptions = ModifiersEventOptions & CoordsEventOptions & {
45
+ view : Window
46
+ composed : boolean
47
+ relatedTarget : HTMLElement | null
48
+ }
49
+
45
50
export const create = ( state , keyboard , focused , Cypress ) => {
46
51
const isFirefox = Cypress . browser . family === 'firefox'
47
52
@@ -65,7 +70,7 @@ export const create = (state, keyboard, focused, Cypress) => {
65
70
66
71
return sendPointerEvent ( el , evtOptions , 'pointerup' , true , true )
67
72
}
68
- const sendPointerdown = ( el , evtOptions ) => {
73
+ const sendPointerdown = ( el , evtOptions ) : { } | SentEvent => {
69
74
if ( isFirefox && el . disabled ) {
70
75
return { }
71
76
}
@@ -75,7 +80,7 @@ export const create = (state, keyboard, focused, Cypress) => {
75
80
const sendPointermove = ( el , evtOptions ) => {
76
81
return sendPointerEvent ( el , evtOptions , 'pointermove' , true , true )
77
82
}
78
- const sendPointerover = ( el , evtOptions ) => {
83
+ const sendPointerover = ( el , evtOptions : DefaultMouseOptions ) => {
79
84
return sendPointerEvent ( el , evtOptions , 'pointerover' , true , true )
80
85
}
81
86
const sendPointerenter = ( el , evtOptions ) => {
@@ -95,7 +100,7 @@ export const create = (state, keyboard, focused, Cypress) => {
95
100
96
101
return sendMouseEvent ( el , evtOptions , 'mouseup' , true , true )
97
102
}
98
- const sendMousedown = ( el , evtOptions ) => {
103
+ const sendMousedown = ( el , evtOptions ) : { } | SentEvent => {
99
104
if ( isFirefox && el . disabled ) {
100
105
return { }
101
106
}
@@ -105,7 +110,7 @@ export const create = (state, keyboard, focused, Cypress) => {
105
110
const sendMousemove = ( el , evtOptions ) => {
106
111
return sendMouseEvent ( el , evtOptions , 'mousemove' , true , true )
107
112
}
108
- const sendMouseover = ( el , evtOptions ) => {
113
+ const sendMouseover = ( el , evtOptions : DefaultMouseOptions ) => {
109
114
return sendMouseEvent ( el , evtOptions , 'mouseover' , true , true )
110
115
}
111
116
const sendMouseenter = ( el , evtOptions ) => {
@@ -117,7 +122,7 @@ export const create = (state, keyboard, focused, Cypress) => {
117
122
const sendMouseout = ( el , evtOptions ) => {
118
123
return sendMouseEvent ( el , evtOptions , 'mouseout' , true , true )
119
124
}
120
- const sendClick = ( el , evtOptions , opts = { } ) => {
125
+ const sendClick = ( el , evtOptions , opts : { force ?: boolean } = { } ) => {
121
126
// send the click event if firefox and force (needed for force check checkbox)
122
127
if ( ! opts . force && isFirefox && el . disabled ) {
123
128
return { }
@@ -154,7 +159,7 @@ export const create = (state, keyboard, focused, Cypress) => {
154
159
return ! _ . isEqual ( xy ( fromElViewport ) , xy ( coords ) )
155
160
}
156
161
157
- const shouldMoveCursorToEndAfterMousedown = ( el ) => {
162
+ const shouldMoveCursorToEndAfterMousedown = ( el : HTMLElement ) => {
158
163
const _debug = debug . extend ( ':shouldMoveCursorToEnd' )
159
164
160
165
_debug ( 'shouldMoveToEnd?' , el )
@@ -182,7 +187,7 @@ export const create = (state, keyboard, focused, Cypress) => {
182
187
}
183
188
184
189
const mouse = {
185
- _getDefaultMouseOptions ( x , y , win ) {
190
+ _getDefaultMouseOptions ( x , y , win ) : DefaultMouseOptions {
186
191
const _activeModifiers = keyboard . getActiveModifiers ( )
187
192
const modifiersEventOptions = $Keyboard . toModifiersEventOptions ( _activeModifiers )
188
193
const coordsEventOptions = toCoordsEventOptions ( x , y , win )
@@ -201,7 +206,7 @@ export const create = (state, keyboard, focused, Cypress) => {
201
206
* @param {Coords } coords
202
207
* @param {HTMLElement } forceEl
203
208
*/
204
- move ( fromElViewport , forceEl ) {
209
+ move ( fromElViewport , forceEl ? ) {
205
210
debug ( 'mouse.move' , fromElViewport )
206
211
207
212
const lastHoveredEl = getLastHoveredEl ( state )
@@ -259,16 +264,21 @@ export const create = (state, keyboard, focused, Cypress) => {
259
264
skipped : formatReasonNotFired ( 'Already on Coordinates' ) ,
260
265
}
261
266
}
267
+
268
+ type EventFunc =
269
+ | ( ( ) => { skipped : string } )
270
+ | ( ( ) => SentEvent )
271
+
262
272
let pointerout = _ . noop
263
273
let pointerleave = _ . noop
264
- let pointerover = notFired
274
+ let pointerover : EventFunc = notFired
265
275
let pointerenter = _ . noop
266
276
let mouseout = _ . noop
267
277
let mouseleave = _ . noop
268
- let mouseover = notFired
278
+ let mouseover : EventFunc = notFired
269
279
let mouseenter = _ . noop
270
- let pointermove = notFired
271
- let mousemove = notFired
280
+ let pointermove : EventFunc = notFired
281
+ let mousemove : EventFunc = notFired
272
282
273
283
const lastHoveredEl = getLastHoveredEl ( state )
274
284
@@ -285,9 +295,9 @@ export const create = (state, keyboard, focused, Cypress) => {
285
295
sendMouseout ( lastHoveredEl , _ . extend ( { } , defaultMouseOptions , { relatedTarget : el } ) )
286
296
}
287
297
288
- let curParent = lastHoveredEl
298
+ let curParent : Node | null = lastHoveredEl
289
299
290
- const elsToSendMouseleave = [ ]
300
+ const elsToSendMouseleave : Node [ ] = [ ]
291
301
292
302
while ( curParent && curParent . ownerDocument && curParent !== commonAncestor ) {
293
303
elsToSendMouseleave . push ( curParent )
@@ -317,8 +327,8 @@ export const create = (state, keyboard, focused, Cypress) => {
317
327
return sendPointerover ( el , _ . extend ( { } , defaultPointerOptions , { relatedTarget : lastHoveredEl } ) )
318
328
}
319
329
320
- let curParent = el
321
- const elsToSendMouseenter = [ ]
330
+ let curParent : Node | null = el
331
+ const elsToSendMouseenter : Node [ ] = [ ]
322
332
323
333
while ( curParent && curParent . ownerDocument && curParent !== commonAncestor ) {
324
334
elsToSendMouseenter . push ( curParent )
@@ -349,7 +359,8 @@ export const create = (state, keyboard, focused, Cypress) => {
349
359
return sendMousemove ( el , defaultMouseOptions )
350
360
}
351
361
352
- const events = [ ]
362
+ // TODO: make `type` below more specific.
363
+ const events : Array < ReturnType < EventFunc > & { type : string } > = [ ]
353
364
354
365
pointerout ( )
355
366
pointerleave ( )
@@ -419,7 +430,7 @@ export const create = (state, keyboard, focused, Cypress) => {
419
430
let pointerdown = sendPointerdown (
420
431
el ,
421
432
pointerEvtOptions ,
422
- )
433
+ ) as Partial < SentEvent >
423
434
424
435
const pointerdownPrevented = pointerdown . preventedDefault
425
436
const elIsDetached = $elements . isDetachedEl ( el )
@@ -461,7 +472,7 @@ export const create = (state, keyboard, focused, Cypress) => {
461
472
// el we just send pointerdown
462
473
const el = mouseDownPhase . targetEl
463
474
464
- if ( mouseDownPhase . events . pointerdown . preventedDefault || mouseDownPhase . events . mousedown . preventedDefault || ! $elements . isAttachedEl ( el ) ) {
475
+ if ( mouseDownPhase . events . pointerdown . preventedDefault || ( mouseDownPhase . events . mousedown as Partial < SentEvent > ) . preventedDefault || ! $elements . isAttachedEl ( el ) ) {
465
476
return mouseDownPhase
466
477
}
467
478
@@ -488,6 +499,8 @@ export const create = (state, keyboard, focused, Cypress) => {
488
499
489
500
if ( shouldMoveCursorToEndAfterMousedown ( el ) ) {
490
501
debug ( 'moveSelectionToEnd due to click' , el )
502
+ // It's a curried function, so the 2 arguments are valid.
503
+ // @ts -ignore
491
504
$selection . moveSelectionToEnd ( el , { onlyIfEmptySelection : true } )
492
505
}
493
506
@@ -532,7 +545,7 @@ export const create = (state, keyboard, focused, Cypress) => {
532
545
533
546
const mouseDownPhase = mouse . down ( fromElViewport , forceEl , pointerEvtOptionsExtend , mouseEvtOptionsExtend )
534
547
535
- const skipMouseupEvent = mouseDownPhase . events . pointerdown . skipped || mouseDownPhase . events . pointerdown . preventedDefault
548
+ const skipMouseupEvent = mouseDownPhase . events . pointerdown . preventedDefault
536
549
537
550
const mouseUpPhase = mouse . up ( fromElViewport , forceEl , skipMouseupEvent , pointerEvtOptionsExtend , mouseEvtOptionsExtend )
538
551
@@ -638,7 +651,7 @@ export const create = (state, keyboard, focused, Cypress) => {
638
651
return { click }
639
652
} ,
640
653
641
- _contextmenuEvent ( fromElViewport , forceEl , mouseEvtOptionsExtend ) {
654
+ _contextmenuEvent ( fromElViewport , forceEl , mouseEvtOptionsExtend ? ) {
642
655
const el = forceEl || mouse . moveToCoords ( fromElViewport )
643
656
644
657
const win = $dom . getWindowByElement ( el )
@@ -695,7 +708,7 @@ export const create = (state, keyboard, focused, Cypress) => {
695
708
696
709
const contextmenuEvent = mouse . _contextmenuEvent ( fromElViewport , forceEl )
697
710
698
- const skipMouseupEvent = mouseDownPhase . events . pointerdown . skipped || mouseDownPhase . events . pointerdown . preventedDefault
711
+ const skipMouseupEvent = mouseDownPhase . events . pointerdown . preventedDefault
699
712
const mouseUpPhase = mouse . up ( fromElViewport , forceEl , skipMouseupEvent , pointerEvtOptionsExtend , mouseEvtOptionsExtend )
700
713
701
714
const clickEvents = _ . extend ( { } , mouseDownPhase . events , mouseUpPhase . events )
@@ -709,7 +722,14 @@ export const create = (state, keyboard, focused, Cypress) => {
709
722
710
723
const { stopPropagation } = window . MouseEvent . prototype
711
724
712
- const sendEvent = ( evtName , el , evtOptions , bubbles = false , cancelable = false , Constructor , composed = false ) => {
725
+ type SentEvent = {
726
+ stoppedPropagation : boolean
727
+ preventedDefault : boolean
728
+ el : HTMLElement
729
+ modifiers : string
730
+ }
731
+
732
+ const sendEvent = ( evtName , el , evtOptions , bubbles = false , cancelable = false , Constructor , composed = false ) : SentEvent => {
713
733
evtOptions = _ . extend ( { } , evtOptions , { bubbles, cancelable } )
714
734
const _eventModifiers = $Keyboard . fromModifierEventOptions ( evtOptions )
715
735
const modifiers = $Keyboard . modifiersToString ( _eventModifiers )
@@ -720,6 +740,9 @@ const sendEvent = (evtName, el, evtOptions, bubbles = false, cancelable = false,
720
740
evt . stopPropagation = function ( ...args ) {
721
741
evt . _hasStoppedPropagation = true
722
742
743
+ // stopPropagation doesn't have argument. So, we cannot type-safely pass the second argument.
744
+ // But we're passing it just in case.
745
+ // @ts -ignore
723
746
return stopPropagation . apply ( this , ...args )
724
747
}
725
748
}
@@ -740,6 +763,19 @@ const formatReasonNotFired = (reason) => {
740
763
return `⚠️ not fired (${ reason } )`
741
764
}
742
765
766
+ type CoordsEventOptions = {
767
+ x : number
768
+ y : number
769
+ clientX : number
770
+ clientY : number
771
+ screenX : number
772
+ screenY : number
773
+ pageX : number
774
+ pageY : number
775
+ layerX : number
776
+ layerY : number
777
+ }
778
+
743
779
const toCoordsEventOptions = ( x , y , win ) => {
744
780
// these are the coords from the element's window,
745
781
// ignoring scroll position
0 commit comments