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

query-options.test-d.ts

209 lines5.4 KB

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