forked from kentcdodds/dom-testing-library-with-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react.test.js
36 lines (32 loc) · 889 Bytes
/
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
import 'jest-dom/extend-expect'
import React from 'react'
import ReactDOM from 'react-dom'
import {getQueriesForElement, fireEvent} from 'dom-testing-library'
class Counter extends React.Component {
state = {count: 0}
increment = () => this.setState(({count}) => ({count: count + 1}))
render() {
return (
<div>
<button onClick={this.increment}>{this.state.count}</button>
</div>
)
}
}
function render(ui) {
const container = document.createElement('div')
document.body.appendChild(container)
ReactDOM.render(ui, container)
return {
container,
...getQueriesForElement(container),
}
}
test('renders a counter', () => {
const {getByText} = render(<Counter />)
const counter = getByText('0')
fireEvent.click(counter)
expect(counter).toHaveTextContent('1')
fireEvent.click(counter)
expect(counter).toHaveTextContent('2')
})