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

queryOptions.test-d.ts

213 lines6.1 KB

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