Skip to content

Commit

Permalink
fix(cdk/testing): emit pointer events on hover and mouseAway (#20098)
Browse files Browse the repository at this point in the history
In 9cd1891 pointer event simulation was added to `UnitTestElement.click` to closer simulate the events that happen inside the browser. These changes expand the coverage to the `hover` and `mouseAway` methods since they can dispatch pointer events too.
  • Loading branch information
crisbeto committed Jul 28, 2020
1 parent 04b7523 commit a5274a9
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/cdk/testing/testbed/unit-test-element.ts
Expand Up @@ -81,19 +81,9 @@ export class UnitTestElement implements TestElement {
const clientX = Math.round(left + relativeX);
const clientY = Math.round(top + relativeY);

// The latest versions of all browsers we support have the new `PointerEvent` API.
// Though since we capture the two most recent versions of these browsers, we also
// need to support Safari 12 at time of writing. Safari 12 does not have support for this,
// so we need to conditionally create and dispatch these events based on feature detection.
const emitPointerEvents = window.PointerEvent !== undefined;

if (emitPointerEvents) {
dispatchPointerEvent(this.element, 'pointerdown', clientX, clientY);
}
this._dispatchPointerEventIfSupported('pointerdown', clientX, clientY);
dispatchMouseEvent(this.element, 'mousedown', clientX, clientY);
if (emitPointerEvents) {
dispatchMouseEvent(this.element, 'pointerup', clientX, clientY);
}
this._dispatchPointerEventIfSupported('pointerup', clientX, clientY);
dispatchMouseEvent(this.element, 'mouseup', clientX, clientY);
dispatchMouseEvent(this.element, 'click', clientX, clientY);

Expand All @@ -115,12 +105,14 @@ export class UnitTestElement implements TestElement {

async hover(): Promise<void> {
await this._stabilize();
this._dispatchPointerEventIfSupported('pointerenter');
dispatchMouseEvent(this.element, 'mouseenter');
await this._stabilize();
}

async mouseAway(): Promise<void> {
await this._stabilize();
this._dispatchPointerEventIfSupported('pointerleave');
dispatchMouseEvent(this.element, 'mouseleave');
await this._stabilize();
}
Expand Down Expand Up @@ -175,4 +167,20 @@ export class UnitTestElement implements TestElement {
await this._stabilize();
return document.activeElement === this.element;
}

/**
* Dispatches a pointer event on the current element if the browser supports it.
* @param name Name of the pointer event to be dispatched.
* @param clientX Coordinate of the user's pointer along the X axis.
* @param clientY Coordinate of the user's pointer along the Y axis.
*/
private _dispatchPointerEventIfSupported(name: string, clientX?: number, clientY?: number) {
// The latest versions of all browsers we support have the new `PointerEvent` API.
// Though since we capture the two most recent versions of these browsers, we also
// need to support Safari 12 at time of writing. Safari 12 does not have support for this,
// so we need to conditionally create and dispatch these events based on feature detection.
if (typeof PointerEvent !== 'undefined' && PointerEvent) {
dispatchPointerEvent(this.element, name, clientX, clientY);
}
}
}

0 comments on commit a5274a9

Please sign in to comment.