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