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

useLiveInfiniteQuery.test-d.ts

59 lines2.0 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { createCollection, createLiveQueryCollection } from '@tanstack/db'
3import { mockSyncCollectionOptions } from '../../db/tests/utils'
4import { useLiveInfiniteQuery } from '../src/useLiveInfiniteQuery'
5import type { OutputWithVirtual } from '../../db/tests/utils'
6​
7type Post = { id: string; title: string; rank: number }
8​
9const posts = createCollection(
10 mockSyncCollectionOptions<Post>({
11 id: `type-posts`,
12 getKey: (post) => post.id,
13 initialData: [],
14 }),
16​
17describe(`useLiveInfiniteQuery types`, () => {
18 it(`types pages of a query callback`, () => {
19 const result = useLiveInfiniteQuery(
20 (q) =>
21 q
22 .from({ post: posts })
23 .orderBy(({ post }) => post.rank, `desc`)
24 .select(({ post }) => ({ id: post.id, title: post.title })),
25 { pageSize: 10 },
26 )
27 expectTypeOf(result.data).toMatchTypeOf<
28 Array<OutputWithVirtual<{ id: string; title: string }>>
29 >()
30 expectTypeOf(result.pages).toMatchTypeOf<
31 Array<Array<OutputWithVirtual<{ id: string; title: string }>>>
32 >()
33 expectTypeOf(result.pageParams).toEqualTypeOf<Array<number>>()
34 expectTypeOf(result.fetchNextPage).toEqualTypeOf<() => Promise<void>>()
35 expectTypeOf(result.hasNextPage).toEqualTypeOf<boolean>()
36 expectTypeOf(result.isEnabled).toEqualTypeOf<true>()
37 })
38​
39 it(`types pages of a supplied live-query Collection`, () => {
40 const ranked = createLiveQueryCollection((q) =>
41 q.from({ post: posts }).orderBy(({ post }) => post.rank),
42 )
43 const result = useLiveInfiniteQuery(ranked, { pageSize: 5 })
44 expectTypeOf(result.pages).toMatchTypeOf<
45 Array<Array<OutputWithVirtual<Post>>>
46 >()
47 })
48​
49 it(`accepts queryKey for functional variants`, () => {
50 useLiveInfiniteQuery(
51 (q) =>
52 q
53 .from({ post: posts })
54 .fn.where(({ post }) => post.rank > 0)
55 .orderBy(({ post }) => post.rank),
56 { pageSize: 5, queryKey: [`ranked`] },
57 )
58 })
59})