@@ -13,6 +13,7 @@ import {
1313 waitForDomChange ,
1414 waitForElement ,
1515 waitForElementToBeRemoved ,
16+ wait ,
1617} from '@testing-library/dom' ;
1718import { RenderComponentOptions , RenderDirectiveOptions , RenderResult } from './models' ;
1819import { createSelectOptions , createType , tab } from './user-events' ;
@@ -111,11 +112,11 @@ export async function render<SutType, WrapperType = SutType>(
111112 detectChanges ( ) ;
112113 } ;
113114
114- let router = routes ? ( TestBed . get < Router > ( Router ) as Router ) : null ;
115- const zone = TestBed . get < NgZone > ( NgZone ) as NgZone ;
115+ let router = routes ? TestBed . inject ( Router ) : null ;
116+ const zone = TestBed . inject ( NgZone ) ;
116117 const navigate = async ( elementOrPath : Element | string , basePath = '' ) => {
117118 if ( ! router ) {
118- router = TestBed . get < Router > ( Router ) as Router ;
119+ router = TestBed . inject ( Router ) ;
119120 }
120121
121122 const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath . getAttribute ( 'href' ) ;
@@ -126,41 +127,75 @@ export async function render<SutType, WrapperType = SutType>(
126127 return result ;
127128 } ;
128129
129- function componentWaitForDomChange ( options ?: {
130- container ?: HTMLElement ;
131- timeout ?: number ;
132- mutationObserverOptions ?: MutationObserverInit ;
133- } ) : Promise < void > {
134- const interval = setInterval ( detectChanges , 10 ) ;
135- return waitForDomChange ( { container : fixture . nativeElement , ...options } ) . finally ( ( ) => clearInterval ( interval ) ) ;
130+ function componentWait (
131+ callback ,
132+ options : {
133+ container ?: HTMLElement ;
134+ timeout ?: number ;
135+ interval ?: number ;
136+ mutationObserverOptions ?: {
137+ subtree : boolean ;
138+ childList : boolean ;
139+ attributes : boolean ;
140+ characterData : boolean ;
141+ } ;
142+ } = { container : fixture . nativeElement , interval : 50 } ,
143+ ) : Promise < void > {
144+ const interval = setInterval ( detectChanges , options . interval ) ;
145+ return wait ( callback , options ) . finally ( ( ) => clearInterval ( interval ) ) ;
146+ }
147+
148+ function componentWaitForDomChange (
149+ options : {
150+ container ?: HTMLElement ;
151+ timeout ?: number ;
152+ interval ?: number ;
153+ mutationObserverOptions ?: {
154+ subtree : boolean ;
155+ childList : boolean ;
156+ attributes : boolean ;
157+ characterData : boolean ;
158+ } ;
159+ } = { container : fixture . nativeElement , interval : 50 } ,
160+ ) : Promise < void > {
161+ const interval = setInterval ( detectChanges , options . interval ) ;
162+ return waitForDomChange ( options ) . finally ( ( ) => clearInterval ( interval ) ) ;
136163 }
137164
138- function componentWaitForElement < Result > (
165+ function componentWaitForElement < Result = HTMLElement > (
139166 callback : ( ) => Result ,
140- options ? : {
167+ options : {
141168 container ?: HTMLElement ;
142169 timeout ?: number ;
143- mutationObserverOptions ?: MutationObserverInit ;
144- } ,
170+ interval ?: number ;
171+ mutationObserverOptions ?: {
172+ subtree : boolean ;
173+ childList : boolean ;
174+ attributes : boolean ;
175+ characterData : boolean ;
176+ } ;
177+ } = { container : fixture . nativeElement , interval : 50 } ,
145178 ) : Promise < Result > {
146- const interval = setInterval ( detectChanges , 10 ) ;
147- return waitForElement ( callback , { container : fixture . nativeElement , ...options } ) . finally ( ( ) =>
148- clearInterval ( interval ) ,
149- ) ;
179+ const interval = setInterval ( detectChanges , options . interval ) ;
180+ return waitForElement ( callback , options ) . finally ( ( ) => clearInterval ( interval ) ) ;
150181 }
151182
152- function componentWaitForElementToBeRemoved < Result > (
183+ function componentWaitForElementToBeRemoved < Result = HTMLElement > (
153184 callback : ( ) => Result ,
154- options ? : {
185+ options : {
155186 container ?: HTMLElement ;
156187 timeout ?: number ;
157- mutationObserverOptions ?: MutationObserverInit ;
158- } ,
188+ interval ?: number ;
189+ mutationObserverOptions ?: {
190+ subtree : boolean ;
191+ childList : boolean ;
192+ attributes : boolean ;
193+ characterData : boolean ;
194+ } ;
195+ } = { container : fixture . nativeElement , interval : 50 } ,
159196 ) : Promise < Result > {
160- const interval = setInterval ( detectChanges , 10 ) ;
161- return waitForElementToBeRemoved ( callback , { container : fixture . nativeElement , ...options } ) . finally ( ( ) =>
162- clearInterval ( interval ) ,
163- ) ;
197+ const interval = setInterval ( detectChanges , options . interval ) ;
198+ return waitForElementToBeRemoved ( callback , options ) . finally ( ( ) => clearInterval ( interval ) ) ;
164199 }
165200
166201 return {
@@ -174,6 +209,7 @@ export async function render<SutType, WrapperType = SutType>(
174209 type : createType ( eventsWithDetectChanges ) ,
175210 selectOptions : createSelectOptions ( eventsWithDetectChanges ) ,
176211 tab,
212+ wait : componentWait ,
177213 waitForDomChange : componentWaitForDomChange ,
178214 waitForElement : componentWaitForElement ,
179215 waitForElementToBeRemoved : componentWaitForElementToBeRemoved ,
0 commit comments