@@ -3,29 +3,40 @@ import { DebugElement } from '@angular/core';
3
3
import { tick } from '@angular/core/testing' ;
4
4
import { By } from '@angular/platform-browser' ;
5
5
import { UiSuggestComponent } from '@uipath/angular/components/ui-suggest' ;
6
+
7
+ import { EventGenerator } from '../utilities/event-generator' ;
6
8
import {
7
- EventGenerator ,
8
- Key ,
9
- } from '@uipath/angular/testing' ;
9
+ FixtureTestingUtils ,
10
+ IStubEndpoint ,
11
+ } from '../utilities/fixture-testing-utils' ;
12
+ import { Key } from '../utilities/key' ;
13
+
14
+ export interface ISuggestTestingOptions {
15
+ debounce ?: number ;
16
+ }
10
17
11
- import { SUGGEST_DEBOUNCE } from './constants' ;
18
+ const DEFAULT_SUGGEST_TESTING_OPTIONS : ISuggestTestingOptions = {
19
+ debounce : 300 ,
20
+ } ;
12
21
13
22
export class SuggestUtils < T > {
14
23
dropdownSelector = '.ui-suggest-dropdown-item-list-container' ;
15
24
16
25
constructor (
17
- private _utils : IntegrationUtils < T > ,
18
- ) { }
26
+ private _utils : FixtureTestingUtils < T > ,
27
+ private _options = DEFAULT_SUGGEST_TESTING_OPTIONS ,
28
+ ) {
29
+ }
19
30
20
- openAndFlush = ( selector : string , httpRequest : Function ) => {
31
+ openAndFlush = ( selector : string , httpRequest : ( ) => void ) => {
21
32
this . _utils . click ( '.display' , this . _utils . getDebugElement ( selector ) ) ;
22
33
this . _utils . fixture . detectChanges ( ) ;
23
34
httpRequest ( ) ;
24
35
this . _utils . fixture . detectChanges ( ) ;
25
36
} ;
26
37
27
38
// eslint-disable-next-line complexity
28
- searchAndSelect = ( selector : string , httpRequest ?: Function , searchStr = '' , nth = 0 , debugEl ?: DebugElement ) => {
39
+ searchAndSelect = ( selector : string , httpRequest ?: ( ) => void , searchStr = '' , nth = 0 , debugEl ?: DebugElement ) => {
29
40
const suggest = this . _utils . getDebugElement ( selector , debugEl ) ;
30
41
const multiple = this . isMultiple ( selector ) ;
31
42
const strategy = this . getFetchStrategy ( selector , debugEl ) ;
@@ -66,7 +77,7 @@ export class SuggestUtils<T> {
66
77
}
67
78
68
79
this . _utils . setInput ( 'input' , searchStr , parentContainer ) ;
69
- tick ( SUGGEST_DEBOUNCE ) ;
80
+ tick ( this . _options . debounce ) ;
70
81
this . _utils . fixture . detectChanges ( ) ;
71
82
72
83
if ( httpRequest ) { httpRequest ( ) ; }
@@ -93,7 +104,8 @@ export class SuggestUtils<T> {
93
104
getFetchStrategy = ( selector : string , debugEl ?: DebugElement ) => {
94
105
const suggest = this . _utils . getDebugElement ( selector , debugEl ) ;
95
106
// maybe add a getter along the setter for fetchStrategy ?
96
- return ( suggest . componentInstance as UiSuggestComponent ) . _fetchStrategy$ ?. value ?? 'eager' ;
107
+ const fetchStrategyKey = '_fetchStrategy$' ;
108
+ return ( suggest . componentInstance as UiSuggestComponent ) [ fetchStrategyKey ] ?. value ?? 'eager' ;
97
109
} ;
98
110
99
111
selectNthItem = ( selector : string , nth = 0 , config ?: {
@@ -137,9 +149,14 @@ export class SuggestUtils<T> {
137
149
return listItem ;
138
150
} ;
139
151
140
- isMultiple = ( selector : string , debugEl ?: DebugElement ) => ! ! this . _utils . getNativeElement ( `${ selector } mat-chip-list` , debugEl ) ;
152
+ elementContains = ( suffix : string ) => ( selector : string , debugEl ?: DebugElement ) =>
153
+ ! ! this . _utils . getNativeElement ( `${ selector } ${ suffix } ` , debugEl ) ;
141
154
142
- isOpen = ( selector : string , debugEl ?: DebugElement ) => ! ! this . _utils . getNativeElement ( `${ selector } [aria-expanded="true"]` , debugEl ) ;
155
+ // eslint-disable-next-line @typescript-eslint/member-ordering
156
+ isMultiple = this . elementContains ( 'mat-chip-list' ) ;
157
+
158
+ // eslint-disable-next-line @typescript-eslint/member-ordering
159
+ isOpen = this . elementContains ( '[aria-expanded="true"]' ) ;
143
160
144
161
getValue = ( selector : string , debugEl = this . _utils . fixture . debugElement ) => {
145
162
if ( this . isMultiple ( selector ) ) {
@@ -156,87 +173,3 @@ export class SuggestUtils<T> {
156
173
clear = ( selector : string ) =>
157
174
this . _utils . getNativeElement ( `${ selector } [role=button].mat-icon` ) ?. dispatchEvent ( EventGenerator . click ) ;
158
175
}
159
-
160
- class KVPUtils < T > {
161
- constructor (
162
- private _utils : IntegrationUtils < T > ,
163
- ) { }
164
-
165
- /**
166
- * Creates a new key value pair and populates it with the specified values
167
- *
168
- * @param keySearchText key suggest text to be selected
169
- * @param valueSearchText value suggest text to be selected
170
- */
171
- addAndPopulateKVPInput = ( keySearchText : string , valueSearchText : string , keyHttpRequest ?: Function , valueHttpRequest ?: Function ) => {
172
- this . _utils . click ( '[data-cy=ui-kvp-add-new-entry]' ) ;
173
- this . _utils . fixture . detectChanges ( ) ;
174
-
175
- this . _utils . suggest . searchAndSelect ( this . _nthKeySuggestSelector ( 0 ) , keyHttpRequest , keySearchText ) ;
176
- this . _utils . fixture . detectChanges ( ) ;
177
- tick ( 1000 ) ;
178
- this . _utils . fixture . detectChanges ( ) ;
179
-
180
- this . _utils . suggest . searchAndSelect ( this . _nthValueSuggestSelector ( 0 ) , valueHttpRequest , valueSearchText ) ;
181
- this . _utils . fixture . detectChanges ( ) ;
182
- tick ( 1 ) ;
183
- this . _utils . fixture . detectChanges ( ) ;
184
-
185
- tick ( 1000 ) ;
186
- this . _utils . fixture . detectChanges ( ) ;
187
- } ;
188
-
189
- /**
190
- * Existing number of key value pairs.
191
- *
192
- * @param debugEl
193
- * @returns
194
- */
195
- currentKVPCount ( debugEl = this . _utils . fixture . debugElement ) {
196
- const selector = 'ui-key-value-input' ;
197
- return this . _utils . getAllDebugElements ( selector , debugEl ) . length ;
198
- }
199
-
200
- /**
201
- * Retrive a reference to the nth key suggest.
202
- * Index starts at 1.
203
- *
204
- * @param debugEl
205
- * @returns
206
- */
207
- nthKeySuggest ( index : number , debugEl = this . _utils . fixture . debugElement ) {
208
- const selector = this . _nthKeySuggestSelector ( index ) ;
209
- return this . _utils . getDebugElement ( selector , debugEl ) ;
210
- }
211
-
212
- /**
213
- * Retrive a reference to the nth value suggest.
214
- * Index starts at 1.
215
- *
216
- * @param debugEl
217
- * @returns
218
- */
219
- nthValueSuggest ( index : number , debugEl = this . _utils . fixture . debugElement ) {
220
- const selector = this . _nthValueSuggestSelector ( index ) ;
221
- return this . _utils . getDebugElement ( selector , debugEl ) ;
222
- }
223
-
224
- /**
225
- * Removes the nth key value pair.
226
- * Index starts at 1.
227
- *
228
- * @param debugEl
229
- * @returns
230
- */
231
- removeNthKVP ( index : number , debugEl = this . _utils . fixture . debugElement ) {
232
- const selector = this . _nthRemoveButtonSelector ( index ) ;
233
- this . _utils . click ( selector , debugEl ) ;
234
-
235
- tick ( 1000 ) ;
236
- this . _utils . fixture . detectChanges ( ) ;
237
- }
238
-
239
- private _nthKeySuggestSelector ( index : number ) { return `[data-cy=ui-kvp-input-nr-${ index } ] [data-cy=ui-kvp-key-suggest]` ; }
240
- private _nthValueSuggestSelector ( index : number ) { return `[data-cy=ui-kvp-input-nr-${ index } ] [data-cy=ui-kvp-value-suggest]` ; }
241
- private _nthRemoveButtonSelector ( index : number ) { return `[data-cy=ui-kvp-input-nr-${ index } ] [data-cy=ui-kvp-remove-button]` ; }
242
- }
0 commit comments