-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(tooltip/testing): add test harness for mat-tooltip #19144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
ts_library( | ||
name = "testing", | ||
srcs = glob( | ||
["**/*.ts"], | ||
exclude = ["**/*.spec.ts"], | ||
), | ||
module_name = "@angular/material/tooltip/testing", | ||
deps = [ | ||
"//src/cdk/testing", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "source-files", | ||
srcs = glob(["**/*.ts"]), | ||
) | ||
|
||
ng_test_library( | ||
name = "harness_tests_lib", | ||
srcs = ["shared.spec.ts"], | ||
deps = [ | ||
":testing", | ||
"//src/cdk/testing", | ||
"//src/cdk/testing/testbed", | ||
"//src/material/tooltip", | ||
"@npm//@angular/platform-browser", | ||
], | ||
) | ||
|
||
ng_test_library( | ||
name = "unit_tests_lib", | ||
srcs = glob( | ||
["**/*.spec.ts"], | ||
exclude = ["shared.spec.ts"], | ||
), | ||
deps = [ | ||
":harness_tests_lib", | ||
":testing", | ||
"//src/material/tooltip", | ||
], | ||
) | ||
|
||
ng_web_test_suite( | ||
name = "unit_tests", | ||
deps = [":unit_tests_lib"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
export * from './public-api'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
export * from './tooltip-harness'; | ||
export * from './tooltip-harness-filters'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {HarnessLoader} from '@angular/cdk/testing'; | ||
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; | ||
import {Component} from '@angular/core'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {MatTooltipModule} from '@angular/material/tooltip'; | ||
import {MatTooltipHarness} from '@angular/material/tooltip/testing/tooltip-harness'; | ||
import {NoopAnimationsModule} from '@angular/platform-browser/animations'; | ||
|
||
/** Shared tests to run on both the original and MDC-based tooltips. */ | ||
export function runHarnessTests( | ||
tooltipModule: typeof MatTooltipModule, tooltipHarness: typeof MatTooltipHarness) { | ||
let fixture: ComponentFixture<TooltipHarnessTest>; | ||
let loader: HarnessLoader; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [tooltipModule, NoopAnimationsModule], | ||
declarations: [TooltipHarnessTest], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(TooltipHarnessTest); | ||
fixture.detectChanges(); | ||
loader = TestbedHarnessEnvironment.loader(fixture); | ||
}); | ||
|
||
it('should load all tooltip harnesses', async () => { | ||
const tooltips = await loader.getAllHarnesses(tooltipHarness); | ||
expect(tooltips.length).toBe(2); | ||
}); | ||
|
||
it('should be able to show a tooltip', async () => { | ||
const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'})); | ||
expect(await tooltip.isOpen()).toBe(false); | ||
await tooltip.show(); | ||
expect(await tooltip.isOpen()).toBe(true); | ||
}); | ||
|
||
it('should be able to hide a tooltip', async () => { | ||
const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'})); | ||
expect(await tooltip.isOpen()).toBe(false); | ||
await tooltip.show(); | ||
expect(await tooltip.isOpen()).toBe(true); | ||
await tooltip.hide(); | ||
expect(await tooltip.isOpen()).toBe(false); | ||
}); | ||
|
||
it('should be able to get the text of a tooltip', async () => { | ||
const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'})); | ||
await tooltip.show(); | ||
expect(await tooltip.getTooltipText()).toBe('Tooltip message'); | ||
}); | ||
|
||
it('should return empty when getting the tooltip text while closed', async () => { | ||
const tooltip = await loader.getHarness(tooltipHarness.with({selector: '#one'})); | ||
expect(await tooltip.getTooltipText()).toBe(''); | ||
}); | ||
} | ||
|
||
@Component({ | ||
template: ` | ||
<button [matTooltip]="message" id="one">Trigger 1</button> | ||
<button matTooltip="Static message" id="two">Trigger 2</button> | ||
` | ||
}) | ||
class TooltipHarnessTest { | ||
message = 'Tooltip message'; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {BaseHarnessFilters} from '@angular/cdk/testing'; | ||
|
||
/** A set of criteria that can be used to filter a list of `MatTooltipHarness` instances. */ | ||
export interface TooltipHarnessFilters extends BaseHarnessFilters {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {MatTooltipModule} from '@angular/material/tooltip'; | ||
import {runHarnessTests} from '@angular/material/tooltip/testing/shared.spec'; | ||
import {MatTooltipHarness} from './tooltip-harness'; | ||
|
||
describe('Non-MDC-based MatTooltipHarness', () => { | ||
runHarnessTests(MatTooltipModule, MatTooltipHarness); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing'; | ||
import {TooltipHarnessFilters} from './tooltip-harness-filters'; | ||
|
||
/** Harness for interacting with a standard mat-tooltip in tests. */ | ||
export class MatTooltipHarness extends ComponentHarness { | ||
private _optionalPanel = this.documentRootLocatorFactory().locatorForOptional('.mat-tooltip'); | ||
static hostSelector = '.mat-tooltip-trigger'; | ||
|
||
/** | ||
* Gets a `HarnessPredicate` that can be used to search | ||
* for a tooltip trigger with specific attributes. | ||
* @param options Options for narrowing the search. | ||
* @return a `HarnessPredicate` configured with the given options. | ||
*/ | ||
static with(options: TooltipHarnessFilters = {}): HarnessPredicate<MatTooltipHarness> { | ||
return new HarnessPredicate(MatTooltipHarness, options); | ||
} | ||
|
||
/** Shows the tooltip. */ | ||
async show(): Promise<void> { | ||
return (await this.host()).hover(); | ||
} | ||
crisbeto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** Hides the tooltip. */ | ||
async hide(): Promise<void> { | ||
const host = await this.host(); | ||
await host.mouseAway(); | ||
await this.forceStabilize(); // Needed in order to flush the `hide` animation. | ||
} | ||
|
||
/** Gets whether the tooltip is open. */ | ||
async isOpen(): Promise<boolean> { | ||
return !!(await this._optionalPanel()); | ||
} | ||
|
||
/** Gets a promise for the tooltip panel's text. */ | ||
async getTooltipText(): Promise<string> { | ||
const panel = await this._optionalPanel(); | ||
return panel ? panel.text() : ''; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export declare class MatTooltipHarness extends ComponentHarness { | ||
getTooltipText(): Promise<string>; | ||
hide(): Promise<void>; | ||
isOpen(): Promise<boolean>; | ||
show(): Promise<void>; | ||
static hostSelector: string; | ||
static with(options?: TooltipHarnessFilters): HarnessPredicate<MatTooltipHarness>; | ||
} | ||
|
||
export interface TooltipHarnessFilters extends BaseHarnessFilters { | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.