@@ -78,24 +78,12 @@ export class UnitTestElement implements TestElement {
7878 }
7979
8080 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+ }
9384
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 ) ;
9987 await this . _stabilize ( ) ;
10088 }
10189
@@ -210,14 +198,43 @@ export class UnitTestElement implements TestElement {
210198 * @param name Name of the pointer event to be dispatched.
211199 * @param clientX Coordinate of the user's pointer along the X axis.
212200 * @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.
213202 */
214- private _dispatchPointerEventIfSupported ( name : string , clientX ?: number , clientY ?: number ) {
203+ private _dispatchPointerEventIfSupported (
204+ name : string , clientX ?: number , clientY ?: number , button ?: number ) {
215205 // The latest versions of all browsers we support have the new `PointerEvent` API.
216206 // Though since we capture the two most recent versions of these browsers, we also
217207 // need to support Safari 12 at time of writing. Safari 12 does not have support for this,
218208 // so we need to conditionally create and dispatch these events based on feature detection.
219209 if ( typeof PointerEvent !== 'undefined' && PointerEvent ) {
220- dispatchPointerEvent ( this . element , name , clientX , clientY ) ;
210+ dispatchPointerEvent ( this . element , name , clientX , clientY , { isPrimary : true , button } ) ;
221211 }
222212 }
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+ }
223240}
0 commit comments