Skip to content

Commit 5ab55fc

Browse files
authored
docs(material/tooltip): add harness example for tooltip (#21083)
fixup! remove platform check
1 parent dd8a925 commit 5ab55fc

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

src/components-examples/material/tooltip/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ ng_module(
1212
module_name = "@angular/components-examples/material/tooltip",
1313
deps = [
1414
"//src/cdk/scrolling",
15+
"//src/cdk/testing",
16+
"//src/cdk/testing/testbed",
1517
"//src/material/button",
1618
"//src/material/checkbox",
1719
"//src/material/input",
1820
"//src/material/select",
1921
"//src/material/tooltip",
22+
"//src/material/tooltip/testing",
2023
"@npm//@angular/forms",
24+
"@npm//@angular/platform-browser",
25+
"@npm//@angular/platform-browser-dynamic",
26+
"@npm//@types/jasmine",
2127
],
2228
)
2329

src/components-examples/material/tooltip/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import {
1818
} from './tooltip-modified-defaults/tooltip-modified-defaults-example';
1919
import {TooltipOverviewExample} from './tooltip-overview/tooltip-overview-example';
2020
import {TooltipPositionExample} from './tooltip-position/tooltip-position-example';
21+
import {TooltipHarnessExample} from './tooltip-harness/tooltip-harness-example';
2122

2223
export {
2324
TooltipAutoHideExample,
2425
TooltipCustomClassExample,
2526
TooltipDelayExample,
2627
TooltipDisabledExample,
28+
TooltipHarnessExample,
2729
TooltipManualExample,
2830
TooltipMessageExample,
2931
TooltipModifiedDefaultsExample,
@@ -36,6 +38,7 @@ const EXAMPLES = [
3638
TooltipCustomClassExample,
3739
TooltipDelayExample,
3840
TooltipDisabledExample,
41+
TooltipHarnessExample,
3942
TooltipManualExample,
4043
TooltipMessageExample,
4144
TooltipModifiedDefaultsExample,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<button [matTooltip]="message" id="one">Trigger 1</button>
2+
<button matTooltip="Static message" id="two">Trigger 2</button>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatTooltipHarness} from '@angular/material/tooltip/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatTooltipModule} from '@angular/material/tooltip';
8+
import {TooltipHarnessExample} from './tooltip-harness-example';
9+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
10+
11+
describe('TooltipHarnessExample', () => {
12+
let fixture: ComponentFixture<TooltipHarnessExample>;
13+
let loader: HarnessLoader;
14+
15+
beforeAll(() => {
16+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
17+
});
18+
19+
beforeEach(
20+
waitForAsync(() => {
21+
TestBed.configureTestingModule({
22+
imports: [MatTooltipModule, NoopAnimationsModule],
23+
declarations: [TooltipHarnessExample]
24+
}).compileComponents();
25+
fixture = TestBed.createComponent(TooltipHarnessExample);
26+
fixture.detectChanges();
27+
loader = TestbedHarnessEnvironment.loader(fixture);
28+
})
29+
);
30+
31+
it('should load all tooltip harnesses', async () => {
32+
const tooltips = await loader.getAllHarnesses(MatTooltipHarness);
33+
expect(tooltips.length).toBe(2);
34+
});
35+
36+
it('should be able to show a tooltip', async () => {
37+
const tooltip = await loader.getHarness(MatTooltipHarness.with({selector: '#one'}));
38+
expect(await tooltip.isOpen()).toBe(false);
39+
await tooltip.show();
40+
expect(await tooltip.isOpen()).toBe(true);
41+
});
42+
43+
it('should be able to hide a tooltip', async () => {
44+
const tooltip = await loader.getHarness(MatTooltipHarness.with({selector: '#one'}));
45+
expect(await tooltip.isOpen()).toBe(false);
46+
await tooltip.show();
47+
expect(await tooltip.isOpen()).toBe(true);
48+
await tooltip.hide();
49+
expect(await tooltip.isOpen()).toBe(false);
50+
});
51+
52+
it('should be able to get the text of a tooltip', async () => {
53+
const tooltip = await loader.getHarness(MatTooltipHarness.with({selector: '#one'}));
54+
await tooltip.show();
55+
expect(await tooltip.getTooltipText()).toBe('Tooltip message');
56+
});
57+
58+
it('should return empty when getting the tooltip text while closed', async () => {
59+
const tooltip = await loader.getHarness(MatTooltipHarness.with({selector: '#one'}));
60+
expect(await tooltip.getTooltipText()).toBe('');
61+
});
62+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatTooltipHarness
5+
*/
6+
@Component({
7+
selector: 'tooltip-harness-example',
8+
templateUrl: 'tooltip-harness-example.html',
9+
})
10+
export class TooltipHarnessExample {
11+
message = 'Tooltip message';
12+
}

0 commit comments

Comments
 (0)