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