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

queryOptions.test-d.tsx

189 lines5.8 KB

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