@@ -7,23 +7,29 @@ import { RouterTestingModule } from '@angular/router/testing';
7
7
import {
8
8
FireFunction ,
9
9
FireObject ,
10
- getQueriesForElement ,
11
- prettyDOM ,
10
+ getQueriesForElement as dtlGetQueriesForElement ,
11
+ prettyDOM as dtlPrettyDOM ,
12
12
waitFor as dtlWaitFor ,
13
13
waitForElementToBeRemoved as dtlWaitForElementToBeRemoved ,
14
14
fireEvent as dtlFireEvent ,
15
15
screen as dtlScreen ,
16
16
queries as dtlQueries ,
17
- waitForOptions ,
17
+ waitForOptions as dtlWaitForOptions ,
18
+ configure as dtlConfigure ,
18
19
} from '@testing-library/dom' ;
19
20
import { RenderComponentOptions , RenderDirectiveOptions , RenderResult } from './models' ;
20
21
import { createSelectOptions , createType , tab } from './user-events' ;
21
22
22
- @Component ( { selector : 'wrapper-component' , template : '' } )
23
- class WrapperComponent { }
24
-
25
23
const mountedFixtures = new Set < ComponentFixture < any > > ( ) ;
26
24
25
+ dtlConfigure ( {
26
+ eventWrapper : cb => {
27
+ const result = cb ( ) ;
28
+ detectChangesForMountedFixtures ( ) ;
29
+ return result ;
30
+ } ,
31
+ } ) ;
32
+
27
33
export async function render < ComponentType > (
28
34
component : Type < ComponentType > ,
29
35
renderOptions ?: RenderComponentOptions < ComponentType > ,
@@ -148,13 +154,16 @@ export async function render<SutType, WrapperType = SutType>(
148
154
return result ;
149
155
} ;
150
156
151
- function componentWaitFor < T > ( callback , options : waitForOptions = { container : fixture . nativeElement } ) : Promise < T > {
157
+ function componentWaitFor < T > (
158
+ callback ,
159
+ options : dtlWaitForOptions = { container : fixture . nativeElement } ,
160
+ ) : Promise < T > {
152
161
return waitForWrapper ( detectChanges , callback , options ) ;
153
162
}
154
163
155
164
function componentWaitForElementToBeRemoved < T > (
156
165
callback : ( ( ) => T ) | T ,
157
- options : waitForOptions = { container : fixture . nativeElement } ,
166
+ options : dtlWaitForOptions = { container : fixture . nativeElement } ,
158
167
) : Promise < T > {
159
168
return waitForElementToBeRemovedWrapper ( detectChanges , callback , options ) ;
160
169
}
@@ -168,14 +177,17 @@ export async function render<SutType, WrapperType = SutType>(
168
177
container : fixture . nativeElement ,
169
178
debug : ( element = fixture . nativeElement , maxLength , options ) =>
170
179
Array . isArray ( element )
171
- ? element . forEach ( e => console . log ( prettyDOM ( e , maxLength , options ) ) )
172
- : console . log ( prettyDOM ( element , maxLength , options ) ) ,
180
+ ? element . forEach ( e => console . log ( dtlPrettyDOM ( e , maxLength , options ) ) )
181
+ : console . log ( dtlPrettyDOM ( element , maxLength , options ) ) ,
173
182
type : createType ( eventsWithDetectChanges ) ,
174
183
selectOptions : createSelectOptions ( eventsWithDetectChanges ) ,
175
184
tab,
176
185
waitFor : componentWaitFor ,
177
186
waitForElementToBeRemoved : componentWaitForElementToBeRemoved ,
178
- ...replaceFindWithFindAndDetectChanges ( fixture . nativeElement , getQueriesForElement ( fixture . nativeElement , queries ) ) ,
187
+ ...replaceFindWithFindAndDetectChanges (
188
+ fixture . nativeElement ,
189
+ dtlGetQueriesForElement ( fixture . nativeElement , queries ) ,
190
+ ) ,
179
191
...eventsWithDetectChanges ,
180
192
} ;
181
193
}
@@ -244,7 +256,7 @@ function addAutoImports({ imports, routes }: Pick<RenderComponentOptions<any>, '
244
256
async function waitForWrapper < T > (
245
257
detectChanges : ( ) => void ,
246
258
callback : ( ) => T extends Promise < any > ? never : T ,
247
- options ?: waitForOptions ,
259
+ options ?: dtlWaitForOptions ,
248
260
) : Promise < T > {
249
261
return await dtlWaitFor ( ( ) => {
250
262
detectChanges ( ) ;
@@ -258,7 +270,7 @@ async function waitForWrapper<T>(
258
270
async function waitForElementToBeRemovedWrapper < T > (
259
271
detectChanges : ( ) => void ,
260
272
callback : ( ( ) => T ) | T ,
261
- options ?: waitForOptions ,
273
+ options ?: dtlWaitForOptions ,
262
274
) : Promise < T > {
263
275
let cb ;
264
276
if ( typeof callback !== 'function' ) {
@@ -298,6 +310,9 @@ if (typeof afterEach === 'function' && !process.env.ATL_SKIP_AUTO_CLEANUP) {
298
310
} ) ;
299
311
}
300
312
313
+ @Component ( { selector : 'wrapper-component' , template : '' } )
314
+ class WrapperComponent { }
315
+
301
316
/**
302
317
* Wrap findBy queries to poke the Angular change detection cycle
303
318
*/
@@ -329,7 +344,15 @@ function replaceFindWithFindAndDetectChanges<T>(container: HTMLElement, original
329
344
* Call detectChanges for all fixtures
330
345
*/
331
346
function detectChangesForMountedFixtures ( ) {
332
- mountedFixtures . forEach ( fixture => fixture . detectChanges ( ) ) ;
347
+ mountedFixtures . forEach ( fixture => {
348
+ try {
349
+ fixture . detectChanges ( ) ;
350
+ } catch ( err ) {
351
+ if ( ! err . message . startsWith ( 'ViewDestroyedError' ) ) {
352
+ throw err ;
353
+ }
354
+ }
355
+ } ) ;
333
356
}
334
357
335
358
/**
@@ -355,14 +378,14 @@ const screen = replaceFindWithFindAndDetectChanges(document.body, dtlScreen);
355
378
/**
356
379
* Re-export waitFor with patched waitFor
357
380
*/
358
- async function waitFor < T > ( callback : ( ) => T extends Promise < any > ? never : T , options ?: waitForOptions ) : Promise < T > {
381
+ async function waitFor < T > ( callback : ( ) => T extends Promise < any > ? never : T , options ?: dtlWaitForOptions ) : Promise < T > {
359
382
return waitForWrapper ( detectChangesForMountedFixtures , callback , options ) ;
360
383
}
361
384
362
385
/**
363
386
* Re-export waitForElementToBeRemoved with patched waitForElementToBeRemoved
364
387
*/
365
- async function waitForElementToBeRemoved < T > ( callback : ( ( ) => T ) | T , options ?: waitForOptions ) : Promise < T > {
388
+ async function waitForElementToBeRemoved < T > ( callback : ( ( ) => T ) | T , options ?: dtlWaitForOptions ) : Promise < T > {
366
389
return waitForElementToBeRemovedWrapper ( detectChangesForMountedFixtures , callback , options ) ;
367
390
}
368
391
0 commit comments