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

inject-query.test-d.ts

257 lines7.7 KB

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})