-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathrender.js
92 lines (80 loc) · 2.08 KB
/
render.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
import 'jest-dom/extend-expect'
import React from 'react'
import ReactDOM from 'react-dom'
import {render, cleanup} from '../'
afterEach(cleanup)
test('renders div into document', () => {
const ref = React.createRef()
const {container} = render(<div ref={ref} />)
expect(container.firstChild).toBe(ref.current)
})
test('works great with react portals', () => {
class MyPortal extends React.Component {
constructor(...args) {
super(...args)
this.portalNode = document.createElement('div')
this.portalNode.dataset.testid = 'my-portal'
}
componentDidMount() {
document.body.appendChild(this.portalNode)
}
componentWillUnmount() {
this.portalNode.parentNode.removeChild(this.portalNode)
}
render() {
return ReactDOM.createPortal(
<Greet greeting="Hello" subject="World" />,
this.portalNode,
)
}
}
function Greet({greeting, subject}) {
return (
<div>
<strong>
{greeting} {subject}
</strong>
</div>
)
}
const {unmount, getByTestId, getByText} = render(<MyPortal />)
expect(getByText('Hello World')).toBeInTheDocument()
const portalNode = getByTestId('my-portal')
expect(portalNode).toBeInTheDocument()
unmount()
expect(portalNode).not.toBeInTheDocument()
})
test('returns baseElement which defaults to document.body', () => {
const {baseElement} = render(<div />)
expect(baseElement).toBe(document.body)
})
it('cleansup document', () => {
const spy = jest.fn()
class Test extends React.Component {
componentWillUnmount() {
spy()
}
render() {
return <div />
}
}
render(<Test />)
cleanup()
expect(document.body.innerHTML).toBe('')
expect(spy).toHaveBeenCalledTimes(1)
})
it('supports fragments', () => {
class Test extends React.Component {
render() {
return (
<div>
<code>DocumentFragment</code> is pretty cool!
</div>
)
}
}
const {asFragment} = render(<Test />)
expect(asFragment()).toMatchSnapshot()
cleanup()
expect(document.body.innerHTML).toBe('')
})