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

infiniteQueryOptions.test-d.ts

123 lines3.8 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​
10describe('infiniteQueryOptions', () => {
11 it('should not allow excess properties', () => {
12 const key = queryKey()
13 assertType(
14 infiniteQueryOptions({
15 queryKey: key,
16 queryFn: () => Promise.resolve('data'),
17 getNextPageParam: () => 1,
18 initialPageParam: 1,
19 // @ts-expect-error this is a good error, because stallTime does not exist!
20 stallTime: 1000,
21 }),
22 )
23 })
24 it('should infer types for callbacks', () => {
25 const key = queryKey()
26 infiniteQueryOptions({
27 queryKey: key,
28 queryFn: () => Promise.resolve('data'),
29 staleTime: 1000,
30 getNextPageParam: () => 1,
31 initialPageParam: 1,
32 select: (data) => {
33 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
34 },
35 })
36 })
37 it('should work when passed to useInfiniteQuery', () => {
38 const key = queryKey()
39 const options = infiniteQueryOptions({
40 queryKey: key,
41 queryFn: () => Promise.resolve('string'),
42 getNextPageParam: () => 1,
43 initialPageParam: 1,
44 })
45​
46 const { data } = reactive(useInfiniteQuery(options))
47​
48 expectTypeOf(data).toEqualTypeOf<
49 InfiniteData<string, unknown> | undefined
50 >()
51 })
52 it('should tag the queryKey with the result type of the QueryFn', () => {
53 const key = queryKey()
54 const { queryKey: tagged } = infiniteQueryOptions({
55 queryKey: key,
56 queryFn: () => Promise.resolve('string'),
57 getNextPageParam: () => 1,
58 initialPageParam: 1,
59 })
60​
61 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
62 })
63 it('should tag the queryKey even if no promise is returned', () => {
64 const key = queryKey()
65 const { queryKey: tagged } = infiniteQueryOptions({
66 queryKey: key,
67 queryFn: () => 'string',
68 getNextPageParam: () => 1,
69 initialPageParam: 1,
70 })
71​
72 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
73 })
74 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
75 const key = queryKey()
76 const { queryKey: tagged } = infiniteQueryOptions({
77 queryKey: key,
78 queryFn: () => Promise.resolve('string'),
79 select: (data) => data.pages,
80 getNextPageParam: () => 1,
81 initialPageParam: 1,
82 })
83​
84 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
85 })
86 it('should return the proper type when passed to getQueryData', () => {
87 const key = queryKey()
88 const { queryKey: tagged } = infiniteQueryOptions({
89 queryKey: key,
90 queryFn: () => Promise.resolve('string'),
91 getNextPageParam: () => 1,
92 initialPageParam: 1,
93 })
94​
95 const queryClient = new QueryClient()
96 const data = queryClient.getQueryData(tagged)
97​
98 expectTypeOf(data).toEqualTypeOf<
99 InfiniteData<string, unknown> | undefined
100 >()
101 })
102 it('should properly type when passed to setQueryData', () => {
103 const key = queryKey()
104 const { queryKey: tagged } = infiniteQueryOptions({
105 queryKey: key,
106 queryFn: () => Promise.resolve('string'),
107 getNextPageParam: () => 1,
108 initialPageParam: 1,
109 })
110​
111 const queryClient = new QueryClient()
112 const data = queryClient.setQueryData(tagged, (prev) => {
113 expectTypeOf(prev).toEqualTypeOf<
114 InfiniteData<string, unknown> | undefined
115 >()
116 return prev
117 })
118​
119 expectTypeOf(data).toEqualTypeOf<
120 InfiniteData<string, unknown> | undefined
121 >()
122 })
123})