source: trip-planner-front/node_modules/@angular/cdk/testing/component-harness.d.ts@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 22.4 KB
RevLine 
[6a3a178]1/**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { TestElement } from './test-element';
9/** An async function that returns a promise when called. */
10export declare type AsyncFactoryFn<T> = () => Promise<T>;
11/** An async function that takes an item and returns a boolean promise */
12export declare type AsyncPredicate<T> = (item: T) => Promise<boolean>;
13/** An async function that takes an item and an option value and returns a boolean promise. */
14export declare type AsyncOptionPredicate<T, O> = (item: T, option: O) => Promise<boolean>;
15/**
16 * A query for a `ComponentHarness`, which is expressed as either a `ComponentHarnessConstructor` or
17 * a `HarnessPredicate`.
18 */
19export declare type HarnessQuery<T extends ComponentHarness> = ComponentHarnessConstructor<T> | HarnessPredicate<T>;
20/**
21 * The result type obtained when searching using a particular list of queries. This type depends on
22 * the particular items being queried.
23 * - If one of the queries is for a `ComponentHarnessConstructor<C1>`, it means that the result
24 * might be a harness of type `C1`
25 * - If one of the queries is for a `HarnessPredicate<C2>`, it means that the result might be a
26 * harness of type `C2`
27 * - If one of the queries is for a `string`, it means that the result might be a `TestElement`.
28 *
29 * Since we don't know for sure which query will match, the result type if the union of the types
30 * for all possible results.
31 *
32 * e.g.
33 * The type:
34 * `LocatorFnResult&lt;[
35 * ComponentHarnessConstructor&lt;MyHarness&gt;,
36 * HarnessPredicate&lt;MyOtherHarness&gt;,
37 * string
38 * ]&gt;`
39 * is equivalent to:
40 * `MyHarness | MyOtherHarness | TestElement`.
41 */
42export declare type LocatorFnResult<T extends (HarnessQuery<any> | string)[]> = {
43 [I in keyof T]: T[I] extends new (...args: any[]) => infer C ? C : T[I] extends {
44 harnessType: new (...args: any[]) => infer C;
45 } ? C : T[I] extends string ? TestElement : never;
46}[number];
47/**
48 * Interface used to load ComponentHarness objects. This interface is used by test authors to
49 * instantiate `ComponentHarness`es.
50 */
51export interface HarnessLoader {
52 /**
53 * Searches for an element with the given selector under the current instances's root element,
54 * and returns a `HarnessLoader` rooted at the matching element. If multiple elements match the
55 * selector, the first is used. If no elements match, an error is thrown.
56 * @param selector The selector for the root element of the new `HarnessLoader`
57 * @return A `HarnessLoader` rooted at the element matching the given selector.
58 * @throws If a matching element can't be found.
59 */
60 getChildLoader(selector: string): Promise<HarnessLoader>;
61 /**
62 * Searches for all elements with the given selector under the current instances's root element,
63 * and returns an array of `HarnessLoader`s, one for each matching element, rooted at that
64 * element.
65 * @param selector The selector for the root element of the new `HarnessLoader`
66 * @return A list of `HarnessLoader`s, one for each matching element, rooted at that element.
67 */
68 getAllChildLoaders(selector: string): Promise<HarnessLoader[]>;
69 /**
70 * Searches for an instance of the component corresponding to the given harness type under the
71 * `HarnessLoader`'s root element, and returns a `ComponentHarness` for that instance. If multiple
72 * matching components are found, a harness for the first one is returned. If no matching
73 * component is found, an error is thrown.
74 * @param query A query for a harness to create
75 * @return An instance of the given harness type
76 * @throws If a matching component instance can't be found.
77 */
78 getHarness<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T>;
79 /**
80 * Searches for all instances of the component corresponding to the given harness type under the
81 * `HarnessLoader`'s root element, and returns a list `ComponentHarness` for each instance.
82 * @param query A query for a harness to create
83 * @return A list instances of the given harness type.
84 */
85 getAllHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T[]>;
86}
87/**
88 * Interface used to create asynchronous locator functions used find elements and component
89 * harnesses. This interface is used by `ComponentHarness` authors to create locator functions for
90 * their `ComponentHarness` subclass.
91 */
92export interface LocatorFactory {
93 /** Gets a locator factory rooted at the document root. */
94 documentRootLocatorFactory(): LocatorFactory;
95 /** The root element of this `LocatorFactory` as a `TestElement`. */
96 rootElement: TestElement;
97 /**
98 * Creates an asynchronous locator function that can be used to find a `ComponentHarness` instance
99 * or element under the root element of this `LocatorFactory`.
100 * @param queries A list of queries specifying which harnesses and elements to search for:
101 * - A `string` searches for elements matching the CSS selector specified by the string.
102 * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
103 * given class.
104 * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
105 * predicate.
106 * @return An asynchronous locator function that searches for and returns a `Promise` for the
107 * first element or harness matching the given search criteria. Matches are ordered first by
108 * order in the DOM, and second by order in the queries list. If no matches are found, the
109 * `Promise` rejects. The type that the `Promise` resolves to is a union of all result types for
110 * each query.
111 *
112 * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
113 * `DivHarness.hostSelector === 'div'`:
114 * - `await lf.locatorFor(DivHarness, 'div')()` gets a `DivHarness` instance for `#d1`
115 * - `await lf.locatorFor('div', DivHarness)()` gets a `TestElement` instance for `#d1`
116 * - `await lf.locatorFor('span')()` throws because the `Promise` rejects.
117 */
118 locatorFor<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>>;
119 /**
120 * Creates an asynchronous locator function that can be used to find a `ComponentHarness` instance
121 * or element under the root element of this `LocatorFactory`.
122 * @param queries A list of queries specifying which harnesses and elements to search for:
123 * - A `string` searches for elements matching the CSS selector specified by the string.
124 * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
125 * given class.
126 * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
127 * predicate.
128 * @return An asynchronous locator function that searches for and returns a `Promise` for the
129 * first element or harness matching the given search criteria. Matches are ordered first by
130 * order in the DOM, and second by order in the queries list. If no matches are found, the
131 * `Promise` is resolved with `null`. The type that the `Promise` resolves to is a union of all
132 * result types for each query or null.
133 *
134 * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
135 * `DivHarness.hostSelector === 'div'`:
136 * - `await lf.locatorForOptional(DivHarness, 'div')()` gets a `DivHarness` instance for `#d1`
137 * - `await lf.locatorForOptional('div', DivHarness)()` gets a `TestElement` instance for `#d1`
138 * - `await lf.locatorForOptional('span')()` gets `null`.
139 */
140 locatorForOptional<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T> | null>;
141 /**
142 * Creates an asynchronous locator function that can be used to find `ComponentHarness` instances
143 * or elements under the root element of this `LocatorFactory`.
144 * @param queries A list of queries specifying which harnesses and elements to search for:
145 * - A `string` searches for elements matching the CSS selector specified by the string.
146 * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
147 * given class.
148 * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
149 * predicate.
150 * @return An asynchronous locator function that searches for and returns a `Promise` for all
151 * elements and harnesses matching the given search criteria. Matches are ordered first by
152 * order in the DOM, and second by order in the queries list. If an element matches more than
153 * one `ComponentHarness` class, the locator gets an instance of each for the same element. If
154 * an element matches multiple `string` selectors, only one `TestElement` instance is returned
155 * for that element. The type that the `Promise` resolves to is an array where each element is
156 * the union of all result types for each query.
157 *
158 * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
159 * `DivHarness.hostSelector === 'div'` and `IdIsD1Harness.hostSelector === '#d1'`:
160 * - `await lf.locatorForAll(DivHarness, 'div')()` gets `[
161 * DivHarness, // for #d1
162 * TestElement, // for #d1
163 * DivHarness, // for #d2
164 * TestElement // for #d2
165 * ]`
166 * - `await lf.locatorForAll('div', '#d1')()` gets `[
167 * TestElement, // for #d1
168 * TestElement // for #d2
169 * ]`
170 * - `await lf.locatorForAll(DivHarness, IdIsD1Harness)()` gets `[
171 * DivHarness, // for #d1
172 * IdIsD1Harness, // for #d1
173 * DivHarness // for #d2
174 * ]`
175 * - `await lf.locatorForAll('span')()` gets `[]`.
176 */
177 locatorForAll<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>[]>;
178 /** @return A `HarnessLoader` rooted at the root element of this `LocatorFactory`. */
179 rootHarnessLoader(): Promise<HarnessLoader>;
180 /**
181 * Gets a `HarnessLoader` instance for an element under the root of this `LocatorFactory`.
182 * @param selector The selector for the root element.
183 * @return A `HarnessLoader` rooted at the first element matching the given selector.
184 * @throws If no matching element is found for the given selector.
185 */
186 harnessLoaderFor(selector: string): Promise<HarnessLoader>;
187 /**
188 * Gets a `HarnessLoader` instance for an element under the root of this `LocatorFactory`
189 * @param selector The selector for the root element.
190 * @return A `HarnessLoader` rooted at the first element matching the given selector, or null if
191 * no matching element is found.
192 */
193 harnessLoaderForOptional(selector: string): Promise<HarnessLoader | null>;
194 /**
195 * Gets a list of `HarnessLoader` instances, one for each matching element.
196 * @param selector The selector for the root element.
197 * @return A list of `HarnessLoader`, one rooted at each element matching the given selector.
198 */
199 harnessLoaderForAll(selector: string): Promise<HarnessLoader[]>;
200 /**
201 * Flushes change detection and async tasks captured in the Angular zone.
202 * In most cases it should not be necessary to call this manually. However, there may be some edge
203 * cases where it is needed to fully flush animation events.
204 */
205 forceStabilize(): Promise<void>;
206 /**
207 * Waits for all scheduled or running async tasks to complete. This allows harness
208 * authors to wait for async tasks outside of the Angular zone.
209 */
210 waitForTasksOutsideAngular(): Promise<void>;
211}
212/**
213 * Base class for component harnesses that all component harness authors should extend. This base
214 * component harness provides the basic ability to locate element and sub-component harness. It
215 * should be inherited when defining user's own harness.
216 */
217export declare abstract class ComponentHarness {
218 protected readonly locatorFactory: LocatorFactory;
219 constructor(locatorFactory: LocatorFactory);
220 /** Gets a `Promise` for the `TestElement` representing the host element of the component. */
221 host(): Promise<TestElement>;
222 /**
223 * Gets a `LocatorFactory` for the document root element. This factory can be used to create
224 * locators for elements that a component creates outside of its own root element. (e.g. by
225 * appending to document.body).
226 */
227 protected documentRootLocatorFactory(): LocatorFactory;
228 /**
229 * Creates an asynchronous locator function that can be used to find a `ComponentHarness` instance
230 * or element under the host element of this `ComponentHarness`.
231 * @param queries A list of queries specifying which harnesses and elements to search for:
232 * - A `string` searches for elements matching the CSS selector specified by the string.
233 * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
234 * given class.
235 * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
236 * predicate.
237 * @return An asynchronous locator function that searches for and returns a `Promise` for the
238 * first element or harness matching the given search criteria. Matches are ordered first by
239 * order in the DOM, and second by order in the queries list. If no matches are found, the
240 * `Promise` rejects. The type that the `Promise` resolves to is a union of all result types for
241 * each query.
242 *
243 * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
244 * `DivHarness.hostSelector === 'div'`:
245 * - `await ch.locatorFor(DivHarness, 'div')()` gets a `DivHarness` instance for `#d1`
246 * - `await ch.locatorFor('div', DivHarness)()` gets a `TestElement` instance for `#d1`
247 * - `await ch.locatorFor('span')()` throws because the `Promise` rejects.
248 */
249 protected locatorFor<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>>;
250 /**
251 * Creates an asynchronous locator function that can be used to find a `ComponentHarness` instance
252 * or element under the host element of this `ComponentHarness`.
253 * @param queries A list of queries specifying which harnesses and elements to search for:
254 * - A `string` searches for elements matching the CSS selector specified by the string.
255 * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
256 * given class.
257 * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
258 * predicate.
259 * @return An asynchronous locator function that searches for and returns a `Promise` for the
260 * first element or harness matching the given search criteria. Matches are ordered first by
261 * order in the DOM, and second by order in the queries list. If no matches are found, the
262 * `Promise` is resolved with `null`. The type that the `Promise` resolves to is a union of all
263 * result types for each query or null.
264 *
265 * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
266 * `DivHarness.hostSelector === 'div'`:
267 * - `await ch.locatorForOptional(DivHarness, 'div')()` gets a `DivHarness` instance for `#d1`
268 * - `await ch.locatorForOptional('div', DivHarness)()` gets a `TestElement` instance for `#d1`
269 * - `await ch.locatorForOptional('span')()` gets `null`.
270 */
271 protected locatorForOptional<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T> | null>;
272 /**
273 * Creates an asynchronous locator function that can be used to find `ComponentHarness` instances
274 * or elements under the host element of this `ComponentHarness`.
275 * @param queries A list of queries specifying which harnesses and elements to search for:
276 * - A `string` searches for elements matching the CSS selector specified by the string.
277 * - A `ComponentHarness` constructor searches for `ComponentHarness` instances matching the
278 * given class.
279 * - A `HarnessPredicate` searches for `ComponentHarness` instances matching the given
280 * predicate.
281 * @return An asynchronous locator function that searches for and returns a `Promise` for all
282 * elements and harnesses matching the given search criteria. Matches are ordered first by
283 * order in the DOM, and second by order in the queries list. If an element matches more than
284 * one `ComponentHarness` class, the locator gets an instance of each for the same element. If
285 * an element matches multiple `string` selectors, only one `TestElement` instance is returned
286 * for that element. The type that the `Promise` resolves to is an array where each element is
287 * the union of all result types for each query.
288 *
289 * e.g. Given the following DOM: `<div id="d1" /><div id="d2" />`, and assuming
290 * `DivHarness.hostSelector === 'div'` and `IdIsD1Harness.hostSelector === '#d1'`:
291 * - `await ch.locatorForAll(DivHarness, 'div')()` gets `[
292 * DivHarness, // for #d1
293 * TestElement, // for #d1
294 * DivHarness, // for #d2
295 * TestElement // for #d2
296 * ]`
297 * - `await ch.locatorForAll('div', '#d1')()` gets `[
298 * TestElement, // for #d1
299 * TestElement // for #d2
300 * ]`
301 * - `await ch.locatorForAll(DivHarness, IdIsD1Harness)()` gets `[
302 * DivHarness, // for #d1
303 * IdIsD1Harness, // for #d1
304 * DivHarness // for #d2
305 * ]`
306 * - `await ch.locatorForAll('span')()` gets `[]`.
307 */
308 protected locatorForAll<T extends (HarnessQuery<any> | string)[]>(...queries: T): AsyncFactoryFn<LocatorFnResult<T>[]>;
309 /**
310 * Flushes change detection and async tasks in the Angular zone.
311 * In most cases it should not be necessary to call this manually. However, there may be some edge
312 * cases where it is needed to fully flush animation events.
313 */
314 protected forceStabilize(): Promise<void>;
315 /**
316 * Waits for all scheduled or running async tasks to complete. This allows harness
317 * authors to wait for async tasks outside of the Angular zone.
318 */
319 protected waitForTasksOutsideAngular(): Promise<void>;
320}
321/**
322 * Base class for component harnesses that authors should extend if they anticipate that consumers
323 * of the harness may want to access other harnesses within the `<ng-content>` of the component.
324 */
325export declare abstract class ContentContainerComponentHarness<S extends string = string> extends ComponentHarness implements HarnessLoader {
326 getChildLoader(selector: S): Promise<HarnessLoader>;
327 getAllChildLoaders(selector: S): Promise<HarnessLoader[]>;
328 getHarness<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T>;
329 getAllHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T[]>;
330 /**
331 * Gets the root harness loader from which to start
332 * searching for content contained by this harness.
333 */
334 protected getRootHarnessLoader(): Promise<HarnessLoader>;
335}
336/** Constructor for a ComponentHarness subclass. */
337export interface ComponentHarnessConstructor<T extends ComponentHarness> {
338 new (locatorFactory: LocatorFactory): T;
339 /**
340 * `ComponentHarness` subclasses must specify a static `hostSelector` property that is used to
341 * find the host element for the corresponding component. This property should match the selector
342 * for the Angular component.
343 */
344 hostSelector: string;
345}
346/** A set of criteria that can be used to filter a list of `ComponentHarness` instances. */
347export interface BaseHarnessFilters {
348 /** Only find instances whose host element matches the given selector. */
349 selector?: string;
350 /** Only find instances that are nested under an element with the given selector. */
351 ancestor?: string;
352}
353/**
354 * A class used to associate a ComponentHarness class with predicates functions that can be used to
355 * filter instances of the class.
356 */
357export declare class HarnessPredicate<T extends ComponentHarness> {
358 harnessType: ComponentHarnessConstructor<T>;
359 private _predicates;
360 private _descriptions;
361 private _ancestor;
362 constructor(harnessType: ComponentHarnessConstructor<T>, options: BaseHarnessFilters);
363 /**
364 * Checks if the specified nullable string value matches the given pattern.
365 * @param value The nullable string value to check, or a Promise resolving to the
366 * nullable string value.
367 * @param pattern The pattern the value is expected to match. If `pattern` is a string,
368 * `value` is expected to match exactly. If `pattern` is a regex, a partial match is
369 * allowed. If `pattern` is `null`, the value is expected to be `null`.
370 * @return Whether the value matches the pattern.
371 */
372 static stringMatches(value: string | null | Promise<string | null>, pattern: string | RegExp | null): Promise<boolean>;
373 /**
374 * Adds a predicate function to be run against candidate harnesses.
375 * @param description A description of this predicate that may be used in error messages.
376 * @param predicate An async predicate function.
377 * @return this (for method chaining).
378 */
379 add(description: string, predicate: AsyncPredicate<T>): this;
380 /**
381 * Adds a predicate function that depends on an option value to be run against candidate
382 * harnesses. If the option value is undefined, the predicate will be ignored.
383 * @param name The name of the option (may be used in error messages).
384 * @param option The option value.
385 * @param predicate The predicate function to run if the option value is not undefined.
386 * @return this (for method chaining).
387 */
388 addOption<O>(name: string, option: O | undefined, predicate: AsyncOptionPredicate<T, O>): this;
389 /**
390 * Filters a list of harnesses on this predicate.
391 * @param harnesses The list of harnesses to filter.
392 * @return A list of harnesses that satisfy this predicate.
393 */
394 filter(harnesses: T[]): Promise<T[]>;
395 /**
396 * Evaluates whether the given harness satisfies this predicate.
397 * @param harness The harness to check
398 * @return A promise that resolves to true if the harness satisfies this predicate,
399 * and resolves to false otherwise.
400 */
401 evaluate(harness: T): Promise<boolean>;
402 /** Gets a description of this predicate for use in error messages. */
403 getDescription(): string;
404 /** Gets the selector used to find candidate elements. */
405 getSelector(): string;
406 /** Adds base options common to all harness types. */
407 private _addBaseOptions;
408}
Note: See TracBrowser for help on using the repository browser.