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

queryOptions.test-d.ts

197 lines5.6 KB

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