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