forked from kentcdodds/dom-testing-library-with-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react.test.js
40 lines (36 loc) · 1.01 KB
/
react.test.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
import '@testing-library/jest-dom/extend-expect'
import * as React from 'react'
import ReactDOM from 'react-dom'
import {getQueriesForElement} from '@testing-library/dom'
import userEvent from '@testing-library/user-event'
function Counter() {
const [count, setCount] = React.useState(0)
const increment = () => setCount(c => c + 1)
return (
<div>
<button onClick={increment}>{count}</button>
</div>
)
}
function render(ui) {
const container = document.createElement('div')
ReactDOM.render(ui, container)
document.body.appendChild(container)
return {
...getQueriesForElement(container),
container,
cleanup() {
ReactDOM.unmountComponentAtNode(container)
document.body.removeChild(container)
},
}
}
test('renders a counter', () => {
const {getByText, cleanup} = render(<Counter />)
const counter = getByText('0')
userEvent.click(counter)
expect(counter).toHaveTextContent('1')
userEvent.click(counter)
expect(counter).toHaveTextContent('2')
cleanup()
})