forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconcurrent.js
52 lines (37 loc) · 1.29 KB
/
concurrent.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
import React from 'react'
import {act, render, cleanup} from '../'
test('simple render works like legacy', () => {
const {container} = render(<div>test</div>, {root: 'concurrent'})
expect(container).toHaveTextContent('test')
})
test('unmounts are flushed in sync', () => {
const {container, unmount} = render(<div>test</div>, {root: 'concurrent'})
unmount()
expect(container.children).toHaveLength(0)
})
test('rerender are flushed in sync', () => {
const {container, rerender} = render(<div>foo</div>, {root: 'concurrent'})
rerender(<div>bar</div>)
expect(container).toHaveTextContent('foo')
})
test('cleanup unmounts in sync', () => {
const {container} = render(<div>test</div>, {root: 'concurrent'})
cleanup()
expect(container.children).toHaveLength(0)
})
test('state updates are concurrent', () => {
function TrackingButton() {
const [clickCount, increment] = React.useReducer(n => n + 1, 0)
return (
<button type="button" onClick={increment}>
Clicked {clickCount} times.
</button>
)
}
const {getByRole} = render(<TrackingButton />, {root: 'concurrent'})
act(() => {
getByRole('button').click()
expect(getByRole('button')).toHaveTextContent('Clicked 0 times')
})
expect(getByRole('button')).toHaveTextContent('Clicked 1 times')
})