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

queryOptions.test-d.ts

215 lines6.1 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { reactive, ref } from 'vue-demi'
3import { dataTagSymbol } from '@tanstack/query-core'
4import { QueryClient } from '../queryClient'
5import { queryOptions } from '../queryOptions'
6import { useQuery } from '../useQuery'
7​
8describe('queryOptions', () => {
9 it('should not allow excess properties', () => {
10 assertType(
11 queryOptions({
12 queryKey: ['key'],
13 queryFn: () => Promise.resolve(5),
14 // @ts-expect-error this is a good error, because stallTime does not exist!
15 stallTime: 1000,
16 }),
17 )
18 })
19 it('should infer types for callbacks', () => {
20 queryOptions({
21 queryKey: ['key'],
22 queryFn: () => Promise.resolve(5),
23 staleTime: 1000,
24 select: (data) => {
25 expectTypeOf(data).toEqualTypeOf<number>()
26 },
27 })
28 })
29 it('should work when passed to useQuery', () => {
30 const options = queryOptions({
31 queryKey: ['key'],
32 queryFn: () => Promise.resolve(5),
33 })
34​
35 const { data } = reactive(useQuery(options))
36 expectTypeOf(data).toEqualTypeOf<number | undefined>()
37 })
38 it('should tag the queryKey with the result type of the QueryFn', () => {
39 const { queryKey } = queryOptions({
40 queryKey: ['key'],
41 queryFn: () => Promise.resolve(5),
42 })
43​
44 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
45 })
46 it('should tag the queryKey even if no promise is returned', () => {
47 const { queryKey } = queryOptions({
48 queryKey: ['key'],
49 queryFn: () => 5,
50 })
51​
52 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
53 })
54 it('should tag the queryKey with unknown if there is no queryFn', () => {
55 const { queryKey } = queryOptions({
56 queryKey: ['key'],
57 })
58​
59 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
60 })
61 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
62 const { queryKey } = queryOptions({
63 queryKey: ['key'],
64 queryFn: () => Promise.resolve(5),
65 select: (data) => data.toString(),
66 })
67​
68 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
69 })
70 it('should return the proper type when passed to getQueryData', () => {
71 const { queryKey } = queryOptions({
72 queryKey: ['key'],
73 queryFn: () => Promise.resolve(5),
74 })
75​
76 const queryClient = new QueryClient()
77 const data = queryClient.getQueryData(queryKey)
78​
79 expectTypeOf(data).toEqualTypeOf<number | undefined>()
80 })
81 it('should properly type updaterFn when passed to setQueryData', () => {
82 const { queryKey } = queryOptions({
83 queryKey: ['key'],
84 queryFn: () => Promise.resolve(5),
85 })
86​
87 const queryClient = new QueryClient()
88 const data = queryClient.setQueryData(queryKey, (prev) => {
89 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
90 return prev
91 })
92 expectTypeOf(data).toEqualTypeOf<number | undefined>()
93 })
94 it('should properly type value when passed to setQueryData', () => {
95 const { queryKey } = queryOptions({
96 queryKey: ['key'],
97 queryFn: () => Promise.resolve(5),
98 })
99​
100 const queryClient = new QueryClient()
101​
102 // @ts-expect-error value should be a number
103 queryClient.setQueryData(queryKey, '5')
104 // @ts-expect-error value should be a number
105 queryClient.setQueryData(queryKey, () => '5')
106​
107 const data = queryClient.setQueryData(queryKey, 5)
108​
109 expectTypeOf(data).toEqualTypeOf<number | undefined>()
110 })
111 it('should allow to be passed to QueryClient methods while containing ref in queryKey', () => {
112 const options = queryOptions({
113 queryKey: ['key', ref(1), { nested: ref(2) }],
114 queryFn: () => Promise.resolve(5),
115 })
116​
117 const queryClient = new QueryClient()
118​
119 // Should not error
120 const data = queryClient.invalidateQueries(options)
121 // Should not error
122 const data2 = queryClient.fetchQuery(options)
123​
124 expectTypeOf(data).toEqualTypeOf<Promise<void>>()
125 expectTypeOf(data2).toEqualTypeOf<Promise<number>>()
126 })
127​
128 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
129 const { data } = reactive(
130 useQuery(
131 queryOptions({
132 queryKey: ['key'],
133 queryFn: () => {
134 return {
135 wow: true,
136 }
137 },
138 initialData: () => ({
139 wow: true,
140 }),
141 }),
142 ),
143 )
144​
145 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
146 })
147​
148 it('TData should have undefined in the union when initialData is NOT provided', () => {
149 const { data } = reactive(
150 useQuery(
151 queryOptions({
152 queryKey: ['key'],
153 queryFn: () => {
154 return {
155 wow: true,
156 }
157 },
158 }),
159 ),
160 )
161​
162 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
163 })
164​
165 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
166 const { data } = reactive(
167 useQuery(
168 queryOptions({
169 queryKey: ['key'],
170 queryFn: () => {
171 return {
172 wow: true,
173 }
174 },
175 initialData: () => undefined as { wow: boolean } | undefined,
176 }),
177 ),
178 )
179​
180 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
181 })
182​
183 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
184 const { data, isSuccess } = reactive(
185 useQuery(
186 queryOptions({
187 queryKey: ['key'],
188 queryFn: () => {
189 return {
190 wow: true,
191 }
192 },
193 initialData: () => undefined as { wow: boolean } | undefined,
194 }),
195 ),
196 )
197​
198 if (isSuccess) {
199 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
200 }
201 })
202​
203 it('data should not have undefined when initialData is provided', () => {
204 const { data } = reactive(
205 useQuery(
206 queryOptions({
207 queryKey: ['query-key'],
208 initialData: 42,
209 }),
210 ),
211 )
212​
213 expectTypeOf(data).toEqualTypeOf<number>()
214 })
215})