Skip to content

Commit 8c2ed70

Browse files
committed
Add setDefaultWaitOptions method
1 parent 73518f9 commit 8c2ed70

File tree

11 files changed

+130
-17
lines changed

11 files changed

+130
-17
lines changed

.all-contributorsrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,18 @@
566566
"bug",
567567
"review"
568568
]
569+
},
570+
{
571+
"login": "orokanasaru",
572+
"name": "Michael Hensler",
573+
"avatar_url": "https://avatars.githubusercontent.com/u/5751627?v=4",
574+
"profile": "https://github.com/orokanasaru",
575+
"contributions": [
576+
"code",
577+
"doc",
578+
"ideas",
579+
"test"
580+
]
569581
}
570582
],
571583
"skipCi": true,

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
246246
<td align="center"><a href="https://matan.io"><img src="https://avatars.githubusercontent.com/u/12711091?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Matan Borenkraout</b></sub></a><br /><a href="#maintenance-MatanBobi" title="Maintenance">🚧</a></td>
247247
<td align="center"><a href="https://github.com/andyrooger"><img src="https://avatars.githubusercontent.com/u/420834?v=4?s=100" width="100px;" alt=""/><br /><sub><b>andyrooger</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=andyrooger" title="Code">💻</a></td>
248248
<td align="center"><a href="https://github.com/bdwain"><img src="https://avatars.githubusercontent.com/u/3982094?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Bryan Wain</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/issues?q=author%3Abdwain" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/react-hooks-testing-library/pulls?q=is%3Apr+reviewed-by%3Abdwain" title="Reviewed Pull Requests">👀</a></td>
249+
<td align="center"><a href="https://github.com/orokanasaru"><img src="https://avatars.githubusercontent.com/u/5751627?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michael Hensler</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=orokanasaru" title="Code">💻</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=orokanasaru" title="Documentation">📖</a> <a href="#ideas-orokanasaru" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=orokanasaru" title="Tests">⚠️</a></td>
249250
</tr>
250251
</table>
251252

docs/api-reference.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,30 @@ _Default: 1000_
271271

272272
The maximum amount of time in milliseconds (ms) to wait.
273273

274+
### `setDefaultWaitOptions`
275+
276+
```ts
277+
function setDefaultWaitOptions({
278+
interval?: number | false
279+
timeout?: number | false
280+
}): void
281+
```
282+
283+
Updates the default values for `interval` and `timeout` used by the `waitFor*` functions.
284+
285+
#### `interval`
286+
287+
_Default: 50_
288+
289+
The amount of time in milliseconds (ms) to wait between checks of the callback if no renders occur.
290+
Interval checking is disabled if `interval` is not provided as a `falsy`.
291+
292+
#### `timeout`
293+
294+
_Default: 1000_
295+
296+
The maximum amount of time in milliseconds (ms) to wait.
297+
274298
---
275299

276300
## `console.error`

src/__tests__/asyncHook.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import { setDefaultWaitOptions } from 'core'
12
import { useState, useRef, useEffect } from 'react'
23

34
describe('async hook tests', () => {
5+
beforeEach(() => {
6+
setDefaultWaitOptions({ interval: 50, timeout: 1000 })
7+
})
8+
49
const useSequence = (values: string[], intervalMs = 50) => {
510
const [first, ...otherValues] = values
611
const [value, setValue] = useState(() => first)
@@ -59,6 +64,17 @@ describe('async hook tests', () => {
5964
)
6065
})
6166

67+
test('should reject if custom default timeout exceeded when waiting for next update', async () => {
68+
setDefaultWaitOptions({ timeout: 10 })
69+
const { result, waitForNextUpdate } = renderHook(() => useSequence(['first', 'second']))
70+
71+
expect(result.current).toBe('first')
72+
73+
await expect(waitForNextUpdate()).rejects.toThrow(
74+
Error('Timed out in waitForNextUpdate after 10ms.')
75+
)
76+
})
77+
6278
test('should not reject when waiting for next update if timeout has been disabled', async () => {
6379
const { result, waitForNextUpdate } = renderHook(() => useSequence(['first', 'second'], 1100))
6480

@@ -185,6 +201,20 @@ describe('async hook tests', () => {
185201
expect(checks).toBe(3)
186202
})
187203

204+
test('should check on custom default interval when waiting for expectation to pass', async () => {
205+
setDefaultWaitOptions({ interval: 100 })
206+
const { result, waitFor } = renderHook(() => useSequence(['first', 'second', 'third']))
207+
208+
let checks = 0
209+
210+
await waitFor(() => {
211+
checks++
212+
return result.current === 'third'
213+
})
214+
215+
expect(checks).toBe(3)
216+
})
217+
188218
test('should wait for value to change', async () => {
189219
const { result, waitForValueToChange } = renderHook(() =>
190220
useSequence(['first', 'second', 'third'])

src/core/asyncUtils.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import {
1010
import { resolveAfter, callAfter } from '../helpers/promises'
1111
import { TimeoutError } from '../helpers/error'
1212

13-
const DEFAULT_INTERVAL = 50
14-
const DEFAULT_TIMEOUT = 1000
13+
let defaultInterval: number | false = 50
14+
let defaultTimeout: number | false = 1000
15+
16+
function setDefaultWaitOptions({ interval, timeout }: WaitOptions) {
17+
defaultInterval = interval ?? defaultInterval
18+
defaultTimeout = timeout ?? defaultTimeout
19+
}
1520

1621
function asyncUtils(act: Act, addResolver: (callback: () => void) => void): AsyncUtils {
1722
const wait = async (callback: () => boolean | void, { interval, timeout }: WaitOptions) => {
@@ -55,7 +60,7 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
5560

5661
const waitFor = async (
5762
callback: () => boolean | void,
58-
{ interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT }: WaitForOptions = {}
63+
{ interval = defaultInterval, timeout = defaultTimeout }: WaitForOptions = {}
5964
) => {
6065
const safeCallback = () => {
6166
try {
@@ -73,7 +78,7 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
7378

7479
const waitForValueToChange = async (
7580
selector: () => unknown,
76-
{ interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT }: WaitForValueToChangeOptions = {}
81+
{ interval = defaultInterval, timeout = defaultTimeout }: WaitForValueToChangeOptions = {}
7782
) => {
7883
const initialValue = selector()
7984

@@ -83,9 +88,7 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
8388
}
8489
}
8590

86-
const waitForNextUpdate = async ({
87-
timeout = DEFAULT_TIMEOUT
88-
}: WaitForNextUpdateOptions = {}) => {
91+
const waitForNextUpdate = async ({ timeout = defaultTimeout }: WaitForNextUpdateOptions = {}) => {
8992
let updated = false
9093
addResolver(() => {
9194
updated = true
@@ -104,4 +107,4 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
104107
}
105108
}
106109

107-
export { asyncUtils }
110+
export { asyncUtils, setDefaultWaitOptions }

src/core/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CreateRenderer, Renderer, RenderResult, RenderHookOptions } from '../types'
22

3-
import { asyncUtils } from './asyncUtils'
3+
import { asyncUtils, setDefaultWaitOptions } from './asyncUtils'
44
import { cleanup, addCleanup, removeCleanup } from './cleanup'
55
import { suppressErrorOutput } from './console'
66

@@ -82,4 +82,11 @@ function createRenderHook<
8282
return renderHook
8383
}
8484

85-
export { createRenderHook, cleanup, addCleanup, removeCleanup, suppressErrorOutput }
85+
export {
86+
createRenderHook,
87+
cleanup,
88+
addCleanup,
89+
removeCleanup,
90+
suppressErrorOutput,
91+
setDefaultWaitOptions
92+
}

src/dom/pure.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ const renderHook = createRenderHook(createDomRenderer)
3737

3838
export { renderHook, act }
3939

40-
export { cleanup, addCleanup, removeCleanup, suppressErrorOutput } from '../core'
40+
export {
41+
cleanup,
42+
addCleanup,
43+
removeCleanup,
44+
setDefaultWaitOptions,
45+
suppressErrorOutput
46+
} from '../core'
4147

4248
export * from '../types/react'

src/native/pure.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const renderHook = createRenderHook(createNativeRenderer)
3636

3737
export { renderHook, act }
3838

39-
export { cleanup, addCleanup, removeCleanup, suppressErrorOutput } from '../core'
39+
export {
40+
cleanup,
41+
addCleanup,
42+
removeCleanup,
43+
setDefaultWaitOptions,
44+
suppressErrorOutput
45+
} from '../core'
4046

4147
export * from '../types/react'

src/pure.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,24 @@ function getRenderer() {
3232
}
3333
}
3434

35-
const { renderHook, act, cleanup, addCleanup, removeCleanup, suppressErrorOutput } = getRenderer()
36-
37-
export { renderHook, act, cleanup, addCleanup, removeCleanup, suppressErrorOutput }
35+
const {
36+
renderHook,
37+
act,
38+
cleanup,
39+
addCleanup,
40+
removeCleanup,
41+
setDefaultWaitOptions,
42+
suppressErrorOutput
43+
} = getRenderer()
44+
45+
export {
46+
renderHook,
47+
act,
48+
cleanup,
49+
addCleanup,
50+
removeCleanup,
51+
setDefaultWaitOptions,
52+
suppressErrorOutput
53+
}
3854

3955
export * from './types/react'

src/server/pure.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ const renderHook = createRenderHook(createServerRenderer)
6161

6262
export { renderHook, act }
6363

64-
export { cleanup, addCleanup, removeCleanup, suppressErrorOutput } from '../core'
64+
export {
65+
cleanup,
66+
addCleanup,
67+
removeCleanup,
68+
setDefaultWaitOptions,
69+
suppressErrorOutput
70+
} from '../core'
6571

6672
export * from '../types/react'

src/types/react.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
RenderHookResult,
66
ServerRenderHookResult,
77
Act,
8-
CleanupCallback
8+
CleanupCallback,
9+
WaitOptions
910
} from '.'
1011

1112
export type WrapperComponent<TProps> = ComponentType<TProps>
@@ -27,6 +28,7 @@ export type ReactHooksRenderer = {
2728
cleanup: () => Promise<void>
2829
addCleanup: (callback: CleanupCallback) => () => void
2930
removeCleanup: (callback: CleanupCallback) => void
31+
setDefaultWaitOptions: (options: WaitOptions) => void
3032
suppressErrorOutput: () => () => void
3133
}
3234

0 commit comments

Comments
 (0)