Skip to content

feat: add asyncWrapper config #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
// './queries' are query functions.
let config = {
testIdAttribute: 'data-testid',
// this is to support React's async `act` function.
// forcing react-testing-library to wrap all async functions would've been
// a total nightmare (consider wrapping every findBy* query and then also
// updating `within` so those would be wrapped too. Total nightmare).
// so we have this config option that's really only intended for
// react-testing-library to use. For that reason, this feature will remain
// undocumented.
asyncWrapper: cb => cb(),
}

export function configure(newConfig) {
Expand Down
7 changes: 6 additions & 1 deletion src/wait-for-dom-change.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {newMutationObserver, getDocument, getSetImmediate} from './helpers'
import {getConfig} from './config'

function waitForDomChange({
container = getDocument(),
Expand Down Expand Up @@ -36,4 +37,8 @@ function waitForDomChange({
})
}

export {waitForDomChange}
function waitForDomChangeWrapper(...args) {
return getConfig().asyncWrapper(() => waitForDomChange(...args))
}

export {waitForDomChangeWrapper as waitForDomChange}
7 changes: 6 additions & 1 deletion src/wait-for-element-to-be-removed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {getDocument, getSetImmediate, newMutationObserver} from './helpers'
import {getConfig} from './config'

function waitForElementToBeRemoved(
callback,
Expand Down Expand Up @@ -69,4 +70,8 @@ function waitForElementToBeRemoved(
})
}

export {waitForElementToBeRemoved}
function waitForElementToBeRemovedWrapper(...args) {
return getConfig().asyncWrapper(() => waitForElementToBeRemoved(...args))
}

export {waitForElementToBeRemovedWrapper as waitForElementToBeRemoved}
7 changes: 6 additions & 1 deletion src/wait-for-element.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {newMutationObserver, getDocument, getSetImmediate} from './helpers'
import {getConfig} from './config'

function waitForElement(
callback,
Expand Down Expand Up @@ -54,4 +55,8 @@ function waitForElement(
})
}

export {waitForElement}
function waitForElementWrapper(...args) {
return getConfig().asyncWrapper(() => waitForElement(...args))
}

export {waitForElementWrapper as waitForElement}
7 changes: 6 additions & 1 deletion src/wait.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import waitForExpect from 'wait-for-expect'
import {getConfig} from './config'

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

export {wait}
function waitWrapper(...args) {
return getConfig().asyncWrapper(() => wait(...args))
}

export {waitWrapper as wait}
1 change: 1 addition & 0 deletions typings/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface IConfig {
testIdAttribute: string
asyncWrapper<T>(cb: Function): Promise<T>
}

export interface IConfigFn {
Expand Down