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

expo-sqlite-driver.test-d.ts

57 lines2.0 KB

1import { describe, it } from 'vitest'
2import { createExpoSQLitePersistence } from '../src'
3import {
4 ExpoSQLiteDriver,
5 createExpoSQLiteDriver,
6} from '../src/expo-sqlite-driver'
7import type { SQLiteDatabase } from 'expo-sqlite'
8import type { ExpoSQLiteDatabaseLike } from '../src'
9​
10/**
11 * The public driver boundary accepts Expo's installed `SQLiteDatabase` while
12 * retaining its exclusive-transaction requirement. It also preserves the
13 * generic `SQLiteDriver.transaction<T>` result even though Expo's native
14 * `withExclusiveTransactionAsync` boundary returns `Promise<void>`.
15 *
16 * These compile-time checkpoints cover structural database compatibility and
17 * the returned `Promise<T>`. The paired runtime test observes the callback value
18 * only after the native exclusive transaction boundary settles.
19 */
20describe(`Expo SQLite driver types`, () => {
21 it(`accepts the vendor database returned by expo-sqlite`, () => {
22 const database = null as unknown as SQLiteDatabase
23​
24 createExpoSQLitePersistence({ database })
25 createExpoSQLiteDriver({ database })
26 new ExpoSQLiteDriver({ database })
27​
28 const compatible: ExpoSQLiteDatabaseLike = database
29 void compatible
30 })
31​
32 it(`preserves the transaction callback result type`, () => {
33 const database = null as unknown as SQLiteDatabase
34 const driver = new ExpoSQLiteDriver({ database })
35​
36 const result = driver.transaction(async (transactionDriver) => {
37 void transactionDriver
38 return { status: `committed` as const }
39 })
40 const expected: Promise<{ readonly status: `committed` }> = result
41 void expected
42 })
43​
44 it(`rejects databases without an exclusive transaction boundary`, () => {
45 const database = {
46 execAsync: (_sql: string) => Promise.resolve(),
47 getAllAsync: <T>(_sql: string) => Promise.resolve([] as Array<T>),
48 runAsync: (_sql: string) =>
49 Promise.resolve({ changes: 0, lastInsertRowId: 0 }),
50 }
51​
52 createExpoSQLitePersistence({
53 // @ts-expect-error persistence requires exclusive transactions
54 database,
55 })
56 })
57})