-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathwait-for-element-to-be-removed.ts
42 lines (36 loc) · 1.15 KB
/
wait-for-element-to-be-removed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { ErrorWithStack } from './helpers/errors';
import type { WaitForOptions } from './wait-for';
import waitFor from './wait-for';
function isRemoved<T>(result: T): boolean {
return !result || (Array.isArray(result) && !result.length);
}
export default async function waitForElementToBeRemoved<T>(
expectation: () => T,
options?: WaitForOptions,
): Promise<T> {
// Created here so we get a nice stacktrace
const timeoutError = new ErrorWithStack(
'Timed out in waitForElementToBeRemoved.',
waitForElementToBeRemoved,
);
// Elements have to be present initally and then removed.
const initialElements = expectation();
if (isRemoved(initialElements)) {
throw new ErrorWithStack(
'The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal.',
waitForElementToBeRemoved,
);
}
return await waitFor(() => {
let result;
try {
result = expectation();
} catch {
return initialElements;
}
if (!isRemoved(result)) {
throw timeoutError;
}
return initialElements;
}, options);
}