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

useQueries.test-d.tsx

171 lines4.7 KB

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