forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender.js
103 lines (90 loc) · 2.43 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
93
94
95
96
97
98
99
100
101
102
103
import * as React from 'react'
import ReactDOM from 'react-dom'
import {render, screen} from '../'
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} = render(<MyPortal />)
expect(screen.getByText('Hello World')).toBeInTheDocument()
const portalNode = screen.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)
})
test('supports fragments', () => {
class Test extends React.Component {
render() {
return (
<div>
<code>DocumentFragment</code> is pretty cool!
</div>
)
}
}
const {asFragment} = render(<Test />)
expect(asFragment()).toMatchSnapshot()
})
test('renders options.wrapper around node', () => {
const WrapperComponent = ({children}) => (
<div data-testid="wrapper">{children}</div>
)
const {container} = render(<div data-testid="inner" />, {
wrapper: WrapperComponent,
})
expect(screen.getByTestId('wrapper')).toBeInTheDocument()
expect(container.firstChild).toMatchInlineSnapshot(`
<div
data-testid=wrapper
>
<div
data-testid=inner
/>
</div>
`)
})
test('flushes useEffect cleanup functions sync on unmount()', () => {
const spy = jest.fn()
function Component() {
React.useEffect(() => spy, [])
return null
}
const {unmount} = render(<Component />)
expect(spy).toHaveBeenCalledTimes(0)
unmount()
expect(spy).toHaveBeenCalledTimes(1)
})