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