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

utils.ts

37 lines996 Bytes

1import { vi } from 'vitest'
2import { onlineManager } from '..'
3import * as utils from '../utils'
4import type { MockInstance } from 'vitest'
5import type { MutationOptions, QueryClient } from '..'
6​
7export function mockOnlineManagerIsOnline(
8 value: boolean,
9): MockInstance<() => boolean> {
10 return vi.spyOn(onlineManager, 'isOnline').mockReturnValue(value)
12​
13export function executeMutation<TVariables>(
14 queryClient: QueryClient,
15 options: MutationOptions<any, any, TVariables, any>,
16 variables: TVariables,
17) {
18 return queryClient
19 .getMutationCache()
20 .build(queryClient, options)
21 .execute(variables)
23​
24// This monkey-patches the isServer-value from utils,
25// so that we can pretend to be in a server environment
26export function setIsServer(isServer: boolean) {
27 const original = utils.isServer
28 Object.defineProperty(utils, 'isServer', {
29 get: () => isServer,
30 })
31​
32 return () => {
33 Object.defineProperty(utils, 'isServer', {
34 get: () => original,
35 })
36 }