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

useLiveQuery.test-d.tsx

119 lines3.2 KB

1import { describe, expectTypeOf, it } from "vitest"
2import { renderHook } from "@testing-library/react"
3import { createCollection } from "../../db/src/collection/index"
4import { mockSyncCollectionOptions } from "../../db/tests/utils"
5import {
6 createLiveQueryCollection,
7 eq,
8 liveQueryCollectionOptions,
9} from "../../db/src/query/index"
10import { useLiveQuery } from "../src/useLiveQuery"
11import type { SingleResult } from "../../db/src/types"
12​
13type Person = {
14 id: string
15 name: string
16 age: number
17 email: string
18 isActive: boolean
19 team: string
21​
22describe(`useLiveQuery type assertions`, () => {
23 it(`should type findOne query builder to return a single row`, () => {
24 const collection = createCollection(
25 mockSyncCollectionOptions<Person>({
26 id: `test-persons-2`,
27 getKey: (person: Person) => person.id,
28 initialData: [],
29 })
30 )
31​
32 const { result } = renderHook(() => {
33 return useLiveQuery((q) =>
34 q
35 .from({ collection })
36 .where(({ collection: c }) => eq(c.id, `3`))
37 .findOne()
38 )
39 })
40​
41 expectTypeOf(result.current.data).toEqualTypeOf<Person | undefined>()
42 })
43​
44 it(`should type findOne config object to return a single row`, () => {
45 const collection = createCollection(
46 mockSyncCollectionOptions<Person>({
47 id: `test-persons-2`,
48 getKey: (person: Person) => person.id,
49 initialData: [],
50 })
51 )
52​
53 const { result } = renderHook(() => {
54 return useLiveQuery({
55 query: (q) =>
56 q
57 .from({ collection })
58 .where(({ collection: c }) => eq(c.id, `3`))
59 .findOne(),
60 })
61 })
62​
63 expectTypeOf(result.current.data).toEqualTypeOf<Person | undefined>()
64 })
65​
66 it(`should type findOne collection using liveQueryCollectionOptions to return a single row`, () => {
67 const collection = createCollection(
68 mockSyncCollectionOptions<Person>({
69 id: `test-persons-2`,
70 getKey: (person: Person) => person.id,
71 initialData: [],
72 })
73 )
74​
75 const options = liveQueryCollectionOptions({
76 query: (q) =>
77 q
78 .from({ collection })
79 .where(({ collection: c }) => eq(c.id, `3`))
80 .findOne(),
81 })
82​
83 const liveQueryCollection = createCollection(options)
84​
85 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
86​
87 const { result } = renderHook(() => {
88 return useLiveQuery(liveQueryCollection)
89 })
90​
91 expectTypeOf(result.current.data).toEqualTypeOf<Person | undefined>()
92 })
93​
94 it(`should type findOne collection using createLiveQueryCollection to return a single row`, () => {
95 const collection = createCollection(
96 mockSyncCollectionOptions<Person>({
97 id: `test-persons-2`,
98 getKey: (person: Person) => person.id,
99 initialData: [],
100 })
101 )
102​
103 const liveQueryCollection = createLiveQueryCollection({
104 query: (q) =>
105 q
106 .from({ collection })
107 .where(({ collection: c }) => eq(c.id, `3`))
108 .findOne(),
109 })
110​
111 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
112​
113 const { result } = renderHook(() => {
114 return useLiveQuery(liveQueryCollection)
115 })
116​
117 expectTypeOf(result.current.data).toEqualTypeOf<Person | undefined>()
118 })
119})