Open History to see the last commit
1import { 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 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 } = reactive(useInfiniteQuery(options))
40β
41 expectTypeOf(data).toEqualTypeOf<
42 InfiniteData<string, unknown> | undefined
43 >()
44 })
45 it('should tag the queryKey with the result type of the QueryFn', () => {
46 const { queryKey } = infiniteQueryOptions({
47 queryKey: ['key'],
48 queryFn: () => Promise.resolve('string'),
49 getNextPageParam: () => 1,
50 initialPageParam: 1,
51 })
52β
53 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
54 })
55 it('should tag the queryKey even if no promise is returned', () => {
56 const { queryKey } = infiniteQueryOptions({
57 queryKey: ['key'],
58 queryFn: () => 'string',
59 getNextPageParam: () => 1,
60 initialPageParam: 1,
61 })
62β
63 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
64 })
65 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
66 const { queryKey } = infiniteQueryOptions({
67 queryKey: ['key'],
68 queryFn: () => Promise.resolve('string'),
69 select: (data) => data.pages,
70 getNextPageParam: () => 1,
71 initialPageParam: 1,
72 })
73β
74 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
75 })
76 it('should return the proper type when passed to getQueryData', () => {
77 const { queryKey } = infiniteQueryOptions({
78 queryKey: ['key'],
79 queryFn: () => Promise.resolve('string'),
80 getNextPageParam: () => 1,
81 initialPageParam: 1,
82 })
83β
84 const queryClient = new QueryClient()
85 const data = queryClient.getQueryData(queryKey)
86β
87 expectTypeOf(data).toEqualTypeOf<
88 InfiniteData<string, unknown> | undefined
89 >()
90 })
91 it('should properly type when passed to setQueryData', () => {
92 const { queryKey } = infiniteQueryOptions({
93 queryKey: ['key'],
94 queryFn: () => Promise.resolve('string'),
95 getNextPageParam: () => 1,
96 initialPageParam: 1,
97 })
98β
99 const queryClient = new QueryClient()
100 const data = queryClient.setQueryData(queryKey, (prev) => {
101 expectTypeOf(prev).toEqualTypeOf<
102 InfiniteData<string, unknown> | undefined
103 >()
104 return prev
105 })
106β
107 expectTypeOf(data).toEqualTypeOf<
108 InfiniteData<string, unknown> | undefined
109 >()
110 })
111})