forked from vercel/swr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.tsx
105 lines (92 loc) · 3.12 KB
/
utils.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { act, fireEvent, render } from '@testing-library/react'
import React from 'react'
import { SWRConfig } from 'swr'
export function sleep(time: number) {
return new Promise<void>(resolve => setTimeout(resolve, time))
}
export const createResponse = <T,>(
response: T,
{ delay } = { delay: 10 }
): Promise<T> =>
new Promise((resolve, reject) =>
setTimeout(() => {
if (response instanceof Error) {
reject(response)
} else {
resolve(response)
}
}, delay)
)
export const nextTick = () => act(() => sleep(1))
export const focusOn = (element: any) =>
act(async () => {
fireEvent.focus(element)
})
export const createKey = () => 'swr-key-' + ~~(Math.random() * 1e7)
const _renderWithConfig = (
element: React.ReactElement,
config: Parameters<typeof SWRConfig>[0]['value']
): ReturnType<typeof render> => {
const TestSWRConfig = ({ children }: { children: React.ReactNode }) => (
<SWRConfig value={config}>{children}</SWRConfig>
)
return render(element, { wrapper: TestSWRConfig })
}
export const renderWithConfig = (
element: React.ReactElement,
config?: Parameters<typeof _renderWithConfig>[1]
): ReturnType<typeof _renderWithConfig> => {
const provider = () => new Map()
return _renderWithConfig(element, { provider, ...config })
}
export const renderWithGlobalCache = (
element: React.ReactElement,
config?: Parameters<typeof _renderWithConfig>[1]
): ReturnType<typeof _renderWithConfig> => {
return _renderWithConfig(element, { ...config })
}
export const hydrateWithConfig = (
element: React.ReactElement,
container: HTMLElement,
config?: Parameters<typeof _renderWithConfig>[1]
): ReturnType<typeof _renderWithConfig> => {
const provider = () => new Map()
const TestSWRConfig = ({ children }: { children: React.ReactNode }) => (
<SWRConfig value={{ provider, ...config }}>{children}</SWRConfig>
)
return render(element, { container, wrapper: TestSWRConfig, hydrate: true })
}
export const mockVisibilityHidden = () => {
const mockVisibilityState = jest.spyOn(document, 'visibilityState', 'get')
mockVisibilityState.mockImplementation(() => 'hidden')
return () => mockVisibilityState.mockRestore()
}
// Using `act()` will cause React 18 to batch updates.
// https://github.com/reactwg/react-18/discussions/102
export async function executeWithoutBatching(fn: () => any) {
const prev = global.IS_REACT_ACT_ENVIRONMENT
global.IS_REACT_ACT_ENVIRONMENT = false
await fn()
global.IS_REACT_ACT_ENVIRONMENT = prev
}
export const mockConsoleForHydrationErrors = () => {
jest.spyOn(console, 'error').mockImplementation(() => {})
return () => {
// It should not have any hydration warnings.
expect(
// @ts-expect-error
console.error.mock.calls.find(([err]) => {
return (
err?.message?.includes(
'Text content does not match server-rendered HTML.'
) ||
err?.message?.includes(
'Hydration failed because the initial UI does not match what was rendered on the server.'
)
)
})
).toBeFalsy()
// @ts-expect-error
console.error.mockRestore()
}
}