forked from callstack/react-native-testing-library
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaitForElementToBeRemoved.ts
42 lines (36 loc) · 1.15 KB
/
waitForElementToBeRemoved.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 waitFor from './waitFor';
import type { WaitForOptions } from './waitFor';
import { ErrorWithStack } from './helpers/errors';
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 waitFor(() => {
let result;
try {
result = expectation();
} catch (error) {
return initialElements;
}
if (!isRemoved(result)) {
throw timeoutError;
}
return initialElements;
}, options);
}