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

inject-queries.test-d.ts

261 lines6.9 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { injectQueries, skipToken } from '..'
3import { queryOptions } from '../query-options'
4import type { CreateQueryOptions, CreateQueryResult, OmitKeyof } from '..'
5import type { Signal } from '@angular/core'
6​
7describe('InjectQueries config object overload', () => {
8 it('TData should always be defined when initialData is provided as an object', () => {
9 const query1 = {
10 queryKey: ['key1'],
11 queryFn: () => {
12 return {
13 wow: true,
14 }
15 },
16 initialData: {
17 wow: false,
18 },
19 }
20​
21 const query2 = {
22 queryKey: ['key2'],
23 queryFn: () => 'Query Data',
24 initialData: 'initial data',
25 }
26​
27 const query3 = {
28 queryKey: ['key2'],
29 queryFn: () => 'Query Data',
30 }
31​
32 const queryResults = injectQueries(() => ({
33 queries: [query1, query2, query3],
34 }))
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: ['key'],
48 queryFn: () => {
49 return {
50 wow: true,
51 }
52 },
53 initialData: {
54 wow: true,
55 },
56 })
57 const queryResults = injectQueries(() => ({ 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 injectQuery', () => {
65 const query1 = queryOptions({
66 queryKey: ['key'],
67 queryFn: () => Promise.resolve(1),
68 select: (data) => data > 1,
69 })
70​
71 const query2 = {
72 queryKey: ['key'],
73 queryFn: () => Promise.resolve(1),
74 select: (data: number) => data > 1,
75 }
76​
77 const queryResults = injectQueries(() => ({ 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 = injectQueries(() => ({
87 queries: [
88 {
89 queryKey: ['key'],
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 injectable', () => {
106 it('should allow custom hooks using UseQueryOptions', () => {
107 type Data = string
108​
109 const injectCustomQueries = (
110 options?: OmitKeyof<CreateQueryOptions<Data>, 'queryKey' | 'queryFn'>,
111 ) => {
112 return injectQueries(() => ({
113 queries: [
114 {
115 ...options,
116 queryKey: ['todos-key'],
117 queryFn: () => Promise.resolve('data'),
118 },
119 ],
120 }))
121 }
122​
123 const queryResults = injectCustomQueries()
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 = injectQueries(() => ({
132 queries: [
133 {
134 queryKey: ['withSkipToken'],
135 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
136 },
137 ],
138 }))
139​
140 const firstResult = queryResults()[0]
141​
142 expectTypeOf(firstResult).toEqualTypeOf<CreateQueryResult<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: ['key1'],
151 queryFn: () => Promise.resolve(1),
152 }),
153 }
154 const Queries2 = {
155 get: () =>
156 queryOptions({
157 queryKey: ['key2'],
158 queryFn: () => Promise.resolve(true),
159 }),
160 }
161​
162 const queries1List = [1, 2, 3].map(() => ({ ...Queries1.get() }))
163 const result = injectQueries(() => ({
164 queries: [...queries1List, { ...Queries2.get() }],
165 }))
166​
167 expectTypeOf(result).branded.toEqualTypeOf<
168 Signal<
169 [
170 ...Array<CreateQueryResult<number, Error>>,
171 CreateQueryResult<boolean, Error>,
172 ]
173 >
174 >()
175 })
176})
177​
178describe('InjectQueries combine', () => {
179 it('should provide the correct type for the combine function', () => {
180 injectQueries(() => ({
181 queries: [
182 {
183 queryKey: ['key'],
184 queryFn: () => Promise.resolve(1),
185 },
186 {
187 queryKey: ['key2'],
188 queryFn: () => Promise.resolve(true),
189 },
190 ],
191 combine: (results) => {
192 expectTypeOf(results[0].data).toEqualTypeOf<number | undefined>()
193 expectTypeOf(results[0].refetch).toBeCallableWith()
194 expectTypeOf(results[1].data).toEqualTypeOf<boolean | undefined>()
195 expectTypeOf(results[1].refetch).toBeCallableWith()
196 },
197 }))
198 })
199​
200 it('should provide the correct types on the combined result with initial data', () => {
201 injectQueries(() => ({
202 queries: [
203 {
204 queryKey: ['key'],
205 queryFn: () => Promise.resolve(1),
206 initialData: 1,
207 },
208 ],
209 combine: (results) => {
210 expectTypeOf(results[0].data).toEqualTypeOf<number>()
211 expectTypeOf(results[0].refetch).toBeCallableWith()
212 },
213 }))
214 })
215​
216 it('should provide the correct result type', () => {
217 const queryResults = injectQueries(() => ({
218 queries: [
219 {
220 queryKey: ['key'],
221 queryFn: () => Promise.resolve(1),
222 },
223 {
224 queryKey: ['key2'],
225 queryFn: () => Promise.resolve(true),
226 },
227 ],
228 combine: (results) => ({
229 data: {
230 1: results[0].data,
231 2: results[1].data,
232 },
233 fn: () => {},
234 }),
235 }))
236​
237 expectTypeOf(queryResults).branded.toEqualTypeOf<
238 Signal<{
239 data: {
240 1: number | undefined
241 2: boolean | undefined
242 }
243 fn: () => void
244 }>
245 >()
246 })
247​
248 it('should provide the correct types on the result with initial data', () => {
249 const queryResults = injectQueries(() => ({
250 queries: [
251 {
252 queryKey: ['key'],
253 queryFn: () => Promise.resolve(1),
254 initialData: 1,
255 },
256 ],
257 }))
258​
259 expectTypeOf(queryResults()[0].data()).toEqualTypeOf<number>()
260 })
261})