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