Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(material/form-field): add error harness (#25698)
* feat(material/form-field): add error harness * feat(material/form-field): deprecate legacy error harness * fix(material/form-field): use error harness for all getters
- Loading branch information
1 parent
8e9625e
commit 36af2a3
Showing
12 changed files
with
217 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
/** | ||
* @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, | ||
ComponentHarness, | ||
ComponentHarnessConstructor, | ||
HarnessPredicate, | ||
} from '@angular/cdk/testing'; | ||
|
||
/** A set of criteria that can be used to filter a list of error harness instances. */ | ||
export interface ErrorHarnessFilters extends BaseHarnessFilters { | ||
/** Only find instances whose text matches the given value. */ | ||
text?: string | RegExp; | ||
} | ||
|
||
export abstract class _MatErrorHarnessBase extends ComponentHarness { | ||
/** Gets a promise for the error's label text. */ | ||
async getText(): Promise<string> { | ||
return (await this.host()).text(); | ||
} | ||
|
||
protected static _getErrorPredicate<T extends MatErrorHarness>( | ||
type: ComponentHarnessConstructor<T>, | ||
options: ErrorHarnessFilters, | ||
): HarnessPredicate<T> { | ||
return new HarnessPredicate(type, options).addOption('text', options.text, (harness, text) => | ||
HarnessPredicate.stringMatches(harness.getText(), text), | ||
); | ||
} | ||
} | ||
|
||
/** Harness for interacting with an MDC-based `mat-error` in tests. */ | ||
export class MatErrorHarness extends _MatErrorHarnessBase { | ||
static hostSelector = '.mat-mdc-form-field-error'; | ||
|
||
/** | ||
* Gets a `HarnessPredicate` that can be used to search for an error with specific | ||
* attributes. | ||
* @param options Options for filtering which error instances are considered a match. | ||
* @return a `HarnessPredicate` configured with the given options. | ||
*/ | ||
static with<T extends MatErrorHarness>( | ||
this: ComponentHarnessConstructor<T>, | ||
options: ErrorHarnessFilters = {}, | ||
): HarnessPredicate<T> { | ||
return _MatErrorHarnessBase._getErrorPredicate(this, options); | ||
} | ||
} |
This file contains 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 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 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 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 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 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,33 @@ | ||
/** | ||
* @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 {ComponentHarnessConstructor, HarnessPredicate} from '@angular/cdk/testing'; | ||
|
||
import {_MatErrorHarnessBase, ErrorHarnessFilters} from '@angular/material/form-field/testing'; | ||
|
||
/** | ||
* Harness for interacting with a `mat-error` in tests. | ||
* @deprecated Use `MatErrorHarness` from `@angular/material/form-field/testing` instead. See https://material.angular.io/guide/mdc-migration for information about migrating. | ||
* @breaking-change 17.0.0 | ||
*/ | ||
export class MatLegacyErrorHarness extends _MatErrorHarnessBase { | ||
static hostSelector = '.mat-error'; | ||
|
||
/** | ||
* Gets a `HarnessPredicate` that can be used to search for an error with specific | ||
* attributes. | ||
* @param options Options for filtering which error instances are considered a match. | ||
* @return a `HarnessPredicate` configured with the given options. | ||
*/ | ||
static with<T extends MatLegacyErrorHarness>( | ||
this: ComponentHarnessConstructor<T>, | ||
options: ErrorHarnessFilters = {}, | ||
): HarnessPredicate<T> { | ||
return _MatErrorHarnessBase._getErrorPredicate(this, options); | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.