Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

utils.ts

69 lines1.8 KB

1import { vi } from 'vitest'
2import { QueryClient, onlineManager } from '..'
3import * as utils from '../utils'
4import type { SpyInstance } from 'vitest'
5import type { MutationOptions, QueryClientConfig } from '..'
6​
7export function createQueryClient(config?: QueryClientConfig): QueryClient {
8 return new QueryClient(config)
9}
10​
11export function mockVisibilityState(
12 value: DocumentVisibilityState,
13): SpyInstance<[], DocumentVisibilityState> {
14 return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
16​
17export function mockOnlineManagerIsOnline(
18 value: boolean,
19): SpyInstance<[], boolean> {
20 return vi.spyOn(onlineManager, 'isOnline').mockReturnValue(value)
22​
23let queryKeyCount = 0
24export function queryKey(): Array<string> {
25 queryKeyCount++
26 return [`query_${queryKeyCount}`]
28​
29export function sleep(timeout: number): Promise<void> {
30 return new Promise((resolve, _reject) => {
31 setTimeout(resolve, timeout)
32 })
34​
35export const executeMutation = <TVariables>(
36 queryClient: QueryClient,
37 options: MutationOptions<any, any, TVariables, any>,
38 variables: TVariables,
39) => {
40 return queryClient
41 .getMutationCache()
42 .build(queryClient, options)
43 .execute(variables)
45​
46// This monkey-patches the isServer-value from utils,
47// so that we can pretend to be in a server environment
48export function setIsServer(isServer: boolean) {
49 const original = utils.isServer
50 Object.defineProperty(utils, 'isServer', {
51 get: () => isServer,
52 })
53​
54 return () => {
55 Object.defineProperty(utils, 'isServer', {
56 get: () => original,
57 })
58 }
60​
61export const doNotExecute = (_func: () => void) => true
62​
63export type Equal<TTargetA, TTargetB> = (<T>() => T extends TTargetA
64 ? 1
65 : 2) extends <T>() => T extends TTargetB ? 1 : 2
66 ? true
67 : false
68​
69export type Expect<T extends true> = T