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

infiniteQueryOptions.test-d.ts

113 lines3.5 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { QueryClient, dataTagSymbol } from '@tanstack/query-core'
3import { reactive } from 'vue-demi'
4import { infiniteQueryOptions } from '../infiniteQueryOptions'
5import { useInfiniteQuery } from '../useInfiniteQuery'
6import type { InfiniteData } from '@tanstack/query-core'
7​
8describe('infiniteQueryOptions', () => {
9 it('should not allow excess properties', () => {
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 it('should infer types for callbacks', () => {
22 infiniteQueryOptions({
23 queryKey: ['key'],
24 queryFn: () => Promise.resolve('data'),
25 staleTime: 1000,
26 getNextPageParam: () => 1,
27 initialPageParam: 1,
28 select: (data) => {
29 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
30 },
31 })
32 })
33 it('should work when passed to useInfiniteQuery', () => {
34 const options = infiniteQueryOptions({
35 queryKey: ['key'],
36 queryFn: () => Promise.resolve('string'),
37 getNextPageParam: () => 1,
38 initialPageParam: 1,
39 })
40​
41 const { data } = reactive(useInfiniteQuery(options))
42​
43 expectTypeOf(data).toEqualTypeOf<
44 InfiniteData<string, unknown> | undefined
45 >()
46 })
47 it('should tag the queryKey with the result type of the QueryFn', () => {
48 const { queryKey } = infiniteQueryOptions({
49 queryKey: ['key'],
50 queryFn: () => Promise.resolve('string'),
51 getNextPageParam: () => 1,
52 initialPageParam: 1,
53 })
54​
55 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
56 })
57 it('should tag the queryKey even if no promise is returned', () => {
58 const { queryKey } = infiniteQueryOptions({
59 queryKey: ['key'],
60 queryFn: () => 'string',
61 getNextPageParam: () => 1,
62 initialPageParam: 1,
63 })
64​
65 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
66 })
67 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
68 const { queryKey } = infiniteQueryOptions({
69 queryKey: ['key'],
70 queryFn: () => Promise.resolve('string'),
71 select: (data) => data.pages,
72 getNextPageParam: () => 1,
73 initialPageParam: 1,
74 })
75​
76 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
77 })
78 it('should return the proper type when passed to getQueryData', () => {
79 const { queryKey } = infiniteQueryOptions({
80 queryKey: ['key'],
81 queryFn: () => Promise.resolve('string'),
82 getNextPageParam: () => 1,
83 initialPageParam: 1,
84 })
85​
86 const queryClient = new QueryClient()
87 const data = queryClient.getQueryData(queryKey)
88​
89 expectTypeOf(data).toEqualTypeOf<
90 InfiniteData<string, unknown> | undefined
91 >()
92 })
93 it('should properly type when passed to setQueryData', () => {
94 const { queryKey } = infiniteQueryOptions({
95 queryKey: ['key'],
96 queryFn: () => Promise.resolve('string'),
97 getNextPageParam: () => 1,
98 initialPageParam: 1,
99 })
100​
101 const queryClient = new QueryClient()
102 const data = queryClient.setQueryData(queryKey, (prev) => {
103 expectTypeOf(prev).toEqualTypeOf<
104 InfiniteData<string, unknown> | undefined
105 >()
106 return prev
107 })
108​
109 expectTypeOf(data).toEqualTypeOf<
110 InfiniteData<string, unknown> | undefined
111 >()
112 })
113})