-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathuse-swr-legacy-react.test.tsx
74 lines (59 loc) · 2.06 KB
/
use-swr-legacy-react.test.tsx
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
// This test case covers special environments such as React <= 17.
import { act, screen, render, fireEvent } from '@testing-library/react'
// https://github.com/jestjs/jest/issues/11471
jest.mock('react', () => jest.requireActual('react'))
async function withLegacyReact(runner: () => Promise<void>) {
await jest.isolateModulesAsync(async () => {
await runner()
})
}
describe('useSWR - legacy React', () => {
;(process.env.__SWR_TEST_BUILD ? it.skip : it)(
'should enable the IS_REACT_LEGACY flag - startTransition',
async () => {
await withLegacyReact(async () => {
// Test mutation and trigger
const useSWRMutation = (await import('swr/mutation')).default
const waitForNextTick = () =>
act(() => new Promise(resolve => setTimeout(resolve, 1)))
const key = Math.random().toString()
function Page() {
const { data, trigger } = useSWRMutation(key, () => 'data')
return <button onClick={() => trigger()}>{data || 'pending'}</button>
}
render(<Page />)
// mount
await screen.findByText('pending')
fireEvent.click(screen.getByText('pending'))
await waitForNextTick()
screen.getByText('data')
})
}
)
// https://github.com/vercel/swr/blob/cfcfa9e320a59742d41a77e52003127b04378c4f/src/core/use-swr.ts#L345
;(process.env.__SWR_TEST_BUILD ? it.skip : it)(
'should enable the IS_REACT_LEGACY flag - unmount check',
async () => {
await withLegacyReact(async () => {
const useSWR = (await import('swr')).default
const key = Math.random().toString()
function Page() {
// No fallback data
const { data } = useSWR(
key,
() =>
new Promise<string>(resolve =>
setTimeout(() => resolve('data'), 100)
),
{
loadingTimeout: 10
}
)
return <p>{data || 'pending'}</p>
}
render(<Page />)
await screen.findByText('data')
})
}
)
})