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