/
cleanup.js
42 lines (33 loc) 路 894 Bytes
/
cleanup.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
import React from 'react'
import {render, cleanup} from '../'
test('cleans up the document', async () => {
const spy = jest.fn()
const divId = 'my-div'
class Test extends React.Component {
componentWillUnmount() {
expect(document.getElementById(divId)).toBeInTheDocument()
spy()
}
render() {
return <div id={divId} />
}
}
render(<Test />)
await cleanup()
expect(document.body).toBeEmptyDOMElement()
expect(spy).toHaveBeenCalledTimes(1)
})
test('cleanup does not error when an element is not a child', async () => {
render(<div />, {container: document.createElement('div')})
await cleanup()
})
test('cleanup runs effect cleanup functions', async () => {
const spy = jest.fn()
const Test = () => {
React.useEffect(() => spy)
return null
}
render(<Test />)
await cleanup()
expect(spy).toHaveBeenCalledTimes(1)
})