forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.tsx
85 lines (72 loc) · 1.98 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
import { act, render } from '@testing-library/react'
import React from 'react'
import { QueryClient, QueryClientProvider } from '../..'
export function renderWithClient(client: QueryClient, ui: React.ReactElement) {
const { rerender, ...result } = render(
<QueryClientProvider client={client}>{ui}</QueryClientProvider>
)
return {
...result,
rerender: (rerenderUi: React.ReactElement) =>
rerender(
<QueryClientProvider client={client}>{rerenderUi}</QueryClientProvider>
),
}
}
export function mockVisibilityState(value: string) {
Object.defineProperty(document, 'visibilityState', {
value,
configurable: true,
})
}
export function mockNavigatorOnLine(value: boolean) {
Object.defineProperty(navigator, 'onLine', {
value,
configurable: true,
})
}
export function mockConsoleError() {
const consoleMock = jest.spyOn(console, 'error')
consoleMock.mockImplementation(() => undefined)
return consoleMock
}
let queryKeyCount = 0
export function queryKey(): string {
queryKeyCount++
return `query_${queryKeyCount}`
}
export function sleep(timeout: number): Promise<void> {
return new Promise((resolve, _reject) => {
setTimeout(resolve, timeout)
})
}
export function setActTimeout(fn: () => void, ms?: number) {
setTimeout(() => {
act(() => {
fn()
})
}, ms)
}
/**
* Assert the parameter is of a specific type.
*/
export const expectType = <T,>(_: T): void => undefined
/**
* Assert the parameter is not typed as `any`
*/
export const expectTypeNotAny = <T,>(_: 0 extends 1 & T ? never : T): void =>
undefined
export const Blink: React.FC<{ duration: number }> = ({
duration,
children,
}) => {
const [shouldShow, setShouldShow] = React.useState<boolean>(true)
React.useEffect(() => {
setShouldShow(true)
const timeout = setTimeout(() => setShouldShow(false), duration)
return () => {
clearTimeout(timeout)
}
}, [duration, children])
return shouldShow ? <>{children}</> : <>off</>
}