@@ -13,6 +13,7 @@ import {
13
13
waitForDomChange ,
14
14
waitForElement ,
15
15
waitForElementToBeRemoved ,
16
+ wait ,
16
17
} from '@testing-library/dom' ;
17
18
import { RenderComponentOptions , RenderDirectiveOptions , RenderResult } from './models' ;
18
19
import { createSelectOptions , createType , tab } from './user-events' ;
@@ -111,11 +112,11 @@ export async function render<SutType, WrapperType = SutType>(
111
112
detectChanges ( ) ;
112
113
} ;
113
114
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 ) ;
116
117
const navigate = async ( elementOrPath : Element | string , basePath = '' ) => {
117
118
if ( ! router ) {
118
- router = TestBed . get < Router > ( Router ) as Router ;
119
+ router = TestBed . inject ( Router ) ;
119
120
}
120
121
121
122
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath . getAttribute ( 'href' ) ;
@@ -126,41 +127,75 @@ export async function render<SutType, WrapperType = SutType>(
126
127
return result ;
127
128
} ;
128
129
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 : ( ) => void ,
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 ) ) ;
136
163
}
137
164
138
165
function componentWaitForElement < Result > (
139
166
callback : ( ) => Result ,
140
- options ? : {
167
+ options : {
141
168
container ?: HTMLElement ;
142
169
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 } ,
145
178
) : 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 ) ) ;
150
181
}
151
182
152
183
function componentWaitForElementToBeRemoved < Result > (
153
184
callback : ( ) => Result ,
154
- options ? : {
185
+ options : {
155
186
container ?: HTMLElement ;
156
187
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 } ,
159
196
) : 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 ) ) ;
164
199
}
165
200
166
201
return {
@@ -174,6 +209,7 @@ export async function render<SutType, WrapperType = SutType>(
174
209
type : createType ( eventsWithDetectChanges ) ,
175
210
selectOptions : createSelectOptions ( eventsWithDetectChanges ) ,
176
211
tab,
212
+ wait : componentWait ,
177
213
waitForDomChange : componentWaitForDomChange ,
178
214
waitForElement : componentWaitForElement ,
179
215
waitForElementToBeRemoved : componentWaitForElementToBeRemoved ,
0 commit comments