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

infiniteQueryOptions.test-d.ts

165 lines5.1 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { dataTagSymbol } from '@tanstack/query-core'
3import { reactive } from 'vue-demi'
4import { queryKey } from '@tanstack/query-test-utils'
5import { infiniteQueryOptions } from '../infiniteQueryOptions'
6import { QueryClient } from '../queryClient'
7import { useInfiniteQuery } from '../useInfiniteQuery'
8import type { InfiniteData } from '@tanstack/query-core'
9​
10// Regression test for exported infiniteQueryOptions inference under declaration emit.
11// TypeScript should be able to name the return type without expanding the
12// internal data tag symbols into the consumer's .d.ts output.
13export const exportedInfiniteQueryOptions = infiniteQueryOptions({
14 queryKey: ['invalid'],
15 getNextPageParam: () => 1,
16 initialPageParam: 1,
17})
18​
19describe('infiniteQueryOptions', () => {
20 it('should not allow excess properties', () => {
21 const key = queryKey()
22 assertType(
23 infiniteQueryOptions({
24 queryKey: key,
25 queryFn: () => Promise.resolve('data'),
26 getNextPageParam: () => 1,
27 initialPageParam: 1,
28 // @ts-expect-error this is a good error, because stallTime does not exist!
29 stallTime: 1000,
30 }),
31 )
32 })
33 it('should infer types for callbacks', () => {
34 const key = queryKey()
35 infiniteQueryOptions({
36 queryKey: key,
37 queryFn: () => Promise.resolve('data'),
38 staleTime: 1000,
39 getNextPageParam: () => 1,
40 initialPageParam: 1,
41 select: (data) => {
42 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
43 },
44 })
45 })
46 it('should work when passed to useInfiniteQuery', () => {
47 const key = queryKey()
48 const options = infiniteQueryOptions({
49 queryKey: key,
50 queryFn: () => Promise.resolve('string'),
51 getNextPageParam: () => 1,
52 initialPageParam: 1,
53 })
54​
55 const { data } = reactive(useInfiniteQuery(options))
56​
57 expectTypeOf(data).toEqualTypeOf<
58 InfiniteData<string, unknown> | undefined
59 >()
60 })
61 it('should work when passed to infiniteQuery', async () => {
62 const options = infiniteQueryOptions({
63 queryKey: ['key'],
64 queryFn: () => Promise.resolve('string'),
65 getNextPageParam: () => 1,
66 initialPageParam: 1,
67 })
68​
69 const data = await new QueryClient().infiniteQuery({
70 ...options,
71 staleTime: 0,
72 pages: 1,
73 })
74​
75 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
76 })
77 it('should work when passed to infiniteQuery with select', async () => {
78 const options = infiniteQueryOptions({
79 queryKey: ['key'],
80 queryFn: () => Promise.resolve('string'),
81 getNextPageParam: () => 1,
82 initialPageParam: 1,
83 select: (data) => data.pages,
84 })
85​
86 const data = await new QueryClient().infiniteQuery({
87 ...options,
88 staleTime: 0,
89 pages: 1,
90 })
91​
92 expectTypeOf(data).toEqualTypeOf<Array<string>>()
93 })
94 it('should tag the queryKey with the result type of the QueryFn', () => {
95 const key = queryKey()
96 const { queryKey: tagged } = infiniteQueryOptions({
97 queryKey: key,
98 queryFn: () => Promise.resolve('string'),
99 getNextPageParam: () => 1,
100 initialPageParam: 1,
101 })
102​
103 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
104 })
105 it('should tag the queryKey even if no promise is returned', () => {
106 const key = queryKey()
107 const { queryKey: tagged } = infiniteQueryOptions({
108 queryKey: key,
109 queryFn: () => 'string',
110 getNextPageParam: () => 1,
111 initialPageParam: 1,
112 })
113​
114 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
115 })
116 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
117 const key = queryKey()
118 const { queryKey: tagged } = infiniteQueryOptions({
119 queryKey: key,
120 queryFn: () => Promise.resolve('string'),
121 select: (data) => data.pages,
122 getNextPageParam: () => 1,
123 initialPageParam: 1,
124 })
125​
126 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
127 })
128 it('should return the proper type when passed to getQueryData', () => {
129 const key = queryKey()
130 const { queryKey: tagged } = infiniteQueryOptions({
131 queryKey: key,
132 queryFn: () => Promise.resolve('string'),
133 getNextPageParam: () => 1,
134 initialPageParam: 1,
135 })
136​
137 const queryClient = new QueryClient()
138 const data = queryClient.getQueryData(tagged)
139​
140 expectTypeOf(data).toEqualTypeOf<
141 InfiniteData<string, unknown> | undefined
142 >()
143 })
144 it('should properly type when passed to setQueryData', () => {
145 const key = queryKey()
146 const { queryKey: tagged } = infiniteQueryOptions({
147 queryKey: key,
148 queryFn: () => Promise.resolve('string'),
149 getNextPageParam: () => 1,
150 initialPageParam: 1,
151 })
152​
153 const queryClient = new QueryClient()
154 const data = queryClient.setQueryData(tagged, (prev) => {
155 expectTypeOf(prev).toEqualTypeOf<
156 InfiniteData<string, unknown> | undefined
157 >()
158 return prev
159 })
160​
161 expectTypeOf(data).toEqualTypeOf<
162 InfiniteData<string, unknown> | undefined
163 >()
164 })
165})