-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathform-field-control-harness.ts
41 lines (37 loc) · 1.52 KB
/
form-field-control-harness.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* @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.dev/license
*/
import {ComponentHarness} from '@angular/cdk/testing';
/**
* Base class for custom form-field control harnesses. Harnesses for
* custom controls with form-fields need to implement this interface.
*/
export abstract class MatFormFieldControlHarness extends ComponentHarness {
private readonly floatingLabelSelector = '.mdc-floating-label';
/** Gets the text content of the floating label, if it exists. */
protected async _getFloatingLabelText(): Promise<string | null> {
const documentRootLocator = await this.documentRootLocatorFactory();
const labelId = await (await this.host()).getAttribute('aria-labelledby');
const hostId = await (await this.host()).getAttribute('id');
if (labelId) {
// First option, try to fetch the label using the `aria-labelledby`
// attribute.
const labelEl = await await documentRootLocator.locatorForOptional(
`${this.floatingLabelSelector}[id="${labelId}"]`,
)();
return labelEl ? labelEl.text() : null;
} else if (hostId) {
// Fallback option, try to match the id of the input with the `for`
// attribute of the label.
const labelEl = await await documentRootLocator.locatorForOptional(
`${this.floatingLabelSelector}[for="${hostId}"]`,
)();
return labelEl ? labelEl.text() : null;
}
return null;
}
}