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