Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { shallowRef } from 'vue'
3import { createCollection, createLiveQueryCollection } from '@tanstack/db'
4import { useLiveInfiniteQuery } from '../src/useLiveInfiniteQuery'
5import { mockSyncCollectionOptions } from '../../db/tests/utils'
6import type { InitialQueryBuilder } from '@tanstack/db'
7import type { LiveInfiniteQueryConfig } from '../src/useLiveInfiniteQuery'
8
9type Post = {
10 id: string
11 createdAt: number
12}
13
14describe(`useLiveInfiniteQuery type assertions`, () => {
15 it(`does not advertise a server-page callback`, () => {
16 expectTypeOf<
17 Extract<keyof LiveInfiniteQueryConfig<Post>, `getNextPageParam`>
18 >().toEqualTypeOf<never>()
19 })
20
21 it(`preserves query and pre-created collection result types`, () => {
22 const posts = createCollection(
23 mockSyncCollectionOptions<Post>({
24 id: `vue-infinite-types`,
25 getKey: (post) => post.id,
26 initialData: [],
27 }),
28 )
29
30 const queryResult = useLiveInfiniteQuery(
31 (q: InitialQueryBuilder) =>
32 q.from({ posts }).orderBy(({ posts: post }) => post.createdAt, `desc`),
33 { pageSize: 5 },
34 )
35 expectTypeOf(queryResult.data.value[0]!.id).toEqualTypeOf<string>()
36 expectTypeOf(queryResult.data.value[0]!.createdAt).toEqualTypeOf<number>()
37 expectTypeOf(queryResult.fetchNextPage()).toEqualTypeOf<Promise<void>>()
38
39 const livePosts = createLiveQueryCollection((q: InitialQueryBuilder) =>
40 q.from({ posts }).orderBy(({ posts: post }) => post.createdAt, `desc`),
41 )
42 const collectionResult = useLiveInfiniteQuery(shallowRef(livePosts), {
43 pageSize: 5,
44 })
45
46 expectTypeOf(collectionResult.data.value[0]!.id).toEqualTypeOf<string>()
47 expectTypeOf(
48 collectionResult.data.value[0]!.createdAt,
49 ).toEqualTypeOf<number>()
50 expectTypeOf(collectionResult.state.value.get(`1`)?.id).toEqualTypeOf<
51 string | undefined
52 >()
53
54 useLiveInfiniteQuery(
55 // @ts-expect-error Infinite queries cannot use a single-result query.
56 (q: InitialQueryBuilder) =>
57 q
58 .from({ posts })
59 .orderBy(({ posts: post }) => post.createdAt, `desc`)
60 .findOne(),
61 { pageSize: 5 },
62 )
63
64 useLiveInfiniteQuery(
65 // @ts-expect-error Infinite queries do not support disabled null queries.
66 (_q: InitialQueryBuilder) => null,
67 { pageSize: 5 },
68 )
69 })
70})