Skip to content

Commit 2f2b0c7

Browse files
crisbetoandrewseguin
authored andcommitted
fix(cdk/testing): dispatch mouseover and mouseout events in UnitTestElement (#24490)
Fixes that the `UnitTestElement` wasn't dispatching `mouseover` and `mouseout` on `hover`/`mouseAway` like the browser would. Fixes #24486. (cherry picked from commit 552103b)
1 parent f01e495 commit 2f2b0c7

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ export class UnitTestElement implements TestElement {
143143
/** Hovers the mouse over the element. */
144144
async hover(): Promise<void> {
145145
this._dispatchPointerEventIfSupported('pointerenter');
146+
dispatchMouseEvent(this.element, 'mouseover');
146147
dispatchMouseEvent(this.element, 'mouseenter');
147148
await this._stabilize();
148149
}
149150

150151
/** Moves the mouse away from the element. */
151152
async mouseAway(): Promise<void> {
152153
this._dispatchPointerEventIfSupported('pointerleave');
154+
dispatchMouseEvent(this.element, 'mouseout');
153155
dispatchMouseEvent(this.element, 'mouseleave');
154156
await this._stabilize();
155157
}

src/cdk/testing/tests/cross-environment.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,25 +470,29 @@ export function crossEnvironmentSpecs(
470470
expect(dimensions).toEqual(jasmine.objectContaining({height: 100, width: 200}));
471471
});
472472

473-
it('should be able to hover', async () => {
473+
it('should dispatch `mouseenter` and `mouseover` on hover', async () => {
474474
const box = await harness.hoverTest();
475475
let classAttr = await box.getAttribute('class');
476476
expect(classAttr).not.toContain('hovering');
477+
expect(classAttr).not.toContain('pointer-over');
477478
await box.hover();
478479
classAttr = await box.getAttribute('class');
479480
expect(classAttr).toContain('hovering');
481+
expect(classAttr).toContain('pointer-over');
480482
});
481483

482-
it('should be able to stop hovering', async () => {
484+
it('should dispatch `mouseleave` and `mouseout` on mouseAway', async () => {
483485
const box = await harness.hoverTest();
484486
let classAttr = await box.getAttribute('class');
485487
expect(classAttr).not.toContain('hovering');
486488
await box.hover();
487489
classAttr = await box.getAttribute('class');
488490
expect(classAttr).toContain('hovering');
491+
expect(classAttr).toContain('pointer-over');
489492
await box.mouseAway();
490493
classAttr = await box.getAttribute('class');
491494
expect(classAttr).not.toContain('hovering');
495+
expect(classAttr).not.toContain('pointer-over');
492496
});
493497

494498
it('should be able to getAttribute', async () => {

src/cdk/testing/tests/test-main-component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ <h1 style="height: 100px; width: 200px;">Main Component</h1>
7878
<div
7979
id="hover-box"
8080
[class.hovering]="isHovering"
81+
[class.pointer-over]="isPointerOver"
8182
(mouseenter)="isHovering = true"
8283
(mouseleave)="isHovering = false"
84+
(mouseover)="isPointerOver = true"
85+
(mouseout)="isPointerOver = false"
8386
style="width: 50px; height: 50px; background: hotpink;"></div>
8487
<div class="hidden-element" style="display: none;">Hello</div>

src/cdk/testing/tests/test-main-component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class TestMainComponent implements OnDestroy {
3535
testTools: string[];
3636
testMethods: string[];
3737
isHovering = false;
38+
isPointerOver = false;
3839
specialKey = '';
3940
modifiers: string;
4041
singleSelect: string;

0 commit comments

Comments
 (0)