Skip to content

Commit

Permalink
fix(cdk/testing): dispatch mouseover and mouseout events in UnitTestE…
Browse files Browse the repository at this point in the history
…lement (angular#24490)

Fixes that the `UnitTestElement` wasn't dispatching `mouseover` and `mouseout` on `hover`/`mouseAway` like the browser would.

Fixes angular#24486.
  • Loading branch information
crisbeto authored and forsti0506 committed Apr 3, 2022
1 parent 7a5e659 commit 979fab1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/cdk/testing/testbed/unit-test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,15 @@ export class UnitTestElement implements TestElement {
/** Hovers the mouse over the element. */
async hover(): Promise<void> {
this._dispatchPointerEventIfSupported('pointerenter');
dispatchMouseEvent(this.element, 'mouseover');
dispatchMouseEvent(this.element, 'mouseenter');
await this._stabilize();
}

/** Moves the mouse away from the element. */
async mouseAway(): Promise<void> {
this._dispatchPointerEventIfSupported('pointerleave');
dispatchMouseEvent(this.element, 'mouseout');
dispatchMouseEvent(this.element, 'mouseleave');
await this._stabilize();
}
Expand Down
8 changes: 6 additions & 2 deletions src/cdk/testing/tests/cross-environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,25 +488,29 @@ export function crossEnvironmentSpecs(
expect(dimensions).toEqual(jasmine.objectContaining({height: 100, width: 200}));
});

it('should be able to hover', async () => {
it('should dispatch `mouseenter` and `mouseover` on hover', async () => {
const box = await harness.hoverTest();
let classAttr = await box.getAttribute('class');
expect(classAttr).not.toContain('hovering');
expect(classAttr).not.toContain('pointer-over');
await box.hover();
classAttr = await box.getAttribute('class');
expect(classAttr).toContain('hovering');
expect(classAttr).toContain('pointer-over');
});

it('should be able to stop hovering', async () => {
it('should dispatch `mouseleave` and `mouseout` on mouseAway', async () => {
const box = await harness.hoverTest();
let classAttr = await box.getAttribute('class');
expect(classAttr).not.toContain('hovering');
await box.hover();
classAttr = await box.getAttribute('class');
expect(classAttr).toContain('hovering');
expect(classAttr).toContain('pointer-over');
await box.mouseAway();
classAttr = await box.getAttribute('class');
expect(classAttr).not.toContain('hovering');
expect(classAttr).not.toContain('pointer-over');
});

it('should be able to getAttribute', async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/cdk/testing/tests/test-main-component.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ <h1 style="height: 100px; width: 200px;">Main Component</h1>
<div
id="hover-box"
[class.hovering]="isHovering"
[class.pointer-over]="isPointerOver"
(mouseenter)="isHovering = true"
(mouseleave)="isHovering = false"
(mouseover)="isPointerOver = true"
(mouseout)="isPointerOver = false"
style="width: 50px; height: 50px; background: hotpink;"></div>
<div class="hidden-element" style="display: none;">Hello</div>
1 change: 1 addition & 0 deletions src/cdk/testing/tests/test-main-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class TestMainComponent implements OnDestroy {
testTools: string[];
testMethods: string[];
isHovering = false;
isPointerOver = false;
specialKey = '';
modifiers: string;
singleSelect: string;
Expand Down

0 comments on commit 979fab1

Please sign in to comment.