Open History to see the last commit
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})