@@ -2,14 +2,14 @@ import {
2
2
ApplicationInitStatus ,
3
3
ChangeDetectorRef ,
4
4
Component ,
5
- isStandalone ,
6
5
NgZone ,
7
6
OnChanges ,
8
7
OutputRef ,
9
8
OutputRefSubscription ,
10
9
SimpleChange ,
11
10
SimpleChanges ,
12
11
Type ,
12
+ isStandalone ,
13
13
} from '@angular/core' ;
14
14
import { ComponentFixture , DeferBlockBehavior , DeferBlockState , TestBed , tick } from '@angular/core/testing' ;
15
15
import { BrowserAnimationsModule , NoopAnimationsModule } from '@angular/platform-browser/animations' ;
@@ -27,15 +27,16 @@ import {
27
27
waitForOptions as dtlWaitForOptions ,
28
28
within as dtlWithin ,
29
29
} from '@testing-library/dom' ;
30
+ import { getConfig } from './config' ;
30
31
import {
31
32
ComponentOverride ,
33
+ OutputRefKeysWithCallback ,
32
34
RenderComponentOptions ,
33
35
RenderResult ,
34
36
RenderTemplateOptions ,
35
37
OutputRefKeysWithCallback ,
36
38
Config ,
37
39
} from './models' ;
38
- import { getConfig } from './config' ;
39
40
40
41
type SubscribedOutput < T > = readonly [ key : keyof T , callback : ( v : any ) => void , subscription : OutputRefSubscription ] ;
41
42
@@ -72,7 +73,7 @@ export async function render<SutType, WrapperType = SutType>(
72
73
on = { } ,
73
74
componentProviders = [ ] ,
74
75
childComponentOverrides = [ ] ,
75
- componentImports : componentImports ,
76
+ componentImports,
76
77
excludeComponentDeclaration = false ,
77
78
routes = [ ] ,
78
79
removeAngularAttributes = false ,
@@ -119,12 +120,9 @@ export async function render<SutType, WrapperType = SutType>(
119
120
120
121
await TestBed . compileComponents ( ) ;
121
122
122
- componentProviders
123
- . reduce ( ( acc , provider ) => acc . concat ( provider ) , [ ] as any [ ] )
124
- . forEach ( ( p : any ) => {
125
- const { provide, ...provider } = p ;
126
- TestBed . overrideProvider ( provide , provider ) ;
127
- } ) ;
123
+ for ( const { provide, ...provider } of componentProviders ) {
124
+ TestBed . overrideProvider ( provide , provider ) ;
125
+ }
128
126
129
127
const componentContainer = createComponentFixture ( sut , wrapper ) ;
130
128
@@ -161,7 +159,9 @@ export async function render<SutType, WrapperType = SutType>(
161
159
let result ;
162
160
163
161
if ( zone ) {
164
- await zone . run ( ( ) => ( result = doNavigate ( ) ) ) ;
162
+ await zone . run ( ( ) => {
163
+ result = doNavigate ( ) ;
164
+ } ) ;
165
165
} else {
166
166
result = doNavigate ( ) ;
167
167
}
@@ -202,15 +202,17 @@ export async function render<SutType, WrapperType = SutType>(
202
202
if ( removeAngularAttributes ) {
203
203
createdFixture . nativeElement . removeAttribute ( 'ng-version' ) ;
204
204
const idAttribute = createdFixture . nativeElement . getAttribute ( 'id' ) ;
205
- if ( idAttribute && idAttribute . startsWith ( 'root' ) ) {
205
+ if ( idAttribute ? .startsWith ( 'root' ) ) {
206
206
createdFixture . nativeElement . removeAttribute ( 'id' ) ;
207
207
}
208
208
}
209
209
210
210
mountedFixtures . add ( createdFixture ) ;
211
211
212
212
let isAlive = true ;
213
- createdFixture . componentRef . onDestroy ( ( ) => ( isAlive = false ) ) ;
213
+ createdFixture . componentRef . onDestroy ( ( ) => {
214
+ isAlive = false ;
215
+ } ) ;
214
216
215
217
if ( hasOnChangesHook ( createdFixture . componentInstance ) && Object . keys ( properties ) . length > 0 ) {
216
218
const changes = getChangesObj ( null , componentProperties ) ;
@@ -321,10 +323,15 @@ export async function render<SutType, WrapperType = SutType>(
321
323
} ,
322
324
debugElement : fixture . debugElement ,
323
325
container : fixture . nativeElement ,
324
- debug : ( element = fixture . nativeElement , maxLength , options ) =>
325
- Array . isArray ( element )
326
- ? element . forEach ( ( e ) => console . log ( dtlPrettyDOM ( e , maxLength , options ) ) )
327
- : console . log ( dtlPrettyDOM ( element , maxLength , options ) ) ,
326
+ debug : ( element = fixture . nativeElement , maxLength , options ) => {
327
+ if ( Array . isArray ( element ) ) {
328
+ for ( const e of element ) {
329
+ console . log ( dtlPrettyDOM ( e , maxLength , options ) ) ;
330
+ }
331
+ } else {
332
+ console . log ( dtlPrettyDOM ( element , maxLength , options ) ) ;
333
+ }
334
+ } ,
328
335
...replaceFindWithFindAndDetectChanges ( dtlGetQueriesForElement ( fixture . nativeElement , queries ) ) ,
329
336
} ;
330
337
}
@@ -426,9 +433,11 @@ function overrideComponentImports<SutType>(sut: Type<SutType> | string, imports:
426
433
}
427
434
428
435
function overrideChildComponentProviders ( componentOverrides : ComponentOverride < any > [ ] ) {
429
- componentOverrides ?. forEach ( ( { component, providers } ) => {
430
- TestBed . overrideComponent ( component , { set : { providers } } ) ;
431
- } ) ;
436
+ if ( componentOverrides ) {
437
+ for ( const { component, providers } of componentOverrides ) {
438
+ TestBed . overrideComponent ( component , { set : { providers } } ) ;
439
+ }
440
+ }
432
441
}
433
442
434
443
function hasOnChangesHook < SutType > ( componentInstance : SutType ) : componentInstance is SutType & OnChanges {
@@ -442,13 +451,10 @@ function hasOnChangesHook<SutType>(componentInstance: SutType): componentInstanc
442
451
443
452
function getChangesObj ( oldProps : Record < string , any > | null , newProps : Record < string , any > ) {
444
453
const isFirstChange = oldProps === null ;
445
- return Object . keys ( newProps ) . reduce < SimpleChanges > (
446
- ( changes , key ) => ( {
447
- ...changes ,
448
- [ key ] : new SimpleChange ( isFirstChange ? null : oldProps [ key ] , newProps [ key ] , isFirstChange ) ,
449
- } ) ,
450
- { } as Record < string , any > ,
451
- ) ;
454
+ return Object . keys ( newProps ) . reduce < SimpleChanges > ( ( changes , key ) => {
455
+ changes [ key ] = new SimpleChange ( isFirstChange ? null : oldProps [ key ] , newProps [ key ] , isFirstChange ) ;
456
+ return changes ;
457
+ } , { } as Record < string , any > ) ;
452
458
}
453
459
454
460
function update < SutType > (
@@ -464,10 +470,12 @@ function update<SutType>(
464
470
const componentInstance = fixture . componentInstance as Record < string , any > ;
465
471
const simpleChanges : SimpleChanges = { } ;
466
472
467
- for ( const key of prevRenderedKeys ) {
468
- if ( ! partialUpdate && ! Object . prototype . hasOwnProperty . call ( newValues , key ) ) {
469
- simpleChanges [ key ] = new SimpleChange ( componentInstance [ key ] , undefined , false ) ;
470
- delete componentInstance [ key ] ;
473
+ if ( ! partialUpdate ) {
474
+ for ( const key of prevRenderedKeys ) {
475
+ if ( ! Object . prototype . hasOwnProperty . call ( newValues , key ) ) {
476
+ simpleChanges [ key ] = new SimpleChange ( componentInstance [ key ] , undefined , false ) ;
477
+ delete componentInstance [ key ] ;
478
+ }
471
479
}
472
480
}
473
481
@@ -647,15 +655,15 @@ function replaceFindWithFindAndDetectChanges<T extends Record<string, any>>(orig
647
655
* Call detectChanges for all fixtures
648
656
*/
649
657
function detectChangesForMountedFixtures ( ) {
650
- mountedFixtures . forEach ( ( fixture ) => {
658
+ for ( const fixture of mountedFixtures ) {
651
659
try {
652
660
fixture . detectChanges ( ) ;
653
661
} catch ( err : any ) {
654
662
if ( ! err . message . startsWith ( 'ViewDestroyedError' ) ) {
655
663
throw err ;
656
664
}
657
665
}
658
- } ) ;
666
+ }
659
667
}
660
668
661
669
/**
0 commit comments