reactjs jest enzyme mock async api call code example
Example: mock api inside react component jest async
export const waitFor = (callback, { interval = 50, timeout = 1000 } = {}) =>
act(
() =>
new Promise((resolve, reject) => {
const startTime = Date.now()
const nextInterval = () => {
setTimeout(() => {
try {
callback()
resolve()
} catch (err) {
if (Date.now() - startTime > timeout) {
reject(new Error('Timed out.'))
} else {
nextInterval()
}
}
}, interval)
}
nextInterval()
}),
)