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

expo-sqlite-driver.test-d.ts

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