mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
480dd28cbc
Fix types for Flow too
27 lines
623 B
JavaScript
27 lines
623 B
JavaScript
// @flow
|
|
export default function waitForExpect<T: *>(
|
|
expectation: () => T,
|
|
timeout: number = 4500,
|
|
interval: number = 50
|
|
): Promise<T> {
|
|
const startTime = Date.now();
|
|
return new Promise<T>((resolve, reject) => {
|
|
const rejectOrRerun = error => {
|
|
if (Date.now() - startTime >= timeout) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
setTimeout(runExpectation, interval);
|
|
};
|
|
function runExpectation() {
|
|
try {
|
|
const result = expectation();
|
|
resolve(result);
|
|
} catch (error) {
|
|
rejectOrRerun(error);
|
|
}
|
|
}
|
|
setTimeout(runExpectation, 0);
|
|
});
|
|
}
|