forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
78 lines (65 loc) · 1.91 KB
/
utils.ts
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
import { act } from '@testing-library/react'
import {
MutationOptions,
QueryClient,
QueryClientConfig,
} from '@tanstack/query-core'
import * as utils from '../packages/query-core/src/utils'
export function createQueryClient(config?: QueryClientConfig): QueryClient {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
return new QueryClient({ logger: mockLogger, ...config })
}
export function mockVisibilityState(value: DocumentVisibilityState) {
return jest.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
}
export function mockNavigatorOnLine(value: boolean) {
return jest.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
}
export const mockLogger = {
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
}
let queryKeyCount = 0
export function queryKey(): Array<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) {
return 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 executeMutation = (
queryClient: QueryClient,
options: MutationOptions<any, any, any, any>,
): Promise<unknown> => {
return queryClient.getMutationCache().build(queryClient, options).execute()
}
// This monkey-patches the isServer-value from utils,
// so that we can pretend to be in a server environment
export function setIsServer(isServer: boolean) {
const original = utils.isServer
// @ts-ignore
utils.isServer = isServer
return () => {
// @ts-ignore
utils.isServer = original
}
}