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

queryOptions.test-d.ts

125 lines3.9 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { reactive, ref } from 'vue-demi'
3import { dataTagSymbol } from '@tanstack/query-core'
4import { QueryClient } from '../queryClient'
5import { queryOptions } from '../queryOptions'
6import { useQuery } from '../useQuery'
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 } = reactive(useQuery(options))
34 expectTypeOf(data).toEqualTypeOf<number | undefined>()
35 })
36 it('should tag the queryKey with the result type of the QueryFn', () => {
37 const { queryKey } = queryOptions({
38 queryKey: ['key'],
39 queryFn: () => Promise.resolve(5),
40 })
41​
42 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
43 })
44 it('should tag the queryKey even if no promise is returned', () => {
45 const { queryKey } = queryOptions({
46 queryKey: ['key'],
47 queryFn: () => 5,
48 })
49​
50 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
51 })
52 it('should tag the queryKey with unknown if there is no queryFn', () => {
53 const { queryKey } = queryOptions({
54 queryKey: ['key'],
55 })
56​
57 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
58 })
59 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
60 const { queryKey } = queryOptions({
61 queryKey: ['key'],
62 queryFn: () => Promise.resolve(5),
63 select: (data) => data.toString(),
64 })
65​
66 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
67 })
68 it('should return the proper type when passed to getQueryData', () => {
69 const { queryKey } = queryOptions({
70 queryKey: ['key'],
71 queryFn: () => Promise.resolve(5),
72 })
73​
74 const queryClient = new QueryClient()
75 const data = queryClient.getQueryData(queryKey)
76​
77 expectTypeOf(data).toEqualTypeOf<number | undefined>()
78 })
79 it('should properly type updaterFn when passed to setQueryData', () => {
80 const { queryKey } = queryOptions({
81 queryKey: ['key'],
82 queryFn: () => Promise.resolve(5),
83 })
84​
85 const queryClient = new QueryClient()
86 const data = queryClient.setQueryData(queryKey, (prev) => {
87 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
88 return prev
89 })
90 expectTypeOf(data).toEqualTypeOf<number | undefined>()
91 })
92 it('should properly type value when passed to setQueryData', () => {
93 const { queryKey } = queryOptions({
94 queryKey: ['key'],
95 queryFn: () => Promise.resolve(5),
96 })
97​
98 const queryClient = new QueryClient()
99​
100 // @ts-expect-error value should be a number
101 queryClient.setQueryData(queryKey, '5')
102 // @ts-expect-error value should be a number
103 queryClient.setQueryData(queryKey, () => '5')
104​
105 const data = queryClient.setQueryData(queryKey, 5)
106​
107 expectTypeOf(data).toEqualTypeOf<number | undefined>()
108 })
109 it('should allow to be passed to QueryClient methods while containing ref in queryKey', () => {
110 const options = queryOptions({
111 queryKey: ['key', ref(1), { nested: ref(2) }],
112 queryFn: () => Promise.resolve(5),
113 })
114​
115 const queryClient = new QueryClient()
116​
117 // Should not error
118 const data = queryClient.invalidateQueries(options)
119 // Should not error
120 const data2 = queryClient.fetchQuery(options)
121​
122 expectTypeOf(data).toEqualTypeOf<Promise<void>>()
123 expectTypeOf(data2).toEqualTypeOf<Promise<number>>()
124 })
125})