Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk/testing): emit pointer events on hover and mouseAway #20098

Merged
merged 1 commit into from
Jul 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/cdk/testing/testbed/unit-test-element.ts
Original file line number Diff line number Diff line change
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);
}
}
}