Skip to content

Commit a5274a9

Browse files
authored
fix(cdk/testing): emit pointer events on hover and mouseAway (#20098)
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.
1 parent 04b7523 commit a5274a9

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/cdk/testing/testbed/unit-test-element.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,9 @@ export class UnitTestElement implements TestElement {
8181
const clientX = Math.round(left + relativeX);
8282
const clientY = Math.round(top + relativeY);
8383

84-
// The latest versions of all browsers we support have the new `PointerEvent` API.
85-
// Though since we capture the two most recent versions of these browsers, we also
86-
// need to support Safari 12 at time of writing. Safari 12 does not have support for this,
87-
// so we need to conditionally create and dispatch these events based on feature detection.
88-
const emitPointerEvents = window.PointerEvent !== undefined;
89-
90-
if (emitPointerEvents) {
91-
dispatchPointerEvent(this.element, 'pointerdown', clientX, clientY);
92-
}
84+
this._dispatchPointerEventIfSupported('pointerdown', clientX, clientY);
9385
dispatchMouseEvent(this.element, 'mousedown', clientX, clientY);
94-
if (emitPointerEvents) {
95-
dispatchMouseEvent(this.element, 'pointerup', clientX, clientY);
96-
}
86+
this._dispatchPointerEventIfSupported('pointerup', clientX, clientY);
9787
dispatchMouseEvent(this.element, 'mouseup', clientX, clientY);
9888
dispatchMouseEvent(this.element, 'click', clientX, clientY);
9989

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

116106
async hover(): Promise<void> {
117107
await this._stabilize();
108+
this._dispatchPointerEventIfSupported('pointerenter');
118109
dispatchMouseEvent(this.element, 'mouseenter');
119110
await this._stabilize();
120111
}
121112

122113
async mouseAway(): Promise<void> {
123114
await this._stabilize();
115+
this._dispatchPointerEventIfSupported('pointerleave');
124116
dispatchMouseEvent(this.element, 'mouseleave');
125117
await this._stabilize();
126118
}
@@ -175,4 +167,20 @@ export class UnitTestElement implements TestElement {
175167
await this._stabilize();
176168
return document.activeElement === this.element;
177169
}
170+
171+
/**
172+
* Dispatches a pointer event on the current element if the browser supports it.
173+
* @param name Name of the pointer event to be dispatched.
174+
* @param clientX Coordinate of the user's pointer along the X axis.
175+
* @param clientY Coordinate of the user's pointer along the Y axis.
176+
*/
177+
private _dispatchPointerEventIfSupported(name: string, clientX?: number, clientY?: number) {
178+
// The latest versions of all browsers we support have the new `PointerEvent` API.
179+
// Though since we capture the two most recent versions of these browsers, we also
180+
// need to support Safari 12 at time of writing. Safari 12 does not have support for this,
181+
// so we need to conditionally create and dispatch these events based on feature detection.
182+
if (typeof PointerEvent !== 'undefined' && PointerEvent) {
183+
dispatchPointerEvent(this.element, name, clientX, clientY);
184+
}
185+
}
178186
}

0 commit comments

Comments
 (0)