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

queryOptions.test-d.tsx

200 lines6.3 KB

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