-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
utils.ts
69 lines (58 loc) · 1.76 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
import { vi } from 'vitest'
import { QueryClient, onlineManager } from '..'
import * as utils from '../utils'
import type { SpyInstance } from 'vitest'
import type { MutationOptions, QueryClientConfig } from '..'
export function createQueryClient(config?: QueryClientConfig): QueryClient {
return new QueryClient(config)
}
export function mockVisibilityState(
value: DocumentVisibilityState,
): SpyInstance<[], DocumentVisibilityState> {
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
}
export function mockOnlineManagerIsOnline(
value: boolean,
): SpyInstance<[], boolean> {
return vi.spyOn(onlineManager, 'isOnline').mockReturnValue(value)
}
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 const executeMutation = <TVariables>(
queryClient: QueryClient,
options: MutationOptions<any, any, TVariables, any>,
variables: TVariables,
) => {
return queryClient
.getMutationCache()
.build(queryClient, options)
.execute(variables)
}
// 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
Object.defineProperty(utils, 'isServer', {
get: () => isServer,
})
return () => {
Object.defineProperty(utils, 'isServer', {
get: () => original,
})
}
}
export const doNotExecute = (_func: () => void) => true
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T,
>() => T extends Y ? 1 : 2
? true
: false
export type Expect<T extends true> = T