forked from testing-library/dom-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait-for-element.js
100 lines (84 loc) 路 2.89 KB
/
wait-for-element.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import {render, renderIntoDocument} from './helpers/test-utils'
function importModule() {
return require('../').waitForElement
}
let waitForElement
beforeEach(() => {
jest.useRealTimers()
jest.resetModules()
waitForElement = importModule()
console.warn.mockClear()
})
test('waits for element to appear in the document', async () => {
const {rerender, getByTestId} = renderIntoDocument('<div />')
const promise = waitForElement(() => getByTestId('div'))
setTimeout(() => rerender('<div data-testid="div" />'))
const element = await promise
expect(element).toBeInTheDocument()
expect(console.warn.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"\`waitForElement\` has been deprecated. Use a \`find*\` query (preferred: https://testing-library.com/docs/dom-testing-library/api-queries#findby) or use \`waitFor\` instead: https://testing-library.com/docs/dom-testing-library/api-async#waitfor",
],
]
`)
})
test('waits for element to appear in a specified container', async () => {
const {rerender, container, getByTestId} = render('<div />')
const promise = waitForElement(() => getByTestId('div'), {container})
setTimeout(() => rerender('<div data-testid="div" />'))
const element = await promise
expect(element).toBeTruthy()
})
test('throws last thrown error', async () => {
const {rerender, container} = render('<div />')
let error
setTimeout(() => {
error = new Error('first error')
rerender('<div>first</div>')
}, 10)
setTimeout(() => {
error = new Error('second error')
rerender('<div>second</div>')
}, 20)
const promise = waitForElement(
() => {
throw error
},
{container, timeout: 50},
)
await expect(promise).rejects.toThrow(error)
})
test('waits until callback does not return null', async () => {
const {rerender, container, queryByTestId} = render('<div />')
const promise = waitForElement(() => queryByTestId('div'), {container})
setTimeout(() => rerender('<div data-testid="div" />'))
const element = await promise
expect(element).toBeTruthy()
})
test('throws error if no callback is provided', async () => {
await expect(waitForElement()).rejects.toThrow(/callback/i)
})
describe('timers', () => {
const expectElementToExist = async () => {
const importedWaitForElement = importModule()
const {rerender, getByTestId} = renderIntoDocument('<div />')
setTimeout(() => rerender('<div data-testid="div" />'), 100)
const promise = importedWaitForElement(() => getByTestId('div'), {
timeout: 200,
})
if (setTimeout._isMockFunction) {
jest.advanceTimersByTime(110)
}
const element = await promise
await expect(element).toBeInTheDocument()
}
it('works with real timers', async () => {
jest.useRealTimers()
await expectElementToExist()
})
it('works with fake timers', async () => {
jest.useFakeTimers()
await expectElementToExist()
})
})