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

infiniteQueryOptions.test-d.ts

60 lines1.8 KB

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