Skip to content

Commit d0d7943

Browse files
author
Kent C. Dodds
committed
feat: add asyncWrapper config
1 parent 393377a commit d0d7943

6 files changed

+33
-4
lines changed

src/config.js

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
// './queries' are query functions.
44
let config = {
55
testIdAttribute: 'data-testid',
6+
// this is to support React's async `act` function.
7+
// forcing react-testing-library to wrap all async functions would've been
8+
// a total nightmare (consider wrapping every findBy* query and then also
9+
// updating `within` so those would be wrapped too. Total nightmare).
10+
// so we have this config option that's really only intended for
11+
// react-testing-library to use. For that reason, this feature will remain
12+
// undocumented.
13+
asyncWrapper: cb => cb(),
614
}
715

816
export function configure(newConfig) {

src/wait-for-dom-change.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {newMutationObserver, getDocument, getSetImmediate} from './helpers'
2+
import {getConfig} from './config'
23

34
function waitForDomChange({
45
container = getDocument(),
@@ -36,4 +37,8 @@ function waitForDomChange({
3637
})
3738
}
3839

39-
export {waitForDomChange}
40+
function waitForDomChangeWrapper(...args) {
41+
return getConfig().asyncWrapper(() => waitForDomChange(...args))
42+
}
43+
44+
export {waitForDomChangeWrapper as waitForDomChange}

src/wait-for-element-to-be-removed.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {getDocument, getSetImmediate, newMutationObserver} from './helpers'
2+
import {getConfig} from './config'
23

34
function waitForElementToBeRemoved(
45
callback,
@@ -69,4 +70,8 @@ function waitForElementToBeRemoved(
6970
})
7071
}
7172

72-
export {waitForElementToBeRemoved}
73+
function waitForElementToBeRemovedWrapper(...args) {
74+
return getConfig().asyncWrapper(() => waitForElementToBeRemoved(...args))
75+
}
76+
77+
export {waitForElementToBeRemovedWrapper as waitForElementToBeRemoved}

src/wait-for-element.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {newMutationObserver, getDocument, getSetImmediate} from './helpers'
2+
import {getConfig} from './config'
23

34
function waitForElement(
45
callback,
@@ -54,4 +55,8 @@ function waitForElement(
5455
})
5556
}
5657

57-
export {waitForElement}
58+
function waitForElementWrapper(...args) {
59+
return getConfig().asyncWrapper(() => waitForElement(...args))
60+
}
61+
62+
export {waitForElementWrapper as waitForElement}

src/wait.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import waitForExpect from 'wait-for-expect'
2+
import {getConfig} from './config'
23

34
function wait(callback = () => {}, {timeout = 4500, interval = 50} = {}) {
45
return waitForExpect(callback, timeout, interval)
56
}
67

7-
export {wait}
8+
function waitWrapper(...args) {
9+
return getConfig().asyncWrapper(() => wait(...args))
10+
}
11+
12+
export {waitWrapper as wait}

typings/config.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface IConfig {
22
testIdAttribute: string
3+
asyncWrapper<T>(cb: Function): Promise<T>
34
}
45

56
export interface IConfigFn {

0 commit comments

Comments
 (0)