-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathwait-for-element-to-be-removed.ts
More file actions
63 lines (54 loc) · 1.83 KB
/
wait-for-element-to-be-removed.ts
File metadata and controls
63 lines (54 loc) · 1.83 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {waitForOptions} from '../types'
import {waitFor} from './wait-for'
type Result = (Node | null)[] | Node | null
const isRemoved = (result: Result): boolean =>
!result || (Array.isArray(result) && !result.length)
// Check if the element is not present.
// As the name implies, waitForElementToBeRemoved should check `present` --> `removed`
function initialCheck(elements: Result): void {
if (isRemoved(elements)) {
throw new Error(
'The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal.',
)
}
}
async function waitForElementToBeRemoved(
arg: Result | (() => Result),
options?: waitForOptions,
): Promise<void> {
// created here so we get a nice stacktrace
const timeoutError = new Error('Timed out in waitForElementToBeRemoved.')
function handleArg(argument: Result): () => Result {
initialCheck(argument)
const elements = Array.isArray(argument) ? argument : [argument]
const getRemainingElements = elements.map(element => {
let parent = element?.parentElement
if (!parent) return () => null
while (parent.parentElement) parent = parent.parentElement
return () => (parent?.contains(element) ? element : null)
})
return () => getRemainingElements.map(c => c()).filter(Boolean)
}
const callback = typeof arg === 'function' ? arg : handleArg(arg)
initialCheck(callback())
return waitFor(() => {
let result
try {
result = callback()
} catch (error: unknown) {
if ((error as Error).name === 'TestingLibraryElementError') {
return undefined
}
throw error
}
if (!isRemoved(result)) {
throw timeoutError
}
return undefined
}, options)
}
export {waitForElementToBeRemoved}
/*
eslint
require-await: "off"
*/