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

infiniteQueryOptions.test-d.ts

59 lines1.8 KB

1import { describe, expectTypeOf, test } from 'vitest'
2import { QueryClient } from '@tanstack/query-core'
3import { createInfiniteQuery, infiniteQueryOptions } from '../../src/index.js'
4import type { InfiniteData } from '@tanstack/query-core'
5​
6describe('queryOptions', () => {
7 test('Should not allow excess properties', () => {
8 infiniteQueryOptions({
9 queryKey: ['key'],
10 queryFn: () => Promise.resolve('data'),
11 getNextPageParam: () => 1,
12 initialPageParam: 1,
13 // @ts-expect-error this is a good error, because stallTime does not exist!
14 stallTime: 1000,
15 })
16 })
17​
18 test('Should infer types for callbacks', () => {
19 infiniteQueryOptions({
20 queryKey: ['key'],
21 queryFn: () => Promise.resolve('data'),
22 staleTime: 1000,
23 getNextPageParam: () => 1,
24 initialPageParam: 1,
25 select: (data) => {
26 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
27 },
28 })
29 })
30​
31 test('Should work when passed to createInfiniteQuery', () => {
32 const options = infiniteQueryOptions({
33 queryKey: ['key'],
34 queryFn: () => Promise.resolve('string'),
35 getNextPageParam: () => 1,
36 initialPageParam: 1,
37 })
38​
39 const query = createInfiniteQuery(() => options)
40​
41 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
42 expectTypeOf(query.data).toEqualTypeOf<
43 InfiniteData<string, unknown> | undefined
44 >()
45 })
46​
47 test('Should work when passed to fetchInfiniteQuery', async () => {
48 const options = infiniteQueryOptions({
49 queryKey: ['key'],
50 queryFn: () => Promise.resolve('string'),
51 getNextPageParam: () => 1,
52 initialPageParam: 1,
53 })
54​
55 const data = await new QueryClient().fetchInfiniteQuery(options)
56​
57 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
58 })
59})