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