forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasync.js
73 lines (58 loc) · 1.68 KB
/
async.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
// TODO: Upstream that the rule should check import source
/* eslint-disable testing-library/no-await-sync-events */
import * as React from 'react'
import {act, render, fireEvent} from '../async'
const isReact19 = React.version.startsWith('19.')
const testGateReact19 = isReact19 ? test : test.skip
testGateReact19('async data requires async APIs', async () => {
let resolve
const promise = new Promise(_resolve => {
resolve = _resolve
})
function Component() {
const value = React.use(promise)
return <div>{value}</div>
}
const {container} = await render(
<React.Suspense fallback="loading...">
<Component />
</React.Suspense>,
)
expect(container).toHaveTextContent('loading...')
await act(async () => {
resolve('Hello, Dave!')
})
expect(container).toHaveTextContent('Hello, Dave!')
})
testGateReact19('async fireEvent', async () => {
let resolve
function Component() {
const [promise, setPromise] = React.useState('initial')
const value = typeof promise === 'string' ? promise : React.use(promise)
return (
<button
onClick={() =>
setPromise(
new Promise(_resolve => {
resolve = _resolve
}),
)
}
>
Value: {value}
</button>
)
}
const {container} = await render(
<React.Suspense fallback="loading...">
<Component />
</React.Suspense>,
)
expect(container).toHaveTextContent('Value: initial')
await fireEvent.click(container.querySelector('button'))
expect(container).toHaveTextContent('loading...')
await act(() => {
resolve('Hello, Dave!')
})
expect(container).toHaveTextContent('Hello, Dave!')
})