Skip to content

Commit cc25517

Browse files
committed
add tsdoc comments
1 parent 7fac8a3 commit cc25517

File tree

6 files changed

+255
-27
lines changed

6 files changed

+255
-27
lines changed

src/cdk-experimental/testing/component-harness.ts

Lines changed: 211 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,65 +8,254 @@
88

99
import {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. */
1212
export 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+
*/
1418
export 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+
*/
2486
export 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
*/
42166
export 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 {
76265
export 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
}

src/cdk-experimental/testing/harness-environment.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,24 @@ import {
1515
} from './component-harness';
1616
import {TestElement} from './test-element';
1717

18-
/** Interface that is used to find elements in the DOM and create harnesses for them. */
19-
export abstract class AbstractHarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
18+
/**
19+
* Base harness environment class that can be extended to allow `ComponentHarness`es to be used in
20+
* different test environments (e.g. testbed, protractor, etc.). This class implements the
21+
* functionality of both a `HarnessLoader` and `LocatorFactory`. This class is generic on the raw
22+
* element type, `E`, used by the particular test environment.
23+
*/
24+
export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
2025
protected constructor(protected rawRootElement: E) {}
2126

27+
// Part of the `HarnessLoader` interface, delegated to concrete implementation.
2228
abstract documentRootLocatorFactory(): LocatorFactory;
2329

30+
// Implemented as part of the `LocatorFactory` interface.
2431
rootElement(): TestElement {
2532
return this.createTestElement(this.rawRootElement);
2633
}
2734

35+
// Implemented as part of the `LocatorFactory` interface.
2836
requiredLocator(selector: string): AsyncFn<TestElement>;
2937
requiredLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T>;
3038
requiredLocator<T extends ComponentHarness>(
@@ -46,6 +54,7 @@ export abstract class AbstractHarnessEnvironment<E> implements HarnessLoader, Lo
4654
};
4755
}
4856

57+
// Implemented as part of the `LocatorFactory` interface.
4958
optionalLocator(selector: string): AsyncFn<TestElement | null>;
5059
optionalLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
5160
AsyncFn<T | null>;
@@ -62,6 +71,7 @@ export abstract class AbstractHarnessEnvironment<E> implements HarnessLoader, Lo
6271
};
6372
}
6473

74+
// Implemented as part of the `LocatorFactory` interface.
6575
allLocator(selector: string): AsyncFn<TestElement[]>;
6676
allLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T[]>;
6777
allLocator<T extends ComponentHarness>(
@@ -76,19 +86,23 @@ export abstract class AbstractHarnessEnvironment<E> implements HarnessLoader, Lo
7686
};
7787
}
7888

89+
// Implemented as part of the `HarnessLoader` interface.
7990
requiredHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T> {
8091
return this.requiredLocator(harness)();
8192
}
8293

94+
// Implemented as part of the `HarnessLoader` interface.
8395
optionalHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
8496
Promise<T | null> {
8597
return this.optionalLocator(harness)();
8698
}
8799

100+
// Implemented as part of the `HarnessLoader` interface.
88101
allHarnesses<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T[]> {
89102
return this.allLocator(harness)();
90103
}
91104

105+
// Implemented as part of the `HarnessLoader` interface.
92106
async findRequired(selector: string): Promise<HarnessLoader> {
93107
const element = await this.getRawElement(selector);
94108
if (element) {
@@ -97,23 +111,35 @@ export abstract class AbstractHarnessEnvironment<E> implements HarnessLoader, Lo
97111
throw Error(`Expected to find element matching selector: "${selector}", but none was found`);
98112
}
99113

114+
// Implemented as part of the `HarnessLoader` interface.
100115
async findOptional(selector: string): Promise<HarnessLoader | null> {
101116
const element = await this.getRawElement(selector);
102117
return element ? this.createHarnessLoader(element) : null;
103118
}
104119

120+
// Implemented as part of the `HarnessLoader` interface.
105121
async findAll(selector: string): Promise<HarnessLoader[]> {
106122
return (await this.getAllRawElements(selector)).map(e => this.createHarnessLoader(e));
107123
}
108124

125+
/** Creates a `TestElement` from a raw element. */
109126
protected abstract createTestElement(element: E): TestElement;
110127

128+
/** Creates a `ComponentHarness` for the given harness type with the given raw host element. */
111129
protected abstract createComponentHarness<T extends ComponentHarness>(
112130
harnessType: ComponentHarnessConstructor<T>, element: E): T;
113131

132+
/** Creates a `HarnessLoader` rooted at the given raw element. */
114133
protected abstract createHarnessLoader(element: E): HarnessLoader;
115134

135+
/**
136+
* Gets the first element matching the given selector under this environment's root element, or
137+
* null if no elements match.
138+
*/
116139
protected abstract getRawElement(selector: string): Promise<E | null>;
117140

141+
/**
142+
* Gets a list of all elements matching the given selector under this environment's root element.
143+
*/
118144
protected abstract getAllRawElements(selector: string): Promise<E[]>;
119145
}

src/cdk-experimental/testing/protractor/protractor-element.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {browser, ElementFinder} from 'protractor';
1010
import {TestElement} from '../test-element';
1111

12+
/** A `TestElement` implementation for Protractor. */
1213
export class ProtractorElement implements TestElement {
1314
constructor(readonly element: ElementFinder) {}
1415

0 commit comments

Comments
 (0)