Open History to see the last commit
1import { signal } from '@angular/core'
2import { describe, expectTypeOf, it } from 'vitest'
3import { sleep } from '@tanstack/query-test-utils'
4import { injectQuery, queryOptions, skipToken, toResource } from '..'
5import type { CreateQueryOptions, CreateQueryResult } from '..'
6import type { Resource, Signal } from '@angular/core'
7β
8describe('initialData', () => {
9 describe('Config object overload', () => {
10 it('TData should always be defined when initialData is provided as an object', () => {
11 const query = injectQuery(() => ({
12 queryKey: ['key'],
13 queryFn: () => ({ wow: true }),
14 initialData: { wow: true },
15 }))
16 const resource = toResource(query)
17β
18 expectTypeOf(query.data).toEqualTypeOf<Signal<{ wow: boolean }>>()
19 expectTypeOf(resource.value).toEqualTypeOf<Signal<{ wow: boolean }>>()
20 })
21β
22 it('TData should be defined when passed through queryOptions', () => {
23 const options = () =>
24 queryOptions({
25 queryKey: ['key'],
26 queryFn: () => {
27 return {
28 wow: true,
29 }
30 },
31 initialData: {
32 wow: true,
33 },
34 })
35 const { data } = injectQuery(options)
36β
37 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
38 })
39β
40 it('should support selection function with select', () => {
41 const options = injectQuery(() => ({
42 queryKey: ['key'],
43 queryFn: () => '1',
44 select: (data) => {
45 expectTypeOf(data).toEqualTypeOf<string>()
46 return parseInt(data)
47 },
48 }))
49 expectTypeOf(options.data).toEqualTypeOf<Signal<number | undefined>>()
50 })
51β
52 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
53 const options = queryOptions({
54 queryKey: ['key'],
55 queryFn: () => Promise.resolve(1),
56 })
57β
58 const query = injectQuery(() => ({
59 ...options,
60 select: (data) => data > 1,
61 }))
62β
63 expectTypeOf(query.data).toEqualTypeOf<Signal<boolean | undefined>>()
64 })
65β
66 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
67 const { data } = injectQuery(() => ({
68 queryKey: ['key'],
69 queryFn: () => {
70 return {
71 wow: true,
72 }
73 },
74 initialData: () => ({
75 wow: true,
76 }),
77 }))
78β
79 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
80 })
81β
82 it('TData should have undefined in the union when initialData is NOT provided', () => {
83 const { data } = injectQuery(() => ({
84 queryKey: ['key'],
85 queryFn: () => {
86 return {
87 wow: true,
88 }
89 },
90 }))
91β
92 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
93 })
94β
95 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
96 const { data } = injectQuery(() => ({
97 queryKey: ['key'],
98 queryFn: () => {
99 return {
100 wow: true,
101 }
102 },
103 initialData: () => undefined as { wow: boolean } | undefined,
104 }))
105β
106 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
107 })
108β
109 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
110 const query = injectQuery(() => ({
111 queryKey: ['key'],
112 queryFn: () => {
113 return {
114 wow: true,
115 }
116 },
117 initialData: () => undefined as { wow: boolean } | undefined,
118 }))
119β
120 if (query.isSuccess()) {
121 expectTypeOf(query.data).toEqualTypeOf<Signal<{ wow: boolean }>>()
122 }
123 })
124β
125 it('should keep data defined after an isError check when initialData is provided', () => {
126 const query = injectQuery(() => ({
127 queryKey: ['key'],
128 queryFn: () => Promise.resolve('Some data'),
129 initialData: 'initial data',
130 }))
131β
132 if (query.isError()) {
133 expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
134 }
135 })
136β
137 it('should make an isPending branch impossible when initialData is provided', () => {
138 const query = injectQuery(() => ({
139 queryKey: ['key'],
140 queryFn: () => Promise.resolve('Some data'),
141 initialData: 'initial data',
142 }))
143β
144 if (query.isPending()) {
145 expectTypeOf(query).toEqualTypeOf<never>()
146 }
147 })
148 })
149β
150 describe('structuralSharing', () => {
151 it('should be able to use structuralSharing with unknown types', () => {
152 // https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
153 injectQuery(() => ({
154 queryKey: ['key'],
155 queryFn: () => 5,
156 structuralSharing: (oldData, newData) => {
157 expectTypeOf(oldData).toBeUnknown()
158 expectTypeOf(newData).toBeUnknown()
159 return newData
160 },
161 }))
162 })
163 })
164})
165β
166it('supports reading data from a generic result type', () => {
167 const readId = <T extends { id: number }>(query: CreateQueryResult<T>) =>
168 query.data()?.id
169β
170 expectTypeOf(readId).toBeCallableWith({} as CreateQueryResult<{ id: number }>)
171})
172β
173describe('Discriminated union return type', () => {
174 it('should expose status predicates as Angular signals', () => {
175 const query = injectQuery(() => ({
176 queryKey: ['key'],
177 queryFn: () => sleep(0).then(() => 'Some data'),
178 }))
179β
180 expectTypeOf(query.isSuccess).toExtend<Signal<boolean>>()
181 expectTypeOf(query.isError).toExtend<Signal<boolean>>()
182 expectTypeOf(query.isPending).toExtend<Signal<boolean>>()
183 })
184β
185 it('should expose an Angular resource view', () => {
186 const query = injectQuery(() => ({
187 queryKey: ['key'],
188 queryFn: () => sleep(0).then(() => 'Some data'),
189 }))
190β
191 const resource = toResource(query)
192 expectTypeOf(resource).toExtend<Resource<string | undefined>>()
193 expectTypeOf(resource.reload).toBeCallableWith()
194β
195 // @ts-expect-error Resources are created explicitly with toResource.
196 query.resource
197 })
198β
199 it('data should be possibly undefined by default', () => {
200 const query = injectQuery(() => ({
201 queryKey: ['key'],
202 queryFn: () => sleep(0).then(() => 'Some data'),
203 }))
204β
205 expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
206 })
207β
208 it('data should be defined when query is success', () => {
209 const query = injectQuery(() => ({
210 queryKey: ['key'],
211 queryFn: () => sleep(0).then(() => 'Some data'),
212 }))
213β
214 if (query.isSuccess()) {
215 expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
216 expectTypeOf(toResource(query).value).toEqualTypeOf<Signal<string>>()
217 }
218 })
219β
220 it('error should be null when query is success', () => {
221 const query = injectQuery(() => ({
222 queryKey: ['key'],
223 queryFn: () => sleep(0).then(() => 'Some data'),
224 }))
225β
226 if (query.isSuccess()) {
227 expectTypeOf(query.error).toEqualTypeOf<Signal<null>>()
228 }
229 })
230β
231 it('data should be undefined when query is pending', () => {
232 const query = injectQuery(() => ({
233 queryKey: ['key'],
234 queryFn: () => sleep(0).then(() => 'Some data'),
235 }))
236β
237 if (query.isPending()) {
238 expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
239 }
240 })
241β
242 it('error should be defined when query is error', () => {
243 const query = injectQuery(() => ({
244 queryKey: ['key'],
245 queryFn: () => sleep(0).then(() => 'Some data'),
246 }))
247β
248 if (query.isError()) {
249 expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
250 }
251 })
252})
253β
254describe('injectQuery options', () => {
255 it('omits observer error reporting options', () => {
256 expectTypeOf<CreateQueryOptions>().not.toHaveProperty('throwOnError')
257 })
258})
259β
260describe('skipToken', () => {
261 it('should narrow data to string | undefined for a conditional skipToken inside a whole-options getter', () => {
262 const postId = signal<number | undefined>(undefined)
263 const query = injectQuery(() => ({
264 queryKey: ['posts', postId()],
265 queryFn:
266 postId() != null
267 ? () => Promise.resolve(`post ${postId()}`)
268 : skipToken,
269 }))
270β
271 expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
272 })
273})