6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- /**
10
- * Test Element interface
11
- * This is a wrapper of native element
12
- */
13
- export interface TestElement {
14
- blur ( ) : Promise < void > ;
15
- clear ( ) : Promise < void > ;
16
- click ( ) : Promise < void > ;
17
- focus ( ) : Promise < void > ;
18
- getCssValue ( property : string ) : Promise < string > ;
19
- hover ( ) : Promise < void > ;
20
- sendKeys ( keys : string ) : Promise < void > ;
21
- text ( ) : Promise < string > ;
22
- getAttribute ( name : string ) : Promise < string | null > ;
23
- }
9
+ import { TestElement } from './test-element' ;
24
10
25
- /**
26
- * Extra searching options used by searching functions
27
- *
28
- * @param allowNull Optional, whether the found element can be null. If
29
- * allowNull is set, the searching function will always try to fetch the element
30
- * at once. When the element cannot be found, the searching function should
31
- * return null if allowNull is set to true, throw an error if allowNull is set
32
- * to false. If allowNull is not set, the framework will choose the behaviors
33
- * that make more sense for each test type (e.g. for unit test, the framework
34
- * will make sure the element is not null; otherwise throw an error); however,
35
- * the internal behavior is not guaranteed and user should not rely on it. Note
36
- * that in most cases, you don't need to care about whether an element is
37
- * present when loading the element and don't need to set this parameter unless
38
- * you do want to check whether the element is present when calling the
39
- * searching function. e.g. you want to make sure some element is not there when
40
- * loading the element in order to check whether a "ngif" works well.
41
- *
42
- * @param global Optional. If global is set to true, the selector will match any
43
- * element on the page and is not limited to the root of the harness. If
44
- * global is unset or set to false, the selector will only find elements under
45
- * the current root.
46
- */
47
- export interface Options {
11
+ /** Options that can be specified when querying for an Element. */
12
+ export interface QueryOptions {
13
+ /**
14
+ * Whether the found element can be null. If allowNull is set, the searching function will always
15
+ * try to fetch the element at once. When the element cannot be found, the searching function
16
+ * should return null if allowNull is set to true, throw an error if allowNull is set to false.
17
+ * If allowNull is not set, the framework will choose the behaviors that make more sense for each
18
+ * test type (e.g. for unit test, the framework will make sure the element is not null; otherwise
19
+ * throw an error); however, the internal behavior is not guaranteed and user should not rely on
20
+ * it. Note that in most cases, you don't need to care about whether an element is present when
21
+ * loading the element and don't need to set this parameter unless you do want to check whether
22
+ * the element is present when calling the searching function. e.g. you want to make sure some
23
+ * element is not there when loading the element in order to check whether a "ngif" works well.
24
+ */
48
25
allowNull ?: boolean ;
26
+ /**
27
+ * If global is set to true, the selector will match any element on the page and is not limited to
28
+ * the root of the harness. If global is unset or set to false, the selector will only find
29
+ * elements under the current root.
30
+ */
49
31
global ?: boolean ;
50
32
}
51
33
52
- /**
53
- * Type narrowing of Options to allow the overloads of ComponentHarness.find to
54
- * return null only if allowNull is set to true.
55
- */
56
- interface OptionsWithAllowNullSet extends Options {
57
- allowNull : true ;
58
- }
59
-
60
- /**
61
- * Locator interface
62
- */
63
- export interface Locator {
34
+ /** Interface that is used to find elements in the DOM and create harnesses for them. */
35
+ export interface HarnessLocator {
64
36
/**
65
37
* Get the host element of locator.
66
38
*/
@@ -71,7 +43,7 @@ export interface Locator {
71
43
* @param selector The CSS selector of the test elements.
72
44
* @param options Optional, extra searching options
73
45
*/
74
- querySelector ( selector : string , options ?: Options ) : Promise < TestElement | null > ;
46
+ querySelector ( selector : string , options ?: QueryOptions ) : Promise < TestElement | null > ;
75
47
76
48
/**
77
49
* Search all matched test elements under current root by CSS selector.
@@ -86,16 +58,16 @@ export interface Locator {
86
58
* @param options Optional, extra searching options
87
59
*/
88
60
load < T extends ComponentHarness > (
89
- componentHarness : ComponentHarnessType < T > , root : string ,
90
- options ?: Options ) : Promise < T | null > ;
61
+ componentHarness : ComponentHarnessConstructor < T > , root : string ,
62
+ options ?: QueryOptions ) : Promise < T | null > ;
91
63
92
64
/**
93
65
* Load all Component Harnesses under current root.
94
66
* @param componentHarness Type of user customized harness.
95
67
* @param root CSS root selector of the new component harnesses.
96
68
*/
97
69
loadAll < T extends ComponentHarness > (
98
- componentHarness : ComponentHarnessType < T > , root : string ) : Promise < T [ ] > ;
70
+ componentHarness : ComponentHarnessConstructor < T > , root : string ) : Promise < T [ ] > ;
99
71
}
100
72
101
73
/**
@@ -104,8 +76,8 @@ export interface Locator {
104
76
* sub-component harness. It should be inherited when defining user's own
105
77
* harness.
106
78
*/
107
- export class ComponentHarness {
108
- constructor ( private readonly locator : Locator ) { }
79
+ export abstract class ComponentHarness {
80
+ constructor ( private readonly locator : HarnessLocator ) { }
109
81
110
82
/**
111
83
* Get the host element of component harness.
@@ -127,7 +99,7 @@ export class ComponentHarness {
127
99
* @param selector The CSS selector of the test element.
128
100
* @param options Extra searching options
129
101
*/
130
- protected find ( selector : string , options : OptionsWithAllowNullSet ) :
102
+ protected find ( selector : string , options : QueryOptions & { allowNull : true } ) :
131
103
( ) => Promise < TestElement | null > ;
132
104
133
105
/**
@@ -136,15 +108,15 @@ export class ComponentHarness {
136
108
* @param selector The CSS selector of the test element.
137
109
* @param options Extra searching options
138
110
*/
139
- protected find ( selector : string , options : Options ) : ( ) => Promise < TestElement > ;
111
+ protected find ( selector : string , options : QueryOptions ) : ( ) => Promise < TestElement > ;
140
112
141
113
/**
142
114
* Generate a function to find the first matched Component Harness.
143
115
* @param componentHarness Type of user customized harness.
144
116
* @param root CSS root selector of the new component harness.
145
117
*/
146
118
protected find < T extends ComponentHarness > (
147
- componentHarness : ComponentHarnessType < T > ,
119
+ componentHarness : ComponentHarnessConstructor < T > ,
148
120
root : string ) : ( ) => Promise < T > ;
149
121
150
122
/**
@@ -154,8 +126,8 @@ export class ComponentHarness {
154
126
* @param options Extra searching options
155
127
*/
156
128
protected find < T extends ComponentHarness > (
157
- componentHarness : ComponentHarnessType < T > , root : string ,
158
- options : OptionsWithAllowNullSet ) : ( ) => Promise < T | null > ;
129
+ componentHarness : ComponentHarnessConstructor < T > , root : string ,
130
+ options : QueryOptions & { allowNull : true } ) : ( ) => Promise < T | null > ;
159
131
160
132
/**
161
133
* Generate a function to find the first matched Component Harness.
@@ -164,16 +136,16 @@ export class ComponentHarness {
164
136
* @param options Extra searching options
165
137
*/
166
138
protected find < T extends ComponentHarness > (
167
- componentHarness : ComponentHarnessType < T > , root : string ,
168
- options : Options ) : ( ) => Promise < T > ;
139
+ componentHarness : ComponentHarnessConstructor < T > , root : string ,
140
+ options : QueryOptions ) : ( ) => Promise < T > ;
169
141
170
142
protected find < T extends ComponentHarness > (
171
- selectorOrComponentHarness : string | ComponentHarnessType < T > ,
172
- selectorOrOptions ?: string | Options ,
173
- options ?: Options ) : ( ) => Promise < TestElement | T | null > {
143
+ selectorOrComponentHarness : string | ComponentHarnessConstructor < T > ,
144
+ selectorOrOptions ?: string | QueryOptions ,
145
+ options ?: QueryOptions ) : ( ) => Promise < TestElement | T | null > {
174
146
if ( typeof selectorOrComponentHarness === 'string' ) {
175
147
const selector = selectorOrComponentHarness ;
176
- return ( ) => this . locator . querySelector ( selector , selectorOrOptions as Options ) ;
148
+ return ( ) => this . locator . querySelector ( selector , selectorOrOptions as QueryOptions ) ;
177
149
} else {
178
150
const componentHarness = selectorOrComponentHarness ;
179
151
const selector = selectorOrOptions as string ;
@@ -196,11 +168,11 @@ export class ComponentHarness {
196
168
* locate harnesses under the current root.
197
169
*/
198
170
protected findAll < T extends ComponentHarness > (
199
- componentHarness : ComponentHarnessType < T > ,
171
+ componentHarness : ComponentHarnessConstructor < T > ,
200
172
root : string ) : ( ) => Promise < T [ ] > ;
201
173
202
174
protected findAll < T extends ComponentHarness > (
203
- selectorOrComponentHarness : string | ComponentHarnessType < T > ,
175
+ selectorOrComponentHarness : string | ComponentHarnessConstructor < T > ,
204
176
root ?: string ) : ( ) => Promise < TestElement [ ] | T [ ] > {
205
177
if ( typeof selectorOrComponentHarness === 'string' ) {
206
178
const selector = selectorOrComponentHarness ;
@@ -212,9 +184,7 @@ export class ComponentHarness {
212
184
}
213
185
}
214
186
215
- /**
216
- * Type of ComponentHarness.
217
- */
218
- export interface ComponentHarnessType < T extends ComponentHarness > {
219
- new ( locator : Locator ) : T ;
187
+ /** Constructor for a ComponentHarness subclass. */
188
+ export interface ComponentHarnessConstructor < T extends ComponentHarness > {
189
+ new ( locator : HarnessLocator ) : T ;
220
190
}
0 commit comments