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