Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { useLiveInfiniteQuery } from '../src/useLiveInfiniteQuery'
3import type { Collection, Context, NonSingleResult } from '@tanstack/db'
4import type {
5 UseLiveInfiniteQueryConfig,
6 UseLiveInfiniteQueryReturn,
7} from '../src/useLiveInfiniteQuery'
8
9describe(`useLiveInfiniteQuery type assertions`, () => {
10 it(`does not advertise a server-page callback`, () => {
11 expectTypeOf<
12 Extract<keyof UseLiveInfiniteQueryConfig<Context>, `getNextPageParam`>
13 >().toEqualTypeOf<never>()
14 })
15
16 it(`keeps legacy generic wrappers source-compatible`, () => {
17 function acceptsContext<TContext extends Context>(
18 _config: UseLiveInfiniteQueryConfig<TContext>,
19 _result: UseLiveInfiniteQueryReturn<TContext>,
20 ): void {}
21
22 void acceptsContext
23 })
24
25 it(`exposes the controller fetch promise`, () => {
26 expectTypeOf<
27 UseLiveInfiniteQueryReturn<Context>[`fetchNextPage`]
28 >().toEqualTypeOf<() => Promise<void>>()
29 })
30
31 /**
32 * Law and source: the public pre-created live-query Collection overload must
33 * preserve its row, key, and utility types through `useLiveInfiniteQuery`.
34 * The compile-time observation cut is the returned public result. Missing-row
35 * and wrong-key accesses are hostile controls. Pagination timing and runtime
36 * page contents remain in the infinite-query conformance suite.
37 */
38 it(`preserves pre-created collection row, key, and utility types`, () => {
39 type Post = { id: `post-${number}`; title: string }
40 type PostKey = Post[`id`]
41 type PostUtils = { refreshPost: (key: PostKey) => Promise<void> }
42
43 const collection = null as unknown as Collection<Post, PostKey, PostUtils> &
44 NonSingleResult
45 const result = useLiveInfiniteQuery(collection, { pageSize: 5 })
46
47 expectTypeOf(result.data).toEqualTypeOf<Array<Post>>()
48 expectTypeOf(result.pages).toEqualTypeOf<Array<Array<Post>>>()
49 expectTypeOf(result.state).toEqualTypeOf<Map<PostKey, Post>>()
50 expectTypeOf(result.collection).toEqualTypeOf<typeof collection>()
51 expectTypeOf(result.collection.utils.refreshPost).toEqualTypeOf<
52 (key: PostKey) => Promise<void>
53 >()
54
55 // @ts-expect-error The collection overload must not erase row fields to any.
56 result.data[0]!.missing
57 // @ts-expect-error The collection overload must preserve the collection key.
58 result.state.get(`not-a-post-key`)
59 })
60})