2018-10-10 21:06:06 +02:00
|
|
|
// @flow
|
2018-10-10 21:45:32 +02:00
|
|
|
export default function waitForExpect<T: *>(
|
2018-10-10 21:06:06 +02:00
|
|
|
expectation: () => T,
|
|
|
|
|
timeout: number = 4500,
|
|
|
|
|
interval: number = 50
|
2018-10-10 21:45:32 +02:00
|
|
|
): Promise<T> {
|
2018-10-10 21:06:06 +02:00
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|