@@ -7,23 +7,29 @@ import { RouterTestingModule } from '@angular/router/testing';
77import {
88 FireFunction ,
99 FireObject ,
10- getQueriesForElement ,
11- prettyDOM ,
10+ getQueriesForElement as dtlGetQueriesForElement ,
11+ prettyDOM as dtlPrettyDOM ,
1212 waitFor as dtlWaitFor ,
1313 waitForElementToBeRemoved as dtlWaitForElementToBeRemoved ,
1414 fireEvent as dtlFireEvent ,
1515 screen as dtlScreen ,
1616 queries as dtlQueries ,
17- waitForOptions ,
17+ waitForOptions as dtlWaitForOptions ,
18+ configure as dtlConfigure ,
1819} from '@testing-library/dom' ;
1920import { RenderComponentOptions , RenderDirectiveOptions , RenderResult } from './models' ;
2021import { createSelectOptions , createType , tab } from './user-events' ;
2122
22- @Component ( { selector : 'wrapper-component' , template : '' } )
23- class WrapperComponent { }
24-
2523const mountedFixtures = new Set < ComponentFixture < any > > ( ) ;
2624
25+ dtlConfigure ( {
26+ eventWrapper : cb => {
27+ const result = cb ( ) ;
28+ detectChangesForMountedFixtures ( ) ;
29+ return result ;
30+ } ,
31+ } ) ;
32+
2733export async function render < ComponentType > (
2834 component : Type < ComponentType > ,
2935 renderOptions ?: RenderComponentOptions < ComponentType > ,
@@ -148,13 +154,16 @@ export async function render<SutType, WrapperType = SutType>(
148154 return result ;
149155 } ;
150156
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 > {
152161 return waitForWrapper ( detectChanges , callback , options ) ;
153162 }
154163
155164 function componentWaitForElementToBeRemoved < T > (
156165 callback : ( ( ) => T ) | T ,
157- options : waitForOptions = { container : fixture . nativeElement } ,
166+ options : dtlWaitForOptions = { container : fixture . nativeElement } ,
158167 ) : Promise < T > {
159168 return waitForElementToBeRemovedWrapper ( detectChanges , callback , options ) ;
160169 }
@@ -168,14 +177,17 @@ export async function render<SutType, WrapperType = SutType>(
168177 container : fixture . nativeElement ,
169178 debug : ( element = fixture . nativeElement , maxLength , options ) =>
170179 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 ) ) ,
173182 type : createType ( eventsWithDetectChanges ) ,
174183 selectOptions : createSelectOptions ( eventsWithDetectChanges ) ,
175184 tab,
176185 waitFor : componentWaitFor ,
177186 waitForElementToBeRemoved : componentWaitForElementToBeRemoved ,
178- ...replaceFindWithFindAndDetectChanges ( fixture . nativeElement , getQueriesForElement ( fixture . nativeElement , queries ) ) ,
187+ ...replaceFindWithFindAndDetectChanges (
188+ fixture . nativeElement ,
189+ dtlGetQueriesForElement ( fixture . nativeElement , queries ) ,
190+ ) ,
179191 ...eventsWithDetectChanges ,
180192 } ;
181193}
@@ -244,7 +256,7 @@ function addAutoImports({ imports, routes }: Pick<RenderComponentOptions<any>, '
244256async function waitForWrapper < T > (
245257 detectChanges : ( ) => void ,
246258 callback : ( ) => T extends Promise < any > ? never : T ,
247- options ?: waitForOptions ,
259+ options ?: dtlWaitForOptions ,
248260) : Promise < T > {
249261 return await dtlWaitFor ( ( ) => {
250262 detectChanges ( ) ;
@@ -258,7 +270,7 @@ async function waitForWrapper<T>(
258270async function waitForElementToBeRemovedWrapper < T > (
259271 detectChanges : ( ) => void ,
260272 callback : ( ( ) => T ) | T ,
261- options ?: waitForOptions ,
273+ options ?: dtlWaitForOptions ,
262274) : Promise < T > {
263275 let cb ;
264276 if ( typeof callback !== 'function' ) {
@@ -298,6 +310,9 @@ if (typeof afterEach === 'function' && !process.env.ATL_SKIP_AUTO_CLEANUP) {
298310 } ) ;
299311}
300312
313+ @Component ( { selector : 'wrapper-component' , template : '' } )
314+ class WrapperComponent { }
315+
301316/**
302317 * Wrap findBy queries to poke the Angular change detection cycle
303318 */
@@ -329,7 +344,15 @@ function replaceFindWithFindAndDetectChanges<T>(container: HTMLElement, original
329344 * Call detectChanges for all fixtures
330345 */
331346function 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+ } ) ;
333356}
334357
335358/**
@@ -355,14 +378,14 @@ const screen = replaceFindWithFindAndDetectChanges(document.body, dtlScreen);
355378/**
356379 * Re-export waitFor with patched waitFor
357380 */
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 > {
359382 return waitForWrapper ( detectChangesForMountedFixtures , callback , options ) ;
360383}
361384
362385/**
363386 * Re-export waitForElementToBeRemoved with patched waitForElementToBeRemoved
364387 */
365- async function waitForElementToBeRemoved < T > ( callback : ( ( ) => T ) | T , options ?: waitForOptions ) : Promise < T > {
388+ async function waitForElementToBeRemoved < T > ( callback : ( ( ) => T ) | T , options ?: dtlWaitForOptions ) : Promise < T > {
366389 return waitForElementToBeRemovedWrapper ( detectChangesForMountedFixtures , callback , options ) ;
367390}
368391
0 commit comments