@@ -78,24 +78,12 @@ export class UnitTestElement implements TestElement {
78
78
}
79
79
80
80
async click ( ...args : [ ] | [ 'center' ] | [ number , number ] ) : Promise < void > {
81
- let clientX : number | undefined = undefined ;
82
- let clientY : number | undefined = undefined ;
83
- if ( args . length ) {
84
- const { left, top, width, height} = await this . getDimensions ( ) ;
85
- const relativeX = args [ 0 ] === 'center' ? width / 2 : args [ 0 ] ;
86
- const relativeY = args [ 0 ] === 'center' ? height / 2 : args [ 1 ] ;
87
-
88
- // Round the computed click position as decimal pixels are not
89
- // supported by mouse events and could lead to unexpected results.
90
- clientX = Math . round ( left + relativeX ) ;
91
- clientY = Math . round ( top + relativeY ) ;
92
- }
81
+ await this . _dispatchMouseEventSequence ( 'click' , args ) ;
82
+ await this . _stabilize ( ) ;
83
+ }
93
84
94
- this . _dispatchPointerEventIfSupported ( 'pointerdown' , clientX , clientY ) ;
95
- dispatchMouseEvent ( this . element , 'mousedown' , clientX , clientY ) ;
96
- this . _dispatchPointerEventIfSupported ( 'pointerup' , clientX , clientY ) ;
97
- dispatchMouseEvent ( this . element , 'mouseup' , clientX , clientY ) ;
98
- dispatchMouseEvent ( this . element , 'click' , clientX , clientY ) ;
85
+ async rightClick ( ...args : [ ] | [ 'center' ] | [ number , number ] ) : Promise < void > {
86
+ await this . _dispatchMouseEventSequence ( 'contextmenu' , args , 2 ) ;
99
87
await this . _stabilize ( ) ;
100
88
}
101
89
@@ -210,14 +198,43 @@ export class UnitTestElement implements TestElement {
210
198
* @param name Name of the pointer event to be dispatched.
211
199
* @param clientX Coordinate of the user's pointer along the X axis.
212
200
* @param clientY Coordinate of the user's pointer along the Y axis.
201
+ * @param button Mouse button that should be pressed when dispatching the event.
213
202
*/
214
- private _dispatchPointerEventIfSupported ( name : string , clientX ?: number , clientY ?: number ) {
203
+ private _dispatchPointerEventIfSupported (
204
+ name : string , clientX ?: number , clientY ?: number , button ?: number ) {
215
205
// The latest versions of all browsers we support have the new `PointerEvent` API.
216
206
// Though since we capture the two most recent versions of these browsers, we also
217
207
// need to support Safari 12 at time of writing. Safari 12 does not have support for this,
218
208
// so we need to conditionally create and dispatch these events based on feature detection.
219
209
if ( typeof PointerEvent !== 'undefined' && PointerEvent ) {
220
- dispatchPointerEvent ( this . element , name , clientX , clientY ) ;
210
+ dispatchPointerEvent ( this . element , name , clientX , clientY , { isPrimary : true , button } ) ;
221
211
}
222
212
}
213
+
214
+ /** Dispatches all the events that are part of a mouse event sequence. */
215
+ private async _dispatchMouseEventSequence (
216
+ name : string ,
217
+ args : [ ] | [ 'center' ] | [ number , number ] ,
218
+ button ?: number ) {
219
+ let clientX : number | undefined = undefined ;
220
+ let clientY : number | undefined = undefined ;
221
+
222
+ if ( args . length ) {
223
+ const { left, top, width, height} = await this . getDimensions ( ) ;
224
+ const relativeX = args [ 0 ] === 'center' ? width / 2 : args [ 0 ] ;
225
+ const relativeY = args [ 0 ] === 'center' ? height / 2 : args [ 1 ] ;
226
+
227
+ // Round the computed click position as decimal pixels are not
228
+ // supported by mouse events and could lead to unexpected results.
229
+ clientX = Math . round ( left + relativeX ) ;
230
+ clientY = Math . round ( top + relativeY ) ;
231
+ }
232
+
233
+ this . _dispatchPointerEventIfSupported ( 'pointerdown' , clientX , clientY , button ) ;
234
+ dispatchMouseEvent ( this . element , 'mousedown' , clientX , clientY , button ) ;
235
+ this . _dispatchPointerEventIfSupported ( 'pointerup' , clientX , clientY , button ) ;
236
+ dispatchMouseEvent ( this . element , 'mouseup' , clientX , clientY , button ) ;
237
+ dispatchMouseEvent ( this . element , name , clientX , clientY , button ) ;
238
+ await this . _stabilize ( ) ;
239
+ }
223
240
}
0 commit comments