Skip to content

Commit 28e6722

Browse files
committed
fix(material/testing): Fix newly introduced issue caused by the addition
of floating label functionality to MatFormFieldControlHarness class Adding anything to this abstract class causes compilation issues when using MatFormFieldHarness.getControl(...) to fetch a type other than the directly supported types. The most common problems arise when fetching MatAutocompleteHarness or MatChipGridHarness, but I have found instances of other components such as MatCheckboxHarness. Moving the functionality to a base class resolves this.
1 parent d6dac1d commit 28e6722

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/cdk/testing/component-harness.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ export interface HarnessLoader {
117117
*/
118118
getHarnessAtIndex<T extends ComponentHarness>(query: HarnessQuery<T>, index: number): Promise<T>;
119119

120+
/**
121+
* Searches for an instance of the component corresponding to the given harness type under the
122+
* `HarnessLoader`'s root element, and returns a `ComponentHarness` for the instance on the page
123+
* at the given index. If no matching component exists at that index, an error is thrown.
124+
* @param query A query for a harness to create
125+
* @param index The zero-indexed offset of the matching component instance to return
126+
* @return An instance of the given harness type.
127+
* @throws If a matching component instance can't be found at the given index.
128+
*/
129+
getHarnessAtIndex<T extends ComponentHarness>(query: HarnessQuery<T>, index: number): Promise<T>;
130+
120131
/**
121132
* Searches for all instances of the component corresponding to the given harness type under the
122133
* `HarnessLoader`'s root element, and returns a list `ComponentHarness` for each instance.
@@ -129,7 +140,7 @@ export interface HarnessLoader {
129140
* Searches for all instances of the component corresponding to the given harness type under the
130141
* `HarnessLoader`'s root element, and returns the total count of all matching components.
131142
* @param query A query for a harness to create
132-
* @return An integer indicating the number of instances that were found.
143+
* @return The number of instances that were found.
133144
*/
134145
countHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<number>;
135146

@@ -444,6 +455,12 @@ export abstract class ContentContainerComponentHarness<S extends string = string
444455
return (await this.getRootHarnessLoader()).getHarnessOrNull(query);
445456
}
446457

458+
/**
459+
* Gets the nth matching harness for the given query within the current harness's content.
460+
* @param query The harness query to search for.
461+
* @param index The zero-indexed index of the instance to fetch.
462+
* @returns The first harness matching the given query, or null if none is found.
463+
*/
447464
async getHarnessAtIndex<T extends ComponentHarness>(
448465
query: HarnessQuery<T>,
449466
index: number,
@@ -455,6 +472,13 @@ export abstract class ContentContainerComponentHarness<S extends string = string
455472
return (await this.getRootHarnessLoader()).getAllHarnesses(query);
456473
}
457474

475+
/**
476+
* Counts the number of matching harnesses for the given query within the current harness's
477+
* content.
478+
*
479+
* @param query The harness query to search for.
480+
* @returns The number of matching harnesses for the given query.
481+
*/
458482
async countHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<number> {
459483
return (await this.getRootHarnessLoader()).countHarnesses(query);
460484
}

src/cdk/testing/harness-environment.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,36 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
122122
return this.locatorForOptional(query)();
123123
}
124124

125-
// Implemented as part of the `HarnessLoader` interface.
125+
/**
126+
* Searches for an instance of the component corresponding to the given harness type under the
127+
* `HarnessEnvironment`'s root element, and returns a `ComponentHarness` for that instance. The
128+
* index specifies which harness to return. If no matching component is found, or no harness
129+
* exists at that index, an error is thrown.
130+
* @param query A query for a harness to create
131+
* @param index the zero-indexed index of the harness to return
132+
* @return An instance of the given harness type (or null if not found).
133+
*/
134+
async getHarnessAtIndex<T extends ComponentHarness>(
135+
query: HarnessQuery<T>,
136+
index: number,
137+
): Promise<T> {
138+
if (index < 0) {
139+
throw Error('Index must not be negative');
140+
}
141+
const harnesses = await this.locatorForAll(query)();
142+
if (index >= harnesses.length) {
143+
throw Error(`No harness was found at index ${index}`);
144+
}
145+
return harnesses[index];
146+
}
147+
/**
148+
* Searches for all instances of the component corresponding to the given harness type under the
149+
* `HarnessEnvironment`'s root element, and returns a list `ComponentHarness` for each instance.
150+
* @param query A query for a harness to create
151+
* @param index The zero-indexed offset of the component to return
152+
* @return An instance of the given harness type
153+
* @throws If a matching component instance can't be found.
154+
*/
126155
async getHarnessAtIndex<T extends ComponentHarness>(
127156
query: HarnessQuery<T>,
128157
offset: number,
@@ -142,7 +171,12 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
142171
return this.locatorForAll(query)();
143172
}
144173

145-
// Implemented as part of the `HarnessLoader` interface.
174+
/**
175+
* Searches for instances of the component corresponding to the given harness type under the
176+
* `HarnessEnvironment`'s root element, and returns the number that were found.
177+
* @param query A query for a harness to create
178+
* @return The number of instances that were found.
179+
*/
146180
async countHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<number> {
147181
return (await this.locatorForAll(query)()).length;
148182
}

0 commit comments

Comments
 (0)