Skip to content

Commit 9834de2

Browse files
author
Kent C. Dodds
committed
test: improve tests for wait-for-element-to-be-removed
1 parent bc2e3f2 commit 9834de2

File tree

3 files changed

+77
-328
lines changed

3 files changed

+77
-328
lines changed

src/__tests__/helpers/document.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ if (typeof window === 'undefined') {
44
const {JSDOM} = require('jsdom')
55
const dom = new JSDOM()
66
testWindow = dom.window
7+
global.window = testWindow
78
}
89

910
module.exports = testWindow.document
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'jest-dom/extend-expect'
2+
import {waitForElementToBeRemoved} from '../'
3+
import {render} from './helpers/test-utils'
4+
5+
jest.useFakeTimers()
6+
7+
// const skipSomeTimeForMutationObserver = (delayMs = 50) => {
8+
// jest.advanceTimersByTime(delayMs)
9+
// jest.runAllImmediates()
10+
// }
11+
12+
test('requires a function as the first parameter', () => {
13+
return expect(
14+
waitForElementToBeRemoved(),
15+
).rejects.toThrowErrorMatchingInlineSnapshot(
16+
`"waitForElementToBeRemoved requires a function as the first parameter"`,
17+
)
18+
})
19+
20+
test('requires an element to exist first', () => {
21+
return expect(
22+
waitForElementToBeRemoved(() => null),
23+
).rejects.toThrowErrorMatchingInlineSnapshot(
24+
`"The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist before waiting for removal."`,
25+
)
26+
})
27+
28+
test('requires an unempty array of elements to exist first', () => {
29+
return expect(
30+
waitForElementToBeRemoved(() => []),
31+
).rejects.toThrowErrorMatchingInlineSnapshot(
32+
`"The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist before waiting for removal."`,
33+
)
34+
})
35+
36+
test('times out after 4500ms by default', () => {
37+
const {container} = render(`<div></div>`)
38+
const promise = expect(
39+
waitForElementToBeRemoved(() => container),
40+
).rejects.toThrowErrorMatchingInlineSnapshot(
41+
`"Timed out in waitForElementToBeRemoved."`,
42+
)
43+
jest.advanceTimersByTime(4501)
44+
return promise
45+
})

0 commit comments

Comments
 (0)