Open History to see the last commit
1import { signal } from '@angular/core'
2import { sleep } from '@tanstack/query-test-utils'
3import { describe, expectTypeOf, it } from 'vitest'
4import { injectQuery, queryOptions, skipToken, toResource } from '..'
5import type { Resource, Signal } from '@angular/core'
6import type { CreateQueryOptions, CreateQueryResult } from '..'
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 it('supports reading data from a generic result type', () => {
166 const readId = <T extends { id: number }>(query: CreateQueryResult<T>) =>
167 query.data()?.id
168β
169 expectTypeOf(readId).toBeCallableWith(
170 {} as CreateQueryResult<{ id: number }>,
171 )
172 })
173})
174β
175describe('Discriminated union return type', () => {
176 it('should expose status predicates as Angular signals', () => {
177 const query = injectQuery(() => ({
178 queryKey: ['key'],
179 queryFn: () => sleep(0).then(() => 'Some data'),
180 }))
181β
182 expectTypeOf(query.isSuccess).toExtend<Signal<boolean>>()
183 expectTypeOf(query.isError).toExtend<Signal<boolean>>()
184 expectTypeOf(query.isPending).toExtend<Signal<boolean>>()
185 })
186β
187 it('should expose an Angular resource view', () => {
188 const query = injectQuery(() => ({
189 queryKey: ['key'],
190 queryFn: () => sleep(0).then(() => 'Some data'),
191 }))
192β
193 const resource = toResource(query)
194 expectTypeOf(resource).toExtend<Resource<string | undefined>>()
195 expectTypeOf(resource.reload).toBeCallableWith()
196β
197 // @ts-expect-error Resources are created explicitly with toResource.
198 query.resource
199 })
200β
201 it('data should be possibly undefined by default', () => {
202 const query = injectQuery(() => ({
203 queryKey: ['key'],
204 queryFn: () => sleep(0).then(() => 'Some data'),
205 }))
206β
207 expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
208 })
209β
210 it('data should be defined when query is success', () => {
211 const query = injectQuery(() => ({
212 queryKey: ['key'],
213 queryFn: () => sleep(0).then(() => 'Some data'),
214 }))
215β
216 if (query.isSuccess()) {
217 expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
218 expectTypeOf(toResource(query).value).toEqualTypeOf<Signal<string>>()
219 }
220 })
221β
222 it('error should be null when query is success', () => {
223 const query = injectQuery(() => ({
224 queryKey: ['key'],
225 queryFn: () => sleep(0).then(() => 'Some data'),
226 }))
227β
228 if (query.isSuccess()) {
229 expectTypeOf(query.error).toEqualTypeOf<Signal<null>>()
230 }
231 })
232β
233 it('data should be undefined when query is pending', () => {
234 const query = injectQuery(() => ({
235 queryKey: ['key'],
236 queryFn: () => sleep(0).then(() => 'Some data'),
237 }))
238β
239 if (query.isPending()) {
240 expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
241 }
242 })
243β
244 it('error should be defined when query is error', () => {
245 const query = injectQuery(() => ({
246 queryKey: ['key'],
247 queryFn: () => sleep(0).then(() => 'Some data'),
248 }))
249β
250 if (query.isError()) {
251 expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
252 }
253 })
254})
255β
256describe('injectQuery options', () => {
257 it('omits observer error reporting options', () => {
258 expectTypeOf<CreateQueryOptions>().not.toHaveProperty('throwOnError')
259 })
260})
261β
262describe('skipToken', () => {
263 it('should narrow data to string | undefined for a conditional skipToken inside a whole-options getter', () => {
264 const postId = signal<number | undefined>(undefined)
265 const query = injectQuery(() => ({
266 queryKey: ['posts', postId()],
267 queryFn:
268 postId() != null
269 ? () => Promise.resolve(`post ${postId()}`)
270 : skipToken,
271 }))
272β
273 expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
274 })
275})