1
- import { fireEvent } from '@testing-library/dom'
1
+ import { createEvent , fireEvent } from '@testing-library/dom'
2
+ import { eventMap } from '@testing-library/dom/dist/event-map'
2
3
import type { pointerState } from '../../pointer/types'
3
4
import type { keyboardState } from '../../keyboard/types'
4
- import {
5
- FakeMouseEvent ,
6
- FakePointerEvent ,
7
- FakePointerEventInit ,
8
- PointerCoords ,
9
- } from './fakeEvent'
10
5
import { getMouseButton , getMouseButtons , MouseButton } from './mouseButtons'
11
6
12
7
export function firePointerEvent (
@@ -32,19 +27,14 @@ export function firePointerEvent(
32
27
clickCount ?: number
33
28
} ,
34
29
) {
35
- const Event =
36
- type === 'click' || type . startsWith ( 'mouse' )
37
- ? FakeMouseEvent
38
- : FakePointerEvent
39
-
40
- const init : FakePointerEventInit = {
30
+ const init : MouseEventInit & PointerEventInit = {
41
31
...coords ,
42
32
altKey : keyboardState . modifiers . alt ,
43
33
ctrlKey : keyboardState . modifiers . ctrl ,
44
34
metaKey : keyboardState . modifiers . meta ,
45
35
shiftKey : keyboardState . modifiers . shift ,
46
36
}
47
- if ( Event === FakePointerEvent || type === 'click' ) {
37
+ if ( type === 'click' || type . startsWith ( 'pointer' ) ) {
48
38
init . pointerId = pointerId
49
39
init . pointerType = pointerType
50
40
}
@@ -61,9 +51,80 @@ export function firePointerEvent(
61
51
. map ( p => p . keyDef . button ?? 0 ) ,
62
52
)
63
53
}
64
- if ( [ 'mousedown' , 'mouseup' , 'click' ] . includes ( type ) ) {
54
+ if ( [ 'mousedown' , 'mouseup' , 'click' , 'dblclick' ] . includes ( type ) ) {
65
55
init . detail = clickCount
66
56
}
67
57
68
- return fireEvent ( target , new Event ( type , init ) )
58
+ const eventKey = Object . keys ( eventMap ) . find ( k => k . toLowerCase ( ) === type )
59
+ const event = createEvent [ eventKey as keyof typeof createEvent ] ( target , init )
60
+
61
+ // see https://github.com/testing-library/react-testing-library/issues/268
62
+ assignPositionInit ( event as MouseEvent , init )
63
+ assignPointerInit ( event as PointerEvent , init )
64
+
65
+ return fireEvent ( target , event )
66
+ }
67
+
68
+ export interface PointerCoords {
69
+ x ?: number
70
+ y ?: number
71
+ clientX ?: number
72
+ clientY ?: number
73
+ offsetX ?: number
74
+ offsetY ?: number
75
+ pageX ?: number
76
+ pageY ?: number
77
+ screenX ?: number
78
+ screenY ?: number
79
+ }
80
+
81
+ function assignProps (
82
+ obj : MouseEvent | PointerEvent ,
83
+ props : MouseEventInit & PointerEventInit & PointerCoords ,
84
+ ) {
85
+ for ( const [ key , value ] of Object . entries ( props ) ) {
86
+ Object . defineProperty ( obj , key , { get : ( ) => value } )
87
+ }
88
+ }
89
+
90
+ function assignPositionInit (
91
+ obj : MouseEvent | PointerEvent ,
92
+ {
93
+ x,
94
+ y,
95
+ clientX,
96
+ clientY,
97
+ offsetX,
98
+ offsetY,
99
+ pageX,
100
+ pageY,
101
+ screenX,
102
+ screenY,
103
+ } : PointerCoords ,
104
+ ) {
105
+ assignProps ( obj , {
106
+ /* istanbul ignore start */
107
+ x : x ?? clientX ?? 0 ,
108
+ y : y ?? clientY ?? 0 ,
109
+ clientX : x ?? clientX ?? 0 ,
110
+ clientY : y ?? clientY ?? 0 ,
111
+ offsetX : offsetX ?? 0 ,
112
+ offsetY : offsetY ?? 0 ,
113
+ pageX : pageX ?? 0 ,
114
+ pageY : pageY ?? 0 ,
115
+ screenX : screenX ?? 0 ,
116
+ screenY : screenY ?? 0 ,
117
+ /* istanbul ignore end */
118
+ } )
119
+ }
120
+
121
+ function assignPointerInit (
122
+ obj : MouseEvent | PointerEvent ,
123
+ { isPrimary, pointerId, pointerType} : PointerEventInit ,
124
+ ) {
125
+ assignProps ( obj , {
126
+ isPrimary,
127
+ pointerId,
128
+ pointerType,
129
+ } )
69
130
}
0 commit comments