Skip to content

Commit 2b7a5d1

Browse files
committed
feat(material/testing): Fix implementation issue caused by the addition
of getFloatingLabelText functionality to MatFormFieldControlHarness. Adding anything to MatFormFieldControlHarness causes problems with MatFormFieldHarness.getControl(...) since the function expects a MatFormFieldControlHarness, but doesn't always get a child of this type in practice. For example, MatAutocomplete and MatChipGridHarness are commonly fetched types, and even the directly supported MatDateRangeInputHarness does not inherit from MatFormFieldControlHarness. Moving the functionality to MatFormFieldControlHarnessBase fixes this problem.
1 parent d6dac1d commit 2b7a5d1

File tree

6 files changed

+22
-39
lines changed

6 files changed

+22
-39
lines changed

src/material/datepicker/testing/date-range-input-harness.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class MatDateRangeInputHarness extends DatepickerTriggerHarnessBase {
6464
HarnessPredicate.stringMatches(harness.getValue(), value),
6565
)
6666
.addOption('label', options.label, (harness, label) => {
67-
return HarnessPredicate.stringMatches(harness.getLabel(), label);
67+
return HarnessPredicate.stringMatches(harness.getFloatingLabelText(), label);
6868
});
6969
}
7070

@@ -92,8 +92,8 @@ export class MatDateRangeInputHarness extends DatepickerTriggerHarnessBase {
9292
}
9393

9494
/** Gets the floating label text for the range input, if it exists. */
95-
async getLabel(): Promise<string | null> {
96-
// Copied from MatFormFieldControlHarness since this class cannot extend two classes
95+
async getFloatingLabelText(): Promise<string | null> {
96+
// Copied from MatFormFieldControlHarnessBase since this class cannot extend two classes
9797
const documentRootLocator = await this.documentRootLocatorFactory();
9898
const labelId = await (await this.host()).getAttribute('aria-labelledby');
9999
const hostId = await (await this.host()).getAttribute('id');

src/material/datepicker/testing/datepicker-input-harness-base.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {ComponentHarnessConstructor, HarnessPredicate} from '@angular/cdk/testing';
10-
import {MatFormFieldControlHarness} from '@angular/material/form-field/testing/control';
10+
import {MatFormFieldControlHarnessBase} from '@angular/material/form-field/testing/control';
1111
import {DatepickerInputHarnessFilters} from './datepicker-harness-filters';
1212

1313
/** Sets up the filter predicates for a datepicker input harness. */
@@ -23,12 +23,12 @@ export function getInputPredicate<T extends MatDatepickerInputHarnessBase>(
2323
return HarnessPredicate.stringMatches(harness.getPlaceholder(), placeholder);
2424
})
2525
.addOption('label', options.label, (harness, label) => {
26-
return HarnessPredicate.stringMatches(harness.getLabel(), label);
26+
return HarnessPredicate.stringMatches(harness.getFloatingLabelText(), label);
2727
});
2828
}
2929

3030
/** Base class for datepicker input harnesses. */
31-
export abstract class MatDatepickerInputHarnessBase extends MatFormFieldControlHarness {
31+
export abstract class MatDatepickerInputHarnessBase extends MatFormFieldControlHarnessBase {
3232
/** Whether the input is disabled. */
3333
async isDisabled(): Promise<boolean> {
3434
return (await this.host()).getProperty<boolean>('disabled');
@@ -39,11 +39,6 @@ export abstract class MatDatepickerInputHarnessBase extends MatFormFieldControlH
3939
return (await this.host()).getProperty<boolean>('required');
4040
}
4141

42-
/** Gets the floating label text for the input, if it exists. */
43-
async getLabel(): Promise<string | null> {
44-
return await this._getFloatingLabelText();
45-
}
46-
4742
/** Gets the value of the input. */
4843
async getValue(): Promise<string> {
4944
// The "value" property of the native input is always defined.

src/material/form-field/testing/control/form-field-control-harness.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import {ComponentHarness} from '@angular/cdk/testing';
1010

1111
/**
12-
* Base class for custom form-field control harnesses. Harnesses for
13-
* custom controls with form-fields need to implement this interface.
12+
* Base class for custom form field control harnesses. Harnesses for
13+
* custom controls with form fields need to implement this interface.
1414
*/
15-
export abstract class MatFormFieldControlHarness extends ComponentHarness {
15+
export abstract class MatFormFieldControlHarness extends ComponentHarness {}
16+
17+
/** Shared behavior among MatFormFieldControlHarnesses */
18+
export abstract class MatFormFieldControlHarnessBase extends MatFormFieldControlHarness {
1619
private readonly floatingLabelSelector = '.mdc-floating-label';
1720

1821
/** Gets the text content of the floating label, if it exists. */
19-
protected async _getFloatingLabelText(): Promise<string | null> {
22+
public async getFloatingLabelText(): Promise<string | null> {
2023
const documentRootLocator = await this.documentRootLocatorFactory();
2124
const labelId = await (await this.host()).getAttribute('aria-labelledby');
2225
const hostId = await (await this.host()).getAttribute('id');

src/material/input/testing/input-harness.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
*/
88

99
import {HarnessPredicate, parallel} from '@angular/cdk/testing';
10-
import {MatFormFieldControlHarness} from '@angular/material/form-field/testing/control';
10+
import {MatFormFieldControlHarnessBase} from '@angular/material/form-field/testing/control';
1111
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1212
import {InputHarnessFilters} from './input-harness-filters';
1313

1414
/** Harness for interacting with a standard Material inputs in tests. */
15-
export class MatInputHarness extends MatFormFieldControlHarness {
15+
export class MatInputHarness extends MatFormFieldControlHarnessBase {
1616
private readonly _documentRootLocator = this.documentRootLocatorFactory();
1717

1818
// TODO: We do not want to handle `select` elements with `matNativeControl` because
@@ -35,7 +35,7 @@ export class MatInputHarness extends MatFormFieldControlHarness {
3535
return HarnessPredicate.stringMatches(harness.getPlaceholder(), placeholder);
3636
})
3737
.addOption('label', options.label, (harness, label) => {
38-
return HarnessPredicate.stringMatches(harness.getLabel(), label);
38+
return HarnessPredicate.stringMatches(harness.getFloatingLabelText(), label);
3939
});
4040
}
4141

@@ -99,11 +99,6 @@ export class MatInputHarness extends MatFormFieldControlHarness {
9999
return await (await this.host()).getProperty<string>('id');
100100
}
101101

102-
/** Gets the floating label text for the input, if it exists. */
103-
async getLabel(): Promise<string | null> {
104-
return await this._getFloatingLabelText();
105-
}
106-
107102
/**
108103
* Focuses the input and returns a promise that indicates when the
109104
* action is complete.

src/material/input/testing/native-select-harness.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
*/
88

99
import {HarnessPredicate, parallel} from '@angular/cdk/testing';
10-
import {MatFormFieldControlHarness} from '../../form-field/testing/control';
10+
import {MatFormFieldControlHarnessBase} from '../../form-field/testing/control';
1111
import {MatNativeOptionHarness} from './native-option-harness';
1212
import {
1313
NativeOptionHarnessFilters,
1414
NativeSelectHarnessFilters,
1515
} from './native-select-harness-filters';
1616

1717
/** Harness for interacting with a native `select` in tests. */
18-
export class MatNativeSelectHarness extends MatFormFieldControlHarness {
18+
export class MatNativeSelectHarness extends MatFormFieldControlHarnessBase {
1919
static hostSelector = 'select[matNativeControl]';
2020

2121
/**
@@ -29,7 +29,7 @@ export class MatNativeSelectHarness extends MatFormFieldControlHarness {
2929
'label',
3030
options.label,
3131
(harness, label) => {
32-
return HarnessPredicate.stringMatches(harness.getLabel(), label);
32+
return HarnessPredicate.stringMatches(harness.getFloatingLabelText(), label);
3333
},
3434
);
3535
}
@@ -61,11 +61,6 @@ export class MatNativeSelectHarness extends MatFormFieldControlHarness {
6161
return await (await this.host()).getProperty<string>('id');
6262
}
6363

64-
/** Gets the floating label text for the input, if it exists. */
65-
async getLabel(): Promise<string | null> {
66-
return await this._getFloatingLabelText();
67-
}
68-
6964
/** Focuses the select and returns a void promise that indicates when the action is complete. */
7065
async focus(): Promise<void> {
7166
return (await this.host()).focus();

src/material/select/testing/select-harness.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import {
1313
OptionHarnessFilters,
1414
OptgroupHarnessFilters,
1515
} from '@angular/material/core/testing';
16-
import {MatFormFieldControlHarness} from '@angular/material/form-field/testing/control';
16+
import {MatFormFieldControlHarnessBase} from '@angular/material/form-field/testing/control';
1717
import {SelectHarnessFilters} from './select-harness-filters';
1818

1919
/** Harness for interacting with a mat-select in tests. */
20-
export class MatSelectHarness extends MatFormFieldControlHarness {
20+
export class MatSelectHarness extends MatFormFieldControlHarnessBase {
2121
static hostSelector = '.mat-mdc-select';
2222
private _prefix = 'mat-mdc';
2323
private _optionClass = MatOptionHarness;
@@ -39,7 +39,7 @@ export class MatSelectHarness extends MatFormFieldControlHarness {
3939
return (await harness.isDisabled()) === disabled;
4040
})
4141
.addOption('label', options.label, (harness, label) => {
42-
return HarnessPredicate.stringMatches(harness.getLabel(), label);
42+
return HarnessPredicate.stringMatches(harness.getFloatingLabelText(), label);
4343
});
4444
}
4545

@@ -74,11 +74,6 @@ export class MatSelectHarness extends MatFormFieldControlHarness {
7474
return value.text();
7575
}
7676

77-
/** Gets the floating label text for the select, if it exists. */
78-
async getLabel(): Promise<string | null> {
79-
return await this._getFloatingLabelText();
80-
}
81-
8277
/** Focuses the select and returns a void promise that indicates when the action is complete. */
8378
async focus(): Promise<void> {
8479
return (await this.host()).focus();

0 commit comments

Comments
 (0)