Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { QueryClient } from '../queryClient'
3import type { DataTag, InfiniteData } from '@tanstack/query-core'
4β
5describe('getQueryData', () => {
6 it('should be typed if key is tagged', () => {
7 const queryKey = ['key'] as DataTag<Array<string>, number>
8 const queryClient = new QueryClient()
9 const data = queryClient.getQueryData(queryKey)
10β
11 expectTypeOf(data).toEqualTypeOf<number | undefined>()
12 })
13β
14 it('should infer unknown if key is not tagged', () => {
15 const queryKey = ['key'] as const
16 const queryClient = new QueryClient()
17 const data = queryClient.getQueryData(queryKey)
18β
19 expectTypeOf(data).toEqualTypeOf<unknown>()
20 })
21β
22 it('should infer passed generic if passed', () => {
23 const queryKey = ['key'] as const
24 const queryClient = new QueryClient()
25 const data = queryClient.getQueryData<number>(queryKey)
26β
27 expectTypeOf(data).toEqualTypeOf<number | undefined>()
28 })
29β
30 it('should only allow Arrays to be passed', () => {
31 assertType<Parameters<QueryClient['getQueryData']>>([
32 // @ts-expect-error TS2345: Argument of type 'string' is not assignable to parameter of type 'QueryKey'
33 { queryKey: 'key' },
34 ])
35 })
36})
37β
38describe('setQueryData', () => {
39 it('updater should be typed if key is tagged', () => {
40 const queryKey = ['key'] as DataTag<Array<string>, number>
41 const queryClient = new QueryClient()
42 const data = queryClient.setQueryData(queryKey, (prev) => {
43 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
44 return prev
45 })
46 expectTypeOf(data).toEqualTypeOf<number | undefined>()
47 })
48β
49 it('value should be typed if key is tagged', () => {
50 const queryKey = ['key'] as DataTag<Array<string>, number>
51 const queryClient = new QueryClient()
52β
53 // @ts-expect-error value should be a number
54 queryClient.setQueryData(queryKey, '1')
55β
56 // @ts-expect-error value should be a number
57 queryClient.setQueryData(queryKey, () => '1')
58β
59 const data = queryClient.setQueryData(queryKey, 1)
60β
61 expectTypeOf(data).toEqualTypeOf<number | undefined>()
62 })
63β
64 it('should infer unknown for updater if key is not tagged', () => {
65 const queryKey = ['key'] as const
66 const queryClient = new QueryClient()
67 const data = queryClient.setQueryData(queryKey, (prev) => {
68 expectTypeOf(prev).toEqualTypeOf<unknown>()
69 return prev
70 })
71 expectTypeOf(data).toEqualTypeOf<unknown>()
72 })
73β
74 it('should infer unknown for value if key is not tagged', () => {
75 const queryKey = ['key'] as const
76 const queryClient = new QueryClient()
77 const data = queryClient.setQueryData(queryKey, 'foo')
78β
79 expectTypeOf(data).toEqualTypeOf<unknown>()
80 })
81β
82 it('should infer passed generic if passed', () => {
83 const queryKey = ['key'] as const
84 const queryClient = new QueryClient()
85 const data = queryClient.setQueryData<string>(queryKey, (prev) => {
86 expectTypeOf(prev).toEqualTypeOf<string | undefined>()
87 return prev
88 })
89 expectTypeOf(data).toEqualTypeOf<string | undefined>()
90 })
91β
92 it('should infer passed generic for value', () => {
93 const queryKey = ['key'] as const
94 const queryClient = new QueryClient()
95 const data = queryClient.setQueryData<string>(queryKey, 'foo')
96β
97 expectTypeOf(data).toEqualTypeOf<string | undefined>()
98 })
99β
100 it('should preserve updater parameter type inference when used in functions with explicit return types', () => {
101 const queryKey = ['key'] as DataTag<Array<string>, number>
102 const queryClient = new QueryClient()
103β
104 // Simulate usage inside a function with explicit return type
105 // The outer function returns 'unknown' but this shouldn't affect the updater's type inference
106 ;(() =>
107 queryClient.setQueryData(queryKey, (data) => {
108 expectTypeOf(data).toEqualTypeOf<number | undefined>()
109 return data
110 })) satisfies () => unknown
111 })
112})
113β
114describe('fetchInfiniteQuery', () => {
115 it('should allow passing pages', async () => {
116 const data = await new QueryClient().fetchInfiniteQuery({
117 queryKey: ['key'],
118 queryFn: () => Promise.resolve('string'),
119 getNextPageParam: () => 1,
120 initialPageParam: 1,
121 pages: 5,
122 })
123β
124 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
125 })
126β
127 it('should not allow passing getNextPageParam without pages', () => {
128 assertType<Parameters<QueryClient['fetchInfiniteQuery']>>([
129 {
130 queryKey: ['key'],
131 queryFn: () => Promise.resolve('string'),
132 initialPageParam: 1,
133 getNextPageParam: () => 1,
134 },
135 ])
136 })
137β
138 it('should not allow passing pages without getNextPageParam', () => {
139 assertType<Parameters<QueryClient['fetchInfiniteQuery']>>([
140 // @ts-expect-error Property 'getNextPageParam' is missing
141 {
142 queryKey: ['key'],
143 queryFn: () => Promise.resolve('string'),
144 initialPageParam: 1,
145 pages: 5,
146 },
147 ])
148 })
149})