@@ -55,7 +55,8 @@ await waitFor(() => expect(mockAPI).toHaveBeenCalledTimes(1))
55
55
// ...
56
56
` ` `
57
57
58
- ` waitFor ` may run the callback a variable number of times .
58
+ ` waitFor ` may run the callback a number of times until the timeout is reached .
59
+ Note that the number of calls is constrained by the ` timeout ` and ` interval ` options .
59
60
60
61
This can be useful if you have a unit test that mocks API calls and you need to
61
62
wait for your mock promises to all resolve .
@@ -71,8 +72,7 @@ wait for are descendants of `container`.
71
72
The default ` interval ` is ` 50ms ` . However it will run your callback immediately
72
73
before starting the intervals .
73
74
74
- The default ` timeout ` is ` 1000ms ` which will keep you under
75
- [Jest ' s default timeout of `5000ms`](https://jestjs.io/docs/en/jest-object.html#jestsettimeouttimeout).
75
+ The default ` timeout ` is ` 1000ms ` .
76
76
77
77
The default ` onTimeout ` takes the error and appends the ` container ` ' s printed
78
78
state to the error message which should hopefully make it easier to track down
@@ -141,164 +141,3 @@ waitForElementToBeRemoved(() => getByText(/not here/i)).catch((err) =>
141
141
` ` `
142
142
143
143
The options object is forwarded to ` waitFor ` .
144
-
145
- ## Deprecated Methods
146
-
147
- `wait`, `waitForDomChange`, and `waitForElement` have been combined into the
148
- `waitFor` method.
149
-
150
- <details >
151
-
152
- <br />
153
-
154
- <summary >Deprecated Methods</summary >
155
-
156
- ### `wait`
157
-
158
- > (DEPRECATED , use ` waitFor ` instead )
159
-
160
- ```typescript
161
- function wait<T >(
162
- callback : () => void ,
163
- options ? : {
164
- container? : HTMLElement
165
- timeout? : number
166
- interval? : number
167
- mutationObserverOptions? : MutationObserverInit
168
- }
169
- ): Promise <T >
170
- ```
171
-
172
- Previously, wait was a wrapper around wait-for-expect and used polling instead
173
- of a MutationObserver to look for changes. It is now an alias to waitFor and
174
- will be removed in a future release.
175
-
176
- Unlike wait, the callback parameter is mandatory in waitFor. Although you can
177
- migrate an existing `wait()` call to `waitFor( () => {} )`, it is considered bad
178
- practice to use an empty callback because it will make the tests more fragile.
179
-
180
- ### `waitForDomChange`
181
-
182
- > (DEPRECATED , use ` waitFor ` instead )
183
-
184
- ```typescript
185
- function waitForDomChange<T >(options ? : {
186
- container? : HTMLElement
187
- timeout? : number
188
- mutationObserverOptions? : MutationObserverInit
189
- }): Promise <T >
190
- ```
191
-
192
- When in need to wait for the DOM to change you can use `waitForDomChange`. The
193
- `waitForDomChange` function is a small wrapper around the
194
- [`MutationObserver`](https : // developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
195
-
196
- Here is an example where the promise will be resolved because the container is
197
- changed :
198
-
199
- ` ` ` javascript
200
- const container = document.createElement('div')
201
- waitForDomChange({ container })
202
- .then(() => console.log('DOM changed!'))
203
- .catch((err) => console.log( ` Error you need to deal with : $ {err}` ))
204
- container.append(document.createElement('p'))
205
- // if 👆 was the only code affecting the container and it was not run,
206
- // waitForDomChange would throw an error
207
- ` ` `
208
-
209
- The promise will resolve with a
210
- [` mutationsList ` ](https : // developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver)
211
- which you can use to determine what kind of a change (or changes ) affected the
212
- container
213
-
214
- ` ` ` javascript
215
- const container = document.createElement('div')
216
- container.setAttribute('data-cool', 'true')
217
- waitForDomChange({ container }).then((mutationsList) => {
218
- const mutation = mutationsList[0]
219
- console.log(
220
- ` was cool : $ {mutation .oldValue}\ncurrently cool : $ {mutation .target .dataset .cool}`
221
- )
222
- })
223
- container.setAttribute('data-cool', 'false')
224
- /*
225
- logs:
226
- was cool: true
227
- currently cool: false
228
- */
229
- ` ` `
230
-
231
- The default ` container ` is the global ` document ` . Make sure the elements you
232
- wait for are descendants of ` container ` .
233
-
234
- The default ` timeout ` is ` 1000ms ` which will keep you under
235
- [Jest ' s default timeout of `5000ms`](https://jestjs.io/docs/en/jest-object.html#jestsettimeouttimeout).
236
-
237
- The default ` mutationObserverOptions ` is
238
- ` {subtree: true, childList: true, attributes: true, characterData: true} ` which
239
- will detect additions and removals of child elements (including text nodes ) in
240
- the ` container ` and any of its descendants . It will also detect attribute
241
- changes .
242
-
243
- ### ` waitForElement `
244
-
245
- > (DEPRECATED , use ` find* ` queries or ` waitFor ` )
246
-
247
- ` ` ` typescript
248
- function waitForElement<T>(
249
- callback: () => T,
250
- options?: {
251
- container?: HTMLElement
252
- timeout?: number
253
- mutationObserverOptions?: MutationObserverInit
254
- }
255
- ): Promise<T>
256
- ` ` `
257
-
258
- When in need to wait for DOM elements to appear , disappear , or change you can
259
- use ` waitForElement ` . The ` waitForElement ` function is a small wrapper around
260
- the
261
- [` MutationObserver ` ](https : // developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
262
-
263
- Here ' s a simple example:
264
-
265
- ` ` ` javascript
266
- // ...
267
- // Wait until the callback does not throw an error and returns a truthy value. In this case, that means
268
- // it'll wait until we can get a form control with a label that matches "username".
269
- // Previously, the difference from ` wait ` is that rather than running your callback on
270
- // an interval, it's run as soon as there are DOM changes in the container
271
- // and returns the value returned by the callback.
272
- const usernameElement = await waitForElement(
273
- () => getByLabelText(container, 'username'),
274
- { container }
275
- )
276
- usernameElement.value = 'chucknorris'
277
- // ...
278
- ` ` `
279
-
280
- You can also wait for multiple elements at once :
281
-
282
- ` ` ` javascript
283
- const [usernameElement, passwordElement] = await waitForElement(
284
- () => [
285
- getByLabelText(container, 'username'),
286
- getByLabelText(container, 'password'),
287
- ],
288
- { container }
289
- )
290
- ` ` `
291
-
292
- The default ` container ` is the global ` document ` . Make sure the elements you
293
- wait for will be attached to it , or set a different ` container ` .
294
-
295
- The default ` timeout ` is ` 4500ms ` which will keep you under
296
- [Jest ' s default timeout of `5000ms`](https://facebook.github.io/jest/docs/en/jest-object.html#jestsettimeouttimeout).
297
-
298
- The default ` mutationObserverOptions ` is
299
- ` {subtree: true, childList: true, attributes: true, characterData: true} ` which
300
- will detect additions and removals of child elements (including text nodes ) in
301
- the ` container ` and any of its descendants . It will also detect attribute
302
- changes .
303
-
304
- </details >
0 commit comments