@@ -53,8 +53,7 @@ export async function render<SutType, WrapperType = SutType>(
53
53
providers = [ ] ,
54
54
schemas = [ ] ,
55
55
queries,
56
- template = undefined ,
57
- wrapper = WrapperComponent ,
56
+ wrapper = WrapperComponent as Type < WrapperType > ,
58
57
componentProperties = { } ,
59
58
componentProviders = [ ] ,
60
59
excludeComponentDeclaration = false ,
@@ -89,13 +88,13 @@ export async function render<SutType, WrapperType = SutType>(
89
88
await TestBed . compileComponents ( ) ;
90
89
91
90
componentProviders
92
- . reduce ( ( acc , provider ) => acc . concat ( provider ) , [ ] )
93
- . forEach ( ( p ) => {
91
+ . reduce ( ( acc , provider ) => acc . concat ( provider ) , [ ] as any [ ] )
92
+ . forEach ( ( p : any ) => {
94
93
const { provide, ...provider } = p ;
95
94
TestBed . overrideProvider ( provide , provider ) ;
96
95
} ) ;
97
96
98
- const componentContainer = createComponentFixture ( sut , { wrapper } ) ;
97
+ const componentContainer = createComponentFixture ( sut , wrapper ) ;
99
98
100
99
let fixture : ComponentFixture < SutType > ;
101
100
let detectChanges : ( ) => void ;
@@ -120,7 +119,7 @@ export async function render<SutType, WrapperType = SutType>(
120
119
121
120
let router = routes ? inject ( Router ) : null ;
122
121
const zone = inject ( NgZone ) ;
123
- const navigate = async ( elementOrPath : Element | string , basePath = '' ) => {
122
+ const navigate = async ( elementOrPath : Element | string , basePath = '' ) : Promise < boolean > => {
124
123
if ( ! router ) {
125
124
router = inject ( Router ) ;
126
125
}
@@ -139,16 +138,18 @@ export async function render<SutType, WrapperType = SutType>(
139
138
qp [ key ] = [ currentValue , value ] ;
140
139
}
141
140
return qp ;
142
- } , { } )
141
+ } , { } as Record < string , string | string [ ] > )
143
142
: undefined ;
144
143
145
- const navigateOptions : NavigationExtras = queryParams
144
+ const navigateOptions : NavigationExtras | undefined = queryParams
146
145
? {
147
146
queryParams,
148
147
}
149
148
: undefined ;
150
149
151
- const doNavigate = ( ) => ( navigateOptions ? router . navigate ( [ path ] , navigateOptions ) : router . navigate ( [ path ] ) ) ;
150
+ const doNavigate = ( ) => {
151
+ return navigateOptions ? router ?. navigate ( [ path ] , navigateOptions ) : router ?. navigate ( [ path ] ) ;
152
+ } ;
152
153
153
154
let result ;
154
155
@@ -159,21 +160,25 @@ export async function render<SutType, WrapperType = SutType>(
159
160
}
160
161
161
162
detectChanges ( ) ;
162
- return result ;
163
+ return result ?? false ;
163
164
} ;
164
165
165
166
return {
167
+ // @ts -ignore: fixture assigned
166
168
fixture,
167
169
detectChanges : ( ) => detectChanges ( ) ,
168
170
navigate,
169
171
rerender,
170
172
change,
173
+ // @ts -ignore: fixture assigned
171
174
debugElement : typeof sut === 'string' ? fixture . debugElement : fixture . debugElement . query ( By . directive ( sut ) ) ,
175
+ // @ts -ignore: fixture assigned
172
176
container : fixture . nativeElement ,
173
177
debug : ( element = fixture . nativeElement , maxLength , options ) =>
174
178
Array . isArray ( element )
175
179
? element . forEach ( ( e ) => console . log ( dtlPrettyDOM ( e , maxLength , options ) ) )
176
180
: console . log ( dtlPrettyDOM ( element , maxLength , options ) ) ,
181
+ // @ts -ignore: fixture assigned
177
182
...replaceFindWithFindAndDetectChanges ( dtlGetQueriesForElement ( fixture . nativeElement , queries ) ) ,
178
183
} ;
179
184
@@ -220,9 +225,9 @@ async function createComponent<SutType>(component: Type<SutType>): Promise<Compo
220
225
return TestBed . createComponent ( component ) ;
221
226
}
222
227
223
- function createComponentFixture < SutType > (
228
+ function createComponentFixture < SutType , WrapperType > (
224
229
sut : Type < SutType > | string ,
225
- { wrapper } : Pick < RenderTemplateOptions < SutType > , 'wrapper' > ,
230
+ wrapper : Type < WrapperType > ,
226
231
) : Type < any > {
227
232
if ( typeof sut === 'string' ) {
228
233
TestBed . overrideTemplate ( wrapper , sut ) ;
@@ -236,13 +241,13 @@ function setComponentProperties<SutType>(
236
241
{ componentProperties = { } } : Pick < RenderTemplateOptions < SutType , any > , 'componentProperties' > ,
237
242
) {
238
243
for ( const key of Object . keys ( componentProperties ) ) {
239
- const descriptor : PropertyDescriptor = Object . getOwnPropertyDescriptor (
240
- fixture . componentInstance . constructor . prototype ,
244
+ const descriptor = Object . getOwnPropertyDescriptor (
245
+ ( fixture . componentInstance as any ) . constructor . prototype ,
241
246
key ,
242
247
) ;
243
248
let _value = componentProperties [ key ] ;
244
249
const defaultGetter = ( ) => _value ;
245
- const extendedSetter = ( value ) => {
250
+ const extendedSetter = ( value : any ) => {
246
251
_value = value ;
247
252
descriptor ?. set ?. call ( fixture . componentInstance , _value ) ;
248
253
fixture . detectChanges ( ) ;
@@ -268,21 +273,21 @@ function hasOnChangesHook<SutType>(componentInstance: SutType): componentInstanc
268
273
) ;
269
274
}
270
275
271
- function getChangesObj < SutType > ( oldProps : Partial < SutType > | null , newProps : Partial < SutType > ) {
276
+ function getChangesObj < SutType extends Record < string , any > > ( oldProps : Partial < SutType > | null , newProps : Partial < SutType > ) {
272
277
const isFirstChange = oldProps === null ;
273
278
return Object . keys ( newProps ) . reduce < SimpleChanges > (
274
279
( changes , key ) => ( {
275
280
...changes ,
276
281
[ key ] : new SimpleChange ( isFirstChange ? null : oldProps [ key ] , newProps [ key ] , isFirstChange ) ,
277
282
} ) ,
278
- { } ,
283
+ { } as SutType ,
279
284
) ;
280
285
}
281
286
282
287
function addAutoDeclarations < SutType > (
283
288
sut : Type < SutType > | string ,
284
289
{
285
- declarations,
290
+ declarations = [ ] ,
286
291
excludeComponentDeclaration,
287
292
wrapper,
288
293
} : Pick < RenderTemplateOptions < any > , 'declarations' | 'excludeComponentDeclaration' | 'wrapper' > ,
@@ -295,7 +300,7 @@ function addAutoDeclarations<SutType>(
295
300
return [ ...declarations , ...components ( ) ] ;
296
301
}
297
302
298
- function addAutoImports ( { imports, routes } : Pick < RenderComponentOptions < any > , 'imports' | 'routes' > ) {
303
+ function addAutoImports ( { imports = [ ] , routes } : Pick < RenderComponentOptions < any > , 'imports' | 'routes' > ) {
299
304
const animations = ( ) => {
300
305
const animationIsDefined =
301
306
imports . indexOf ( NoopAnimationsModule ) > - 1 || imports . indexOf ( BrowserAnimationsModule ) > - 1 ;
@@ -341,19 +346,19 @@ async function waitForElementToBeRemovedWrapper<T>(
341
346
callback : ( ( ) => T ) | T ,
342
347
options ?: dtlWaitForOptions ,
343
348
) : Promise < void > {
344
- let cb ;
349
+ let cb : ( ( ) => T )
345
350
if ( typeof callback !== 'function' ) {
346
351
const elements = ( Array . isArray ( callback ) ? callback : [ callback ] ) as Element [ ] ;
347
352
const getRemainingElements = elements . map ( ( element ) => {
348
- let parent = element . parentElement ;
353
+ let parent = element . parentElement as Element
349
354
while ( parent . parentElement ) {
350
355
parent = parent . parentElement ;
351
356
}
352
357
return ( ) => ( parent . contains ( element ) ? element : null ) ;
353
358
} ) ;
354
- cb = ( ) => getRemainingElements . map ( ( c ) => c ( ) ) . filter ( Boolean ) ;
359
+ cb = ( ) => getRemainingElements . map ( ( c ) => c ( ) ) . find ( Boolean ) as unknown as T ;
355
360
} else {
356
- cb = callback ;
361
+ cb = callback as ( ( ) => T ) ;
357
362
}
358
363
359
364
return await dtlWaitForElementToBeRemoved ( ( ) => {
@@ -367,7 +372,7 @@ function cleanup() {
367
372
mountedFixtures . forEach ( cleanupAtFixture ) ;
368
373
}
369
374
370
- function cleanupAtFixture ( fixture ) {
375
+ function cleanupAtFixture ( fixture : ComponentFixture < any > ) {
371
376
fixture . destroy ( ) ;
372
377
373
378
if ( ! fixture . nativeElement . getAttribute ( 'ng-version' ) && fixture . nativeElement . parentNode === document . body ) {
@@ -394,25 +399,25 @@ class WrapperComponent {}
394
399
/**
395
400
* Wrap findBy queries to poke the Angular change detection cycle
396
401
*/
397
- function replaceFindWithFindAndDetectChanges < T > ( originalQueriesForContainer : T ) : T {
402
+ function replaceFindWithFindAndDetectChanges < T extends Record < string , any > > ( originalQueriesForContainer : T ) : T {
398
403
return Object . keys ( originalQueriesForContainer ) . reduce ( ( newQueries , key ) => {
399
404
const getByQuery = originalQueriesForContainer [ key . replace ( 'find' , 'get' ) ] ;
400
405
if ( key . startsWith ( 'find' ) && getByQuery ) {
401
- newQueries [ key ] = async ( text , options , waitOptions ) => {
406
+ newQueries [ key ] = async ( ...queryOptions : any [ ] ) => {
407
+ const waitOptions = queryOptions . pop ( )
402
408
// original implementation at https://github.com/testing-library/dom-testing-library/blob/main/src/query-helpers.js
403
- const result = await waitForWrapper (
409
+ return await waitForWrapper (
404
410
detectChangesForMountedFixtures ,
405
- ( ) => getByQuery ( text , options ) ,
411
+ ( ) => getByQuery ( ... queryOptions ) ,
406
412
waitOptions ,
407
413
) ;
408
- return result ;
409
414
} ;
410
415
} else {
411
416
newQueries [ key ] = originalQueriesForContainer [ key ] ;
412
417
}
413
418
414
419
return newQueries ;
415
- } , { } as T ) ;
420
+ } , { } as Record < string , any > ) as T ;
416
421
}
417
422
418
423
/**
@@ -422,7 +427,7 @@ function detectChangesForMountedFixtures() {
422
427
mountedFixtures . forEach ( ( fixture ) => {
423
428
try {
424
429
fixture . detectChanges ( ) ;
425
- } catch ( err ) {
430
+ } catch ( err : any ) {
426
431
if ( ! err . message . startsWith ( 'ViewDestroyedError' ) ) {
427
432
throw err ;
428
433
}
0 commit comments