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