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