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

infiniteQueryOptions.test-d.ts

120 lines3.6 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { QueryClient, skipToken } from '@tanstack/query-core'
3import { queryKey } from '@tanstack/query-test-utils'
4import { createInfiniteQuery, infiniteQueryOptions } from '../src/index.js'
5import type { InfiniteData } from '@tanstack/query-core'
6​
7describe('infiniteQueryOptions', () => {
8 it('should not allow excess properties', () => {
9 const key = queryKey()
10 assertType(
11 infiniteQueryOptions({
12 queryKey: key,
13 queryFn: () => Promise.resolve('data'),
14 getNextPageParam: () => 1,
15 initialPageParam: 1,
16 // @ts-expect-error this is a good error, because stallTime does not exist!
17 stallTime: 1000,
18 }),
19 )
20 })
21​
22 it('should infer types for callbacks', () => {
23 const key = queryKey()
24 infiniteQueryOptions({
25 queryKey: key,
26 queryFn: () => Promise.resolve('data'),
27 staleTime: 1000,
28 getNextPageParam: () => 1,
29 initialPageParam: 1,
30 select: (data) => {
31 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
32 },
33 })
34 })
35​
36 it('should work when passed to createInfiniteQuery', () => {
37 const key = queryKey()
38 const options = infiniteQueryOptions({
39 queryKey: key,
40 queryFn: () => Promise.resolve('string'),
41 getNextPageParam: () => 1,
42 initialPageParam: 1,
43 })
44​
45 const query = createInfiniteQuery(() => options)
46​
47 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
48 expectTypeOf(query.data).toEqualTypeOf<
49 InfiniteData<string, unknown> | undefined
50 >()
51 })
52​
53 it('should work when passed to infiniteQuery', async () => {
54 const options = infiniteQueryOptions({
55 queryKey: ['key'],
56 queryFn: () => Promise.resolve('string'),
57 getNextPageParam: () => 1,
58 initialPageParam: 1,
59 })
60​
61 const data = await new QueryClient().infiniteQuery(options)
62​
63 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
64 })
65​
66 it('should work when passed to infiniteQuery with select', async () => {
67 const options = infiniteQueryOptions({
68 queryKey: ['key'],
69 queryFn: () => Promise.resolve('string'),
70 getNextPageParam: () => 1,
71 initialPageParam: 1,
72 select: (data) => data.pages,
73 })
74​
75 const data = await new QueryClient().infiniteQuery(options)
76​
77 expectTypeOf(data).toEqualTypeOf<Array<string>>()
78 })
79​
80 it('should work when passed to infiniteQuery with enabled: false', async () => {
81 const options = infiniteQueryOptions({
82 queryKey: ['key'],
83 queryFn: () => Promise.resolve('string'),
84 getNextPageParam: () => 1,
85 initialPageParam: 1,
86 enabled: false,
87 })
88​
89 const data = await new QueryClient().infiniteQuery(options)
90​
91 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
92 })
93​
94 it('should work when passed to infiniteQuery with skipToken', async () => {
95 const options = infiniteQueryOptions({
96 queryKey: ['key'],
97 queryFn: skipToken,
98 getNextPageParam: () => 1,
99 initialPageParam: 1,
100 })
101​
102 const data = await new QueryClient().infiniteQuery(options)
103​
104 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
105 })
106​
107 it('should work when passed to fetchInfiniteQuery', async () => {
108 const key = queryKey()
109 const options = infiniteQueryOptions({
110 queryKey: key,
111 queryFn: () => Promise.resolve('string'),
112 getNextPageParam: () => 1,
113 initialPageParam: 1,
114 })
115​
116 const data = await new QueryClient().fetchInfiniteQuery(options)
117​
118 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
119 })
120})