Skip to content

Commit 24cbe1d

Browse files
authored
fix(material/tooltip): handle touch devices in test harness (#21220)
`MatTooltip` binds either a mouse or a touch event, depending on whether it's on a touch device or not. In the harnesses we were limited to dispatching only mouse events which meant that tests couldn't be run against touch devices. Now that we can fake any sort of event, we're able to support touch devices too.
1 parent 28c36f8 commit 24cbe1d

File tree

3 files changed

+13
-30
lines changed

3 files changed

+13
-30
lines changed

src/material/tooltip/testing/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ ng_test_library(
2424
srcs = ["shared.spec.ts"],
2525
deps = [
2626
":testing",
27-
"//src/cdk/platform",
2827
"//src/cdk/testing",
2928
"//src/cdk/testing/testbed",
3029
"//src/material/tooltip",

src/material/tooltip/testing/shared.spec.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
55
import {MatTooltipModule} from '@angular/material/tooltip';
66
import {MatTooltipHarness} from '@angular/material/tooltip/testing/tooltip-harness';
77
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
8-
import {Platform} from '@angular/cdk/platform';
98

109
/** Shared tests to run on both the original and MDC-based tooltips. */
1110
export function runHarnessTests(
1211
tooltipModule: typeof MatTooltipModule, tooltipHarness: typeof MatTooltipHarness) {
13-
// TODO(COMP-322): whether the current test device is supported by the harness. At the time of
14-
// writing, we have to skip these tests on touch devices, because we don't have a way of
15-
// simulating touch events. This variable should be removed once the issue is resolved.
16-
let isSupported: boolean;
1712
let fixture: ComponentFixture<TooltipHarnessTest>;
1813
let loader: HarnessLoader;
1914

@@ -26,36 +21,21 @@ export function runHarnessTests(
2621
fixture = TestBed.createComponent(TooltipHarnessTest);
2722
fixture.detectChanges();
2823
loader = TestbedHarnessEnvironment.loader(fixture);
29-
30-
const platform = TestBed.inject(Platform);
31-
isSupported = !platform.IOS && !platform.ANDROID;
3224
});
3325

3426
it('should load all tooltip harnesses', async () => {
35-
if (!isSupported) {
36-
return;
37-
}
38-
3927
const tooltips = await loader.getAllHarnesses(tooltipHarness);
4028
expect(tooltips.length).toBe(2);
4129
});
4230

4331
it('should be able to show a tooltip', async () => {
44-
if (!isSupported) {
45-
return;
46-
}
47-
4832
const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'}));
4933
expect(await tooltip.isOpen()).toBe(false);
5034
await tooltip.show();
5135
expect(await tooltip.isOpen()).toBe(true);
5236
});
5337

5438
it('should be able to hide a tooltip', async () => {
55-
if (!isSupported) {
56-
return;
57-
}
58-
5939
const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'}));
6040
expect(await tooltip.isOpen()).toBe(false);
6141
await tooltip.show();
@@ -65,20 +45,12 @@ export function runHarnessTests(
6545
});
6646

6747
it('should be able to get the text of a tooltip', async () => {
68-
if (!isSupported) {
69-
return;
70-
}
71-
7248
const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'}));
7349
await tooltip.show();
7450
expect(await tooltip.getTooltipText()).toBe('Tooltip message');
7551
});
7652

7753
it('should return empty when getting the tooltip text while closed', async () => {
78-
if (!isSupported) {
79-
return;
80-
}
81-
8254
const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'}));
8355
expect(await tooltip.getTooltipText()).toBe('');
8456
});

src/material/tooltip/testing/tooltip-harness.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ export class MatTooltipHarness extends ComponentHarness {
2626

2727
/** Shows the tooltip. */
2828
async show(): Promise<void> {
29-
return (await this.host()).hover();
29+
const host = await this.host();
30+
31+
// We need to dispatch both `touchstart` and a hover event, because the tooltip binds
32+
// different events depending on the device. The `changedTouches` is there in case the
33+
// element has ripples.
34+
// @breaking-change 12.0.0 Remove null assertion from `dispatchEvent`.
35+
await host.dispatchEvent?.('touchstart', {changedTouches: []});
36+
await host.hover();
3037
}
3138

3239
/** Hides the tooltip. */
3340
async hide(): Promise<void> {
3441
const host = await this.host();
42+
43+
// We need to dispatch both `touchstart` and a hover event, because
44+
// the tooltip binds different events depending on the device.
45+
// @breaking-change 12.0.0 Remove null assertion from `dispatchEvent`.
46+
await host.dispatchEvent?.('touchend');
3547
await host.mouseAway();
3648
await this.forceStabilize(); // Needed in order to flush the `hide` animation.
3749
}

0 commit comments

Comments
 (0)