Skip to content

Commit 4ea6c3d

Browse files
committed
feat(material/testing): Modify HarnessLoader with the addition of
`getHarnessAtIndex` and `countHarnesses` These two new functions are intended to expand harness testing functionality by providing built-in functions for commonly used patterns. * `getHarnessAtIndex` functions similarly to `getHarness`, but returns a harness for the matching component instance with the given index. An example use case is to fetch the nth MatOptionHarness on screen. * `countHarnesses` simply counts the number of matching component instances and returns the result. Documentation is updated to reflect this changes, and adds a missing row for the `hasHarness` function Manually tested using the MatInputHarness tests
1 parent 015a668 commit 4ea6c3d

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

src/cdk/testing/component-harness.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ export interface HarnessLoader {
130130
*/
131131
getHarnessAtIndex<T extends ComponentHarness>(query: HarnessQuery<T>, index: number): Promise<T>;
132132

133+
/**
134+
* Searches for an instance of the component corresponding to the given harness type under the
135+
* `HarnessLoader`'s root element, and returns a `ComponentHarness` for the instance on the page
136+
* at the given index. If no matching component exists at that index, an error is thrown.
137+
* @param query A query for a harness to create
138+
* @param index The zero-indexed offset of the matching component instance to return
139+
* @return An instance of the given harness type.
140+
* @throws If a matching component instance can't be found at the given index.
141+
*/
142+
getHarnessAtIndex<T extends ComponentHarness>(query: HarnessQuery<T>, index: number): Promise<T>;
143+
133144
/**
134145
* Searches for all instances of the component corresponding to the given harness type under the
135146
* `HarnessLoader`'s root element, and returns a list `ComponentHarness` for each instance.
@@ -142,7 +153,7 @@ export interface HarnessLoader {
142153
* Searches for all instances of the component corresponding to the given harness type under the
143154
* `HarnessLoader`'s root element, and returns the total count of all matching components.
144155
* @param query A query for a harness to create
145-
* @return An integer indicating the number of instances that were found.
156+
* @return The number of instances that were found.
146157
*/
147158
countHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<number>;
148159

@@ -520,12 +531,10 @@ export abstract class ContentContainerComponentHarness<S extends string = string
520531
}
521532

522533
/**
523-
* Gets the nth matching harness for the given query within the current harness's content,
524-
* matching the provided index.
534+
* Gets the nth matching harness for the given query within the current harness's content.
525535
* @param query The harness query to search for.
526-
* @param index The zero-indexed offset of the matching component instance to return
527-
* @returns The first harness matching the given query.
528-
* @throws If no matching harness is found.
536+
* @param index The zero-indexed index of the instance to fetch.
537+
* @returns The first harness matching the given query, or null if none is found.
529538
*/
530539
async getHarnessAtIndex<T extends ComponentHarness>(
531540
query: HarnessQuery<T>,
@@ -554,6 +563,17 @@ export abstract class ContentContainerComponentHarness<S extends string = string
554563
return (await this.getRootHarnessLoader()).countHarnesses(query);
555564
}
556565

566+
/**
567+
* Counts the number of matching harnesses for the given query within the current harness's
568+
* content.
569+
*
570+
* @param query The harness query to search for.
571+
* @returns The number of matching harnesses for the given query.
572+
*/
573+
async countHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<number> {
574+
return (await this.getRootHarnessLoader()).countHarnesses(query);
575+
}
576+
557577
/**
558578
* Checks whether there is a matching harnesses for the given query within the current harness's
559579
* content.

src/cdk/testing/harness-environment.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,28 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
251251
/**
252252
* Searches for an instance of the component corresponding to the given harness type under the
253253
* `HarnessEnvironment`'s root element, and returns a `ComponentHarness` for that instance. The
254-
* specified index indicates the offset of the instance to return. If no matching component is
255-
* found at that index, an error is thrown.
254+
* index specifies which harness to return. If no matching component is found, or no harness
255+
* exists at that index, an error is thrown.
256+
* @param query A query for a harness to create
257+
* @param index the zero-indexed index of the harness to return
258+
* @return An instance of the given harness type (or null if not found).
259+
*/
260+
async getHarnessAtIndex<T extends ComponentHarness>(
261+
query: HarnessQuery<T>,
262+
index: number,
263+
): Promise<T> {
264+
if (index < 0) {
265+
throw Error('Index must not be negative');
266+
}
267+
const harnesses = await this.locatorForAll(query)();
268+
if (index >= harnesses.length) {
269+
throw Error(`No harness was found at index ${index}`);
270+
}
271+
return harnesses[index];
272+
}
273+
/**
274+
* Searches for all instances of the component corresponding to the given harness type under the
275+
* `HarnessEnvironment`'s root element, and returns a list `ComponentHarness` for each instance.
256276
* @param query A query for a harness to create
257277
* @param index The zero-indexed offset of the component to return
258278
* @return An instance of the given harness type
@@ -277,8 +297,9 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
277297
return this.locatorForAll(query)();
278298
}
279299

280-
/**
281-
* Searches for all instances of the component corresponding to the given harness type under the
300+
301+
/**
302+
* Searches for instances of the component corresponding to the given harness type under the
282303
* `HarnessEnvironment`'s root element, and returns the number that were found.
283304
* @param query A query for a harness to create
284305
* @return The number of instances that were found.

0 commit comments

Comments
 (0)