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

useQueries.test-d.tsx

170 lines4.6 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { skipToken } from '..'
3import { useQueries } from '../useQueries'
4import { queryOptions } from '../queryOptions'
5import type { OmitKeyof } from '..'
6import type { UseQueryOptions, UseQueryResult } from '../types'
7​
8describe('UseQueries config object overload', () => {
9 it('TData should always be defined when initialData is provided as an object', () => {
10 const query1 = {
11 queryKey: ['key1'],
12 queryFn: () => {
13 return {
14 wow: true,
15 }
16 },
17 initialData: {
18 wow: false,
19 },
20 }
21​
22 const query2 = {
23 queryKey: ['key2'],
24 queryFn: () => 'Query Data',
25 initialData: 'initial data',
26 }
27​
28 const query3 = {
29 queryKey: ['key2'],
30 queryFn: () => 'Query Data',
31 }
32​
33 const queryResults = useQueries({ queries: [query1, query2, query3] })
34​
35 const query1Data = queryResults[0].data
36 const query2Data = queryResults[1].data
37 const query3Data = queryResults[2].data
38​
39 expectTypeOf(query1Data).toEqualTypeOf<{ wow: boolean }>()
40 expectTypeOf(query2Data).toEqualTypeOf<string>()
41 expectTypeOf(query3Data).toEqualTypeOf<string | undefined>()
42 })
43​
44 it('TData should be defined when passed through queryOptions', () => {
45 const options = queryOptions({
46 queryKey: ['key'],
47 queryFn: () => {
48 return {
49 wow: true,
50 }
51 },
52 initialData: {
53 wow: true,
54 },
55 })
56 const queryResults = useQueries({ queries: [options] })
57​
58 const data = queryResults[0].data
59​
60 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
61 })
62​
63 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
64 const query1 = queryOptions({
65 queryKey: ['key'],
66 queryFn: () => Promise.resolve(1),
67 select: (data) => data > 1,
68 })
69​
70 const query2 = {
71 queryKey: ['key'],
72 queryFn: () => Promise.resolve(1),
73 select: (data: number) => data > 1,
74 }
75​
76 const queryResults = useQueries({ queries: [query1, query2] })
77 const query1Data = queryResults[0].data
78 const query2Data = queryResults[1].data
79​
80 expectTypeOf(query1Data).toEqualTypeOf<boolean | undefined>()
81 expectTypeOf(query2Data).toEqualTypeOf<boolean | undefined>()
82 })
83​
84 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
85 const queryResults = useQueries({
86 queries: [
87 {
88 queryKey: ['key'],
89 queryFn: () => {
90 return {
91 wow: true,
92 }
93 },
94 initialData: () => undefined as { wow: boolean } | undefined,
95 },
96 ],
97 })
98​
99 const data = queryResults[0].data
100​
101 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
102 })
103​
104 describe('custom hook', () => {
105 it('should allow custom hooks using UseQueryOptions', () => {
106 type Data = string
107​
108 const useCustomQueries = (
109 options?: OmitKeyof<UseQueryOptions<Data>, 'queryKey' | 'queryFn'>,
110 ) => {
111 return useQueries({
112 queries: [
113 {
114 ...options,
115 queryKey: ['todos-key'],
116 queryFn: () => Promise.resolve('data'),
117 },
118 ],
119 })
120 }
121​
122 const queryResults = useCustomQueries()
123 const data = queryResults[0].data
124​
125 expectTypeOf(data).toEqualTypeOf<Data | undefined>()
126 })
127 })
128​
129 it('TData should have correct type when conditional skipToken is passed', () => {
130 const queryResults = useQueries({
131 queries: [
132 {
133 queryKey: ['withSkipToken'],
134 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
135 },
136 ],
137 })
138​
139 const firstResult = queryResults[0]
140​
141 expectTypeOf(firstResult).toEqualTypeOf<UseQueryResult<number, Error>>()
142 expectTypeOf(firstResult.data).toEqualTypeOf<number | undefined>()
143 })
144​
145 it('should return correct data for dynamic queries with mixed result types', () => {
146 const Queries1 = {
147 get: () =>
148 queryOptions({
149 queryKey: ['key1'],
150 queryFn: () => Promise.resolve(1),
151 }),
152 }
153 const Queries2 = {
154 get: () =>
155 queryOptions({
156 queryKey: ['key2'],
157 queryFn: () => Promise.resolve(true),
158 }),
159 }
160​
161 const queries1List = [1, 2, 3].map(() => ({ ...Queries1.get() }))
162 const result = useQueries({
163 queries: [...queries1List, { ...Queries2.get() }],
164 })
165​
166 expectTypeOf(result).toEqualTypeOf<
167 [...Array<UseQueryResult<number, Error>>, UseQueryResult<boolean, Error>]
168 >()
169 })
170})