88
99import { TestElement } from './test-element' ;
1010
11- /** An async function that returns a promise of the given type when called. */
11+ /** An async function that returns a promise when called. */
1212export type AsyncFn < T > = ( ) => Promise < T > ;
1313
14+ /**
15+ * Interface used to load ComponentHarness objects. This interface is used by test authors to
16+ * instantiate `ComponentHarness`es.
17+ */
1418export interface HarnessLoader {
19+ /**
20+ * Searches for an element with the given selector under the current instances's root element,
21+ * and returns a `HarnessLoader` rooted at the matching element. If multiple elements match the
22+ * selector, the first is used. If no elements match, an error is thrown.
23+ * @param selector The selector for the root element of the new `HarnessLoader`
24+ * @return A `HarnessLoader` rooted at the element matching the given selector.
25+ * @throws If a matching element can't be found.
26+ */
1527 findRequired ( selector : string ) : Promise < HarnessLoader > ;
28+
29+ /**
30+ * Searches for an element with the given selector under the current instances's root element,
31+ * and returns a `HarnessLoader` rooted at the matching element. If multiple elements match the
32+ * selector, the first is used. If no elements match, null is returned.
33+ * @param selector The selector for the root element of the new `HarnessLoader`
34+ * @return A `HarnessLoader` rooted at the element matching the given selector, or null if no
35+ * matching element was found.
36+ */
1637 findOptional ( selector : string ) : Promise < HarnessLoader | null > ;
38+
39+ /**
40+ * Searches for all elements with the given selector under the current instances's root element,
41+ * and returns an array of `HarnessLoader`s, one for each matching element, rooted at that
42+ * element.
43+ * @param selector The selector for the root element of the new `HarnessLoader`
44+ * @return A list of `HarnessLoader`s, one for each matching element, rooted at that element.
45+ */
1746 findAll ( selector : string ) : Promise < HarnessLoader [ ] > ;
18- requiredHarness < T extends ComponentHarness > ( harness : ComponentHarnessConstructor < T > ) : Promise < T > ;
19- optionalHarness < T extends ComponentHarness > ( harness : ComponentHarnessConstructor < T > ) :
47+
48+ /**
49+ * Searches for an instance of the component corresponding to the given harness type under the
50+ * `HarnessLoader`'s root element, and returns a `ComponentHarness` for that instance. If multiple
51+ * matching components are found, a harness for the first one is returned. If no matching
52+ * component is found, an error is thrown.
53+ * @param harnessType The type of harness to create
54+ * @return An instance of the given harness type
55+ * @throws If a matching component instance can't be found.
56+ */
57+ requiredHarness < T extends ComponentHarness > ( harnessType : ComponentHarnessConstructor < T > ) :
58+ Promise < T > ;
59+
60+ /**
61+ * Searches for an instance of the component corresponding to the given harness type under the
62+ * `HarnessLoader`'s root element, and returns a `ComponentHarness` for that instance. If multiple
63+ * matching components are found, a harness for the first one is returned. If no matching
64+ * component is found, null is returned.
65+ * @param harnessType The type of harness to create
66+ * @return An instance of the given harness type, or null if none is found.
67+ */
68+ optionalHarness < T extends ComponentHarness > ( harnessType : ComponentHarnessConstructor < T > ) :
2069 Promise < T | null > ;
21- allHarnesses < T extends ComponentHarness > ( harness : ComponentHarnessConstructor < T > ) : Promise < T [ ] > ;
70+
71+ /**
72+ * Searches for all instances of the component corresponding to the given harness type under the
73+ * `HarnessLoader`'s root element, and returns a list `ComponentHarness` for each instance.
74+ * @param harnessType The type of harness to create
75+ * @return A list instances of the given harness type.
76+ */
77+ allHarnesses < T extends ComponentHarness > ( harnessType : ComponentHarnessConstructor < T > ) :
78+ Promise < T [ ] > ;
2279}
2380
81+ /**
82+ * Interface used to create asynchronous locator functions used find elements and component
83+ * harnesses. This interface is used by `ComponentHarness` authors to create locator functions for
84+ * their `ComponentHarenss` subclass.
85+ */
2486export interface LocatorFactory {
87+ /** Gets a locator factory rooted at the document root. */
2588 documentRootLocatorFactory ( ) : LocatorFactory ;
89+
90+ /** Gets the root element of this `LocatorFactory` as a `TestElement`. */
2691 rootElement ( ) : TestElement ;
92+
93+ /**
94+ * Creates an asynchronous locator function that can be used to search for elements with the given
95+ * selector under the root element of this `LocatorFactory`. When the resulting locator function
96+ * is invoked, if multiple matching elements are found, the first element is returned. If no
97+ * elements are found, an error is thrown.
98+ * @param selector The selector for the element that the locator function should search for.
99+ * @return An asynchronous locator function that searches for elements with the given selector,
100+ * and either finds one or throws an error
101+ */
27102 requiredLocator ( selector : string ) : AsyncFn < TestElement > ;
28- requiredLocator < T extends ComponentHarness > ( harness : ComponentHarnessConstructor < T > ) : AsyncFn < T > ;
103+
104+ /**
105+ * Creates an asynchronous locator function that can be used to find a `ComponentHarness` for a
106+ * component matching the given harness type under the root element of this `LocatorFactory`.
107+ * When the resulting locator function is invoked, if multiple matching components are found, a
108+ * harness for the first one is returned. If no components are found, an error is thrown.
109+ * @param harnessType The type of harness to search for.
110+ * @return An asynchronous locator function that searches components matching the given harness
111+ * type, and either returns a `ComponentHarness` for the component, or throws an error.
112+ */
113+ requiredLocator < T extends ComponentHarness > ( harnessType : ComponentHarnessConstructor < T > ) :
114+ AsyncFn < T > ;
115+
116+ /**
117+ * Creates an asynchronous locator function that can be used to search for elements with the given
118+ * selector under the root element of this `LocatorFactory`. When the resulting locator function
119+ * is invoked, if multiple matching elements are found, the first element is returned. If no
120+ * elements are found, null is returned.
121+ * @param selector The selector for the element that the locator function should search for.
122+ * @return An asynchronous locator function that searches for elements with the given selector,
123+ * and either finds one or returns null.
124+ */
29125 optionalLocator ( selector : string ) : AsyncFn < TestElement | null > ;
30- optionalLocator < T extends ComponentHarness > ( harness : ComponentHarnessConstructor < T > ) :
126+
127+ /**
128+ * Creates an asynchronous locator function that can be used to find a `ComponentHarness` for a
129+ * component matching the given harness type under the root element of this `LocatorFactory`.
130+ * When the resulting locator function is invoked, if multiple matching components are found, a
131+ * harness for the first one is returned. If no components are found, null is returned.
132+ * @param harnessType The type of harness to search for.
133+ * @return An asynchronous locator function that searches components matching the given harness
134+ * type, and either returns a `ComponentHarness` for the component, or null if none is found.
135+ */
136+ optionalLocator < T extends ComponentHarness > ( harnessType : ComponentHarnessConstructor < T > ) :
31137 AsyncFn < T | null > ;
138+
139+ /**
140+ * Creates an asynchronous locator function that can be used to search for a list of elements with
141+ * the given selector under the root element of this `LocatorFactory`. When the resulting locator
142+ * function is invoked, a list of matching elements is returned.
143+ * @param selector The selector for the element that the locator function should search for.
144+ * @return An asynchronous locator function that searches for elements with the given selector,
145+ * and either finds one or throws an error
146+ */
32147 allLocator ( selector : string ) : AsyncFn < TestElement [ ] > ;
33- allLocator < T extends ComponentHarness > ( harness : ComponentHarnessConstructor < T > ) : AsyncFn < T [ ] > ;
148+
149+ /**
150+ * Creates an asynchronous locator function that can be used to find a list of
151+ * `ComponentHarness`es for all components matching the given harness type under the root element
152+ * of this `LocatorFactory`. When the resulting locator function is invoked, a list of
153+ * `ComponentHarness`es for the matching components is returned.
154+ * @param harnessType The type of harness to search for.
155+ * @return An asynchronous locator function that searches components matching the given harness
156+ * type, and returns a list of `ComponentHarness`es.
157+ */
158+ allLocator < T extends ComponentHarness > ( harnessType : ComponentHarnessConstructor < T > ) : AsyncFn < T [ ] > ;
34159}
35160
36161/**
37- * Base Component Harness
38- * This base component harness provides the basic ability to locate element and
39- * sub-component harness. It should be inherited when defining user's own
40- * harness.
162+ * Base class for component harnesses that all component harness authors should extend. This base
163+ * component harness provides the basic ability to locate element and sub-component harness. It
164+ * should be inherited when defining user's own harness.
41165 */
42166export abstract class ComponentHarness {
43167 constructor ( private readonly locatorFacotry : LocatorFactory ) { }
44168
45- async host ( ) {
169+ /** Gets a `Promise` for the `TestElement` representing the host element of the component. */
170+ async host ( ) : Promise < TestElement > {
46171 return this . locatorFacotry . rootElement ( ) ;
47172 }
48173
174+ /**
175+ * Gets a `LocatorFactory` for the document root element. This factory can be used to create
176+ * locators for elements that a component creates outside of its own root element. (e.g. by
177+ * appending to document.body).
178+ */
49179 protected documentRootLocatorFactory ( ) : LocatorFactory {
50180 return this . locatorFacotry . documentRootLocatorFactory ( ) ;
51181 }
52182
183+ /**
184+ * Creates an asynchronous locator function that can be used to search for elements with the given
185+ * selector under the host element of this `ComponentHarness`. When the resulting locator function
186+ * is invoked, if multiple matching elements are found, the first element is returned. If no
187+ * elements are found, an error is thrown.
188+ * @param selector The selector for the element that the locator function should search for.
189+ * @return An asynchronous locator function that searches for elements with the given selector,
190+ * and either finds one or throws an error
191+ */
53192 protected requiredLocator ( selector : string ) : AsyncFn < TestElement > ;
54- protected requiredLocator < T extends ComponentHarness > ( harness : ComponentHarnessConstructor < T > ) :
55- AsyncFn < T > ;
193+
194+ /**
195+ * Creates an asynchronous locator function that can be used to find a `ComponentHarness` for a
196+ * component matching the given harness type under the host element of this `ComponentHarness`.
197+ * When the resulting locator function is invoked, if multiple matching components are found, a
198+ * harness for the first one is returned. If no components are found, an error is thrown.
199+ * @param harnessType The type of harness to search for.
200+ * @return An asynchronous locator function that searches components matching the given harness
201+ * type, and either returns a `ComponentHarness` for the component, or throws an error.
202+ */
203+ protected requiredLocator < T extends ComponentHarness > (
204+ harnessType : ComponentHarnessConstructor < T > ) : AsyncFn < T > ;
205+
56206 protected requiredLocator ( arg : any ) : any {
57207 return this . locatorFacotry . requiredLocator ( arg ) ;
58208 }
59209
210+ /**
211+ * Creates an asynchronous locator function that can be used to search for elements with the given
212+ * selector under the host element of this `ComponentHarness`. When the resulting locator function
213+ * is invoked, if multiple matching elements are found, the first element is returned. If no
214+ * elements are found, null is returned.
215+ * @param selector The selector for the element that the locator function should search for.
216+ * @return An asynchronous locator function that searches for elements with the given selector,
217+ * and either finds one or returns null.
218+ */
60219 protected optionalLocator ( selector : string ) : AsyncFn < TestElement | null > ;
61- protected optionalLocator < T extends ComponentHarness > ( harness : ComponentHarnessConstructor < T > ) :
62- AsyncFn < T | null > ;
220+
221+ /**
222+ * Creates an asynchronous locator function that can be used to find a `ComponentHarness` for a
223+ * component matching the given harness type under the host element of this `ComponentHarness`.
224+ * When the resulting locator function is invoked, if multiple matching components are found, a
225+ * harness for the first one is returned. If no components are found, null is returned.
226+ * @param harnessType The type of harness to search for.
227+ * @return An asynchronous locator function that searches components matching the given harness
228+ * type, and either returns a `ComponentHarness` for the component, or null if none is found.
229+ */
230+ protected optionalLocator < T extends ComponentHarness > (
231+ harnessType : ComponentHarnessConstructor < T > ) : AsyncFn < T | null > ;
232+
63233 protected optionalLocator ( arg : any ) : any {
64234 return this . locatorFacotry . optionalLocator ( arg ) ;
65235 }
66236
237+ /**
238+ * Creates an asynchronous locator function that can be used to search for a list of elements with
239+ * the given selector under the host element of this `ComponentHarness`. When the resulting
240+ * locator function is invoked, a list of matching elements is returned.
241+ * @param selector The selector for the element that the locator function should search for.
242+ * @return An asynchronous locator function that searches for elements with the given selector,
243+ * and either finds one or throws an error
244+ */
67245 protected allLocator ( selector : string ) : AsyncFn < TestElement [ ] > ;
68- protected allLocator < T extends ComponentHarness > ( harness : ComponentHarnessConstructor < T > ) :
246+
247+ /**
248+ * Creates an asynchronous locator function that can be used to find a list of
249+ * `ComponentHarness`es for all components matching the given harness type under the host element
250+ * of this `ComponentHarness`. When the resulting locator function is invoked, a list of
251+ * `ComponentHarness`es for the matching components is returned.
252+ * @param harnessType The type of harness to search for.
253+ * @return An asynchronous locator function that searches components matching the given harness
254+ * type, and returns a list of `ComponentHarness`es.
255+ */
256+ protected allLocator < T extends ComponentHarness > ( harnessType : ComponentHarnessConstructor < T > ) :
69257 AsyncFn < T [ ] > ;
258+
70259 protected allLocator ( arg : any ) : any {
71260 return this . locatorFacotry . allLocator ( arg ) ;
72261 }
@@ -76,5 +265,10 @@ export abstract class ComponentHarness {
76265export interface ComponentHarnessConstructor < T extends ComponentHarness > {
77266 new ( locatorFactory : LocatorFactory ) : T ;
78267
268+ /**
269+ * `ComponentHarness` subclasses must specify a static `hostSelector` property that is used to
270+ * find the host element for the corresponding component. This property should match the selector
271+ * for the Angular component.
272+ */
79273 hostSelector : string ;
80274}
0 commit comments