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

queryOptions.test-d.tsx

161 lines5.0 KB

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