|
| 1 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {Component} from '@angular/core'; |
| 4 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {MatTooltipModule} from '@angular/material/tooltip'; |
| 6 | +import {MatTooltipHarness} from '@angular/material/tooltip/testing/tooltip-harness'; |
| 7 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 8 | + |
| 9 | +/** Shared tests to run on both the original and MDC-based tooltips. */ |
| 10 | +export function runHarnessTests( |
| 11 | + tooltipModule: typeof MatTooltipModule, tooltipHarness: typeof MatTooltipHarness) { |
| 12 | + let fixture: ComponentFixture<TooltipHarnessTest>; |
| 13 | + let loader: HarnessLoader; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + await TestBed.configureTestingModule({ |
| 17 | + imports: [tooltipModule, NoopAnimationsModule], |
| 18 | + declarations: [TooltipHarnessTest], |
| 19 | + }).compileComponents(); |
| 20 | + |
| 21 | + fixture = TestBed.createComponent(TooltipHarnessTest); |
| 22 | + fixture.detectChanges(); |
| 23 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should load all tooltip harnesses', async () => { |
| 27 | + const tooltips = await loader.getAllHarnesses(tooltipHarness); |
| 28 | + expect(tooltips.length).toBe(2); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should be able to show a tooltip', async () => { |
| 32 | + const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'})); |
| 33 | + expect(await tooltip.isOpen()).toBe(false); |
| 34 | + await tooltip.show(); |
| 35 | + expect(await tooltip.isOpen()).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should be able to hide a tooltip', async () => { |
| 39 | + const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'})); |
| 40 | + expect(await tooltip.isOpen()).toBe(false); |
| 41 | + await tooltip.show(); |
| 42 | + expect(await tooltip.isOpen()).toBe(true); |
| 43 | + await tooltip.hide(); |
| 44 | + expect(await tooltip.isOpen()).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should be able to get the text of a tooltip', async () => { |
| 48 | + const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'})); |
| 49 | + await tooltip.show(); |
| 50 | + expect(await tooltip.getTooltipText()).toBe('Tooltip message'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return empty when getting the tooltip text while closed', async () => { |
| 54 | + const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'})); |
| 55 | + expect(await tooltip.getTooltipText()).toBe(''); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +@Component({ |
| 60 | + template: ` |
| 61 | + <button [matTooltip]="message" id="one">Trigger 1</button> |
| 62 | + <button matTooltip="Static message" id="two">Trigger 2</button> |
| 63 | + ` |
| 64 | +}) |
| 65 | +class TooltipHarnessTest { |
| 66 | + message = 'Tooltip message'; |
| 67 | +} |
0 commit comments