Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { createCollection } from '../../db/src/collection/index'
3import { mockSyncCollectionOptions } from '../../db/tests/utils'
4import { useLiveQuery } from '../src/useLiveQuery.svelte.js'
5import type { ConditionalUseLiveQueryReturn } from '../src/index.js'
6import type { Prettify } from '../../db/src/query/index'
7import type { Collection, CollectionStatus } from '@tanstack/db'
8import type { OutputWithVirtual } from '../../db/tests/utils'
9
10type Person = {
11 id: string
12 name: string
13}
14
15describe(`useLiveQuery type assertions`, () => {
16 it(`types disabled-capable callbacks from their empty reactive runtime`, () => {
17 const collection = createCollection(
18 mockSyncCollectionOptions<Person>({
19 id: `test-conditional-svelte`,
20 getKey: (person: Person) => person.id,
21 initialData: [],
22 }),
23 )
24 const enabled = null as unknown as boolean
25
26 // Compile-time observation cut: the public properties returned by the real
27 // `useLiveQuery` hook; the preload error proves the live-query Collection
28 // is absent while disabled.
29 const result = useLiveQuery((q) =>
30 enabled ? q.from({ collection }) : undefined,
31 )
32
33 expectTypeOf(result.data).toEqualTypeOf<
34 Array<Prettify<OutputWithVirtual<Person>>>
35 >()
36 expectTypeOf(result.collection).toEqualTypeOf<Collection<
37 Prettify<OutputWithVirtual<Person>>,
38 string | number,
39 {}
40 > | null>()
41 expectTypeOf(result.status).toEqualTypeOf<CollectionStatus | `disabled`>()
42
43 // @ts-expect-error Disabled callbacks expose a null collection until enabled.
44 result.collection.preload()
45 })
46
47 it(`preserves findOne cardinality`, () => {
48 const collection = createCollection(
49 mockSyncCollectionOptions<Person>({
50 id: `test-find-one-svelte`,
51 getKey: (person: Person) => person.id,
52 initialData: [],
53 }),
54 )
55
56 const result = useLiveQuery((q) => q.from({ collection }).findOne())
57
58 const data: OutputWithVirtual<Person> | undefined = result.data
59 expectTypeOf(data).toEqualTypeOf<OutputWithVirtual<Person> | undefined>()
60 })
61
62 it(`types conditional findOne data with its empty disabled representation`, () => {
63 const collection = createCollection(
64 mockSyncCollectionOptions<Person>({
65 id: `test-conditional-find-one-svelte`,
66 getKey: (person: Person) => person.id,
67 initialData: [],
68 }),
69 )
70 const enabled = null as unknown as boolean
71
72 // The exact public result combines enabled `findOne` cardinality with the
73 // empty-reactive disabled value. A paired framework test owns transitions.
74 const result = useLiveQuery((q) =>
75 enabled ? q.from({ collection }).findOne() : null,
76 )
77 const annotated: ConditionalUseLiveQueryReturn<
78 Prettify<OutputWithVirtual<Person>>,
79 Prettify<OutputWithVirtual<Person>> | undefined | []
80 > = result
81
82 expectTypeOf(annotated).toEqualTypeOf<typeof result>()
83 expectTypeOf(result.data).toEqualTypeOf<
84 Prettify<OutputWithVirtual<Person>> | undefined | []
85 >()
86 })
87})