Skip to content

Commit 68bcbfc

Browse files
committed
tests: stage adapter queryOptions coverage for new query methods
1 parent 261fa24 commit 68bcbfc

12 files changed

Lines changed: 588 additions & 4 deletions

File tree

packages/angular-query-experimental/src/__tests__/infinite-query-options.test-d.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertType, describe, expectTypeOf, it } from 'vitest'
22
import { queryKey } from '@tanstack/query-test-utils'
3-
import { QueryClient, dataTagSymbol } from '@tanstack/query-core'
3+
import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
44
import { infiniteQueryOptions } from '../infinite-query-options'
55
import { injectInfiniteQuery } from '../inject-infinite-query'
66
import { injectQuery } from '../inject-query'
@@ -67,6 +67,61 @@ describe('infiniteQueryOptions', () => {
6767

6868
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
6969
})
70+
71+
it('should work when passed to infiniteQuery', async () => {
72+
const options = infiniteQueryOptions({
73+
queryKey: ['key'],
74+
queryFn: () => Promise.resolve('string'),
75+
getNextPageParam: () => 1,
76+
initialPageParam: 1,
77+
})
78+
79+
const data = await new QueryClient().infiniteQuery(options)
80+
81+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
82+
})
83+
84+
it('should work when passed to infiniteQuery with select', async () => {
85+
const options = infiniteQueryOptions({
86+
queryKey: ['key'],
87+
queryFn: () => Promise.resolve('string'),
88+
getNextPageParam: () => 1,
89+
initialPageParam: 1,
90+
select: (data) => data.pages,
91+
})
92+
93+
const data = await new QueryClient().infiniteQuery(options)
94+
95+
expectTypeOf(data).toEqualTypeOf<Array<string>>()
96+
})
97+
98+
it('should work when passed to infiniteQuery with enabled: false', async () => {
99+
const options = infiniteQueryOptions({
100+
queryKey: ['key'],
101+
queryFn: () => Promise.resolve('string'),
102+
getNextPageParam: () => 1,
103+
initialPageParam: 1,
104+
enabled: false,
105+
})
106+
107+
const data = await new QueryClient().infiniteQuery(options)
108+
109+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
110+
})
111+
112+
it('should work when passed to infiniteQuery with skipToken', async () => {
113+
const options = infiniteQueryOptions({
114+
queryKey: ['key'],
115+
queryFn: skipToken,
116+
getNextPageParam: () => 1,
117+
initialPageParam: 1,
118+
})
119+
120+
const data = await new QueryClient().infiniteQuery(options)
121+
122+
expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
123+
})
124+
70125
it('should tag the queryKey with the result type of the QueryFn', () => {
71126
const key = queryKey()
72127
const { queryKey: tagged } = infiniteQueryOptions({

packages/angular-query-experimental/src/__tests__/query-options.test-d.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { assertType, describe, expectTypeOf, it } from 'vitest'
22
import { queryKey } from '@tanstack/query-test-utils'
3-
import { QueryClient, dataTagSymbol, injectQuery, queryOptions } from '..'
3+
import {
4+
QueryClient,
5+
dataTagSymbol,
6+
injectQuery,
7+
queryOptions,
8+
skipToken,
9+
} from '..'
410
import type { Signal } from '@angular/core'
511

612
describe('queryOptions', () => {
@@ -67,6 +73,48 @@ it('should work when passed to fetchQuery', () => {
6773
assertType<Promise<number>>(data)
6874
})
6975

76+
it('should work when passed to query', () => {
77+
const options = queryOptions({
78+
queryKey: ['key'],
79+
queryFn: () => Promise.resolve(5),
80+
})
81+
82+
const data = new QueryClient().query(options)
83+
assertType<Promise<number>>(data)
84+
})
85+
86+
it('should work when passed to query with select', () => {
87+
const options = queryOptions({
88+
queryKey: ['key'],
89+
queryFn: () => Promise.resolve(5),
90+
select: (data) => data.toString(),
91+
})
92+
93+
const data = new QueryClient().query(options)
94+
assertType<Promise<string>>(data)
95+
})
96+
97+
it('should work when passed to query with enabled: false', () => {
98+
const options = queryOptions({
99+
queryKey: ['key'],
100+
queryFn: () => Promise.resolve(5),
101+
enabled: false,
102+
})
103+
104+
const data = new QueryClient().query(options)
105+
assertType<Promise<number>>(data)
106+
})
107+
108+
it('should work when passed to query with skipToken', () => {
109+
const options = queryOptions({
110+
queryKey: ['key'],
111+
queryFn: skipToken,
112+
})
113+
114+
const data = new QueryClient().query(options)
115+
assertType<Promise<unknown>>(data)
116+
})
117+
70118
it('should tag the queryKey with the result type of the QueryFn', () => {
71119
const key = queryKey()
72120
const { queryKey: tagged } = queryOptions({

packages/lit-query/src/tests/type-inference.test.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
dataTagSymbol,
33
QueryClient,
4+
skipToken,
45
type DefinedQueryObserverResult,
6+
type InfiniteData,
57
type QueryObserverResult,
68
} from '@tanstack/query-core'
79
import { describe, expectTypeOf, it } from 'vitest'
@@ -207,4 +209,118 @@ describe('type inference', () => {
207209
Array<{ page: number }> | undefined
208210
>()
209211
})
212+
213+
it('L3: queryOptions integrates with queryClient.query', async () => {
214+
const options = queryOptions({
215+
queryKey: ['type-inference', 'query'] as const,
216+
queryFn: () => Promise.resolve(5),
217+
})
218+
219+
const data = await new QueryClient().query(options)
220+
expectTypeOf(data).toEqualTypeOf<number>()
221+
})
222+
223+
it('L4: queryOptions with select integrates with queryClient.query', async () => {
224+
const options = queryOptions({
225+
queryKey: ['type-inference', 'query-select'] as const,
226+
queryFn: () => Promise.resolve(5),
227+
select: (data) => data.toString(),
228+
})
229+
230+
const data = await new QueryClient().query(options)
231+
expectTypeOf(data).toEqualTypeOf<string>()
232+
})
233+
234+
it('L5: queryOptions with enabled: false integrates with queryClient.query', async () => {
235+
const options = queryOptions({
236+
queryKey: ['type-inference', 'query-enabled-false'] as const,
237+
queryFn: () => Promise.resolve(5),
238+
enabled: false,
239+
})
240+
241+
const client = new QueryClient()
242+
// Disabled imperative queries require cached data; otherwise query() throws before type assertions run.
243+
client.setQueryData(options.queryKey, 5)
244+
245+
const data = await client.query(options)
246+
expectTypeOf(data).toEqualTypeOf<number>()
247+
})
248+
249+
it('L6: queryOptions with skipToken integrates with queryClient.query', async () => {
250+
const options = queryOptions({
251+
queryKey: ['type-inference', 'query-skip-token'] as const,
252+
queryFn: skipToken,
253+
})
254+
255+
const client = new QueryClient()
256+
// skipToken disables the query, so seed matching data to exercise the return type.
257+
client.setQueryData(options.queryKey, 5)
258+
259+
const data = await client.query(options)
260+
expectTypeOf(data).toEqualTypeOf<unknown>()
261+
})
262+
263+
it('L7: infiniteQueryOptions integrates with queryClient.infiniteQuery', async () => {
264+
const options = infiniteQueryOptions({
265+
queryKey: ['type-inference', 'infinite-query'] as const,
266+
queryFn: () => Promise.resolve('data'),
267+
getNextPageParam: () => 1,
268+
initialPageParam: 1,
269+
})
270+
271+
const data = await new QueryClient().infiniteQuery(options)
272+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
273+
})
274+
275+
it('L8: infiniteQueryOptions with select integrates with queryClient.infiniteQuery', async () => {
276+
const options = infiniteQueryOptions({
277+
queryKey: ['type-inference', 'infinite-query-select'] as const,
278+
queryFn: () => Promise.resolve('data'),
279+
getNextPageParam: () => 1,
280+
initialPageParam: 1,
281+
select: (data) => data.pages,
282+
})
283+
284+
const data = await new QueryClient().infiniteQuery(options)
285+
expectTypeOf(data).toEqualTypeOf<Array<string>>()
286+
})
287+
288+
it('L9: infiniteQueryOptions with enabled: false integrates with queryClient.infiniteQuery', async () => {
289+
const options = infiniteQueryOptions({
290+
queryKey: ['type-inference', 'infinite-query-enabled-false'] as const,
291+
queryFn: () => Promise.resolve('data'),
292+
getNextPageParam: () => 1,
293+
initialPageParam: 1,
294+
enabled: false,
295+
})
296+
297+
const client = new QueryClient()
298+
// Disabled imperative infinite queries require cached data to avoid throwing before type assertions.
299+
client.setQueryData(options.queryKey, {
300+
pages: ['data'],
301+
pageParams: [1],
302+
})
303+
304+
const data = await client.infiniteQuery(options)
305+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
306+
})
307+
308+
it('L10: infiniteQueryOptions with skipToken integrates with queryClient.infiniteQuery', async () => {
309+
const options = infiniteQueryOptions({
310+
queryKey: ['type-inference', 'infinite-query-skip-token'] as const,
311+
queryFn: skipToken,
312+
getNextPageParam: () => 1,
313+
initialPageParam: 1,
314+
})
315+
316+
const client = new QueryClient()
317+
// skipToken disables the infinite query, so seed data matching the InfiniteData shape.
318+
client.setQueryData(options.queryKey, {
319+
pages: ['data'],
320+
pageParams: [1],
321+
})
322+
323+
const data = await client.infiniteQuery(options)
324+
expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
325+
})
210326
})

packages/preact-query/src/__tests__/infiniteQueryOptions.test-d.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,56 @@ describe('infiniteQueryOptions', () => {
6464

6565
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
6666
})
67+
it('should work when passed to infiniteQuery', async () => {
68+
const options = infiniteQueryOptions({
69+
queryKey: ['key'],
70+
queryFn: () => Promise.resolve('string'),
71+
getNextPageParam: () => 1,
72+
initialPageParam: 1,
73+
})
74+
75+
const data = await new QueryClient().infiniteQuery(options)
76+
77+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
78+
})
79+
it('should work when passed to infiniteQuery with select', async () => {
80+
const options = infiniteQueryOptions({
81+
queryKey: ['key'],
82+
queryFn: () => Promise.resolve('string'),
83+
getNextPageParam: () => 1,
84+
initialPageParam: 1,
85+
select: (data) => data.pages,
86+
})
87+
88+
const data = await new QueryClient().infiniteQuery(options)
89+
90+
expectTypeOf(data).toEqualTypeOf<Array<string>>()
91+
})
92+
it('should work when passed to infiniteQuery with enabled: false', async () => {
93+
const options = infiniteQueryOptions({
94+
queryKey: ['key'],
95+
queryFn: () => Promise.resolve('string'),
96+
getNextPageParam: () => 1,
97+
initialPageParam: 1,
98+
enabled: false,
99+
})
100+
101+
const data = await new QueryClient().infiniteQuery(options)
102+
103+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
104+
})
105+
it('should work when passed to infiniteQuery with skipToken', async () => {
106+
const options = infiniteQueryOptions({
107+
queryKey: ['key'],
108+
queryFn: skipToken,
109+
getNextPageParam: () => 1,
110+
initialPageParam: 1,
111+
})
112+
113+
const data = await new QueryClient().infiniteQuery(options)
114+
115+
expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
116+
})
67117
it('should work when passed to fetchInfiniteQuery', async () => {
68118
const options = infiniteQueryOptions({
69119
queryKey: queryKey(),

packages/preact-query/src/__tests__/queryOptions.test-d.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,44 @@ describe('queryOptions', () => {
5757
const { data } = useSuspenseQuery(options)
5858
expectTypeOf(data).toEqualTypeOf<number>()
5959
})
60+
it('should work when passed to query', async () => {
61+
const options = queryOptions({
62+
queryKey: ['key'],
63+
queryFn: () => Promise.resolve(5),
64+
})
65+
66+
const data = await new QueryClient().query(options)
67+
expectTypeOf(data).toEqualTypeOf<number>()
68+
})
69+
it('should work when passed to query with select', async () => {
70+
const options = queryOptions({
71+
queryKey: ['key'],
72+
queryFn: () => Promise.resolve(5),
73+
select: (data) => data.toString(),
74+
})
75+
76+
const data = await new QueryClient().query(options)
77+
expectTypeOf(data).toEqualTypeOf<string>()
78+
})
79+
it('should work when passed to query with enabled: false', async () => {
80+
const options = queryOptions({
81+
queryKey: ['key'],
82+
queryFn: () => Promise.resolve(5),
83+
enabled: false,
84+
})
85+
86+
const data = await new QueryClient().query(options)
87+
expectTypeOf(data).toEqualTypeOf<number>()
88+
})
89+
it('should work when passed to query with skipToken', async () => {
90+
const options = queryOptions({
91+
queryKey: ['key'],
92+
queryFn: skipToken,
93+
})
94+
95+
const data = await new QueryClient().query(options)
96+
expectTypeOf(data).toEqualTypeOf<unknown>()
97+
})
6098

6199
it('should work when passed to fetchQuery', async () => {
62100
const options = queryOptions({

0 commit comments

Comments
 (0)