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

TransactionStore.test.ts

84 lines2.4 KB

1import { beforeEach, describe, expect, it } from "vitest"
2import { TransactionStore } from "../src/TransactionStore"
3import type { Transaction, TransactionState } from "../src/types"
4import "fake-indexeddb/auto"
5​
6describe(`TransactionStore`, () => {
7 let store: TransactionStore
8​
9 beforeEach(async () => {
10 // Reset store for each test
11 store = new TransactionStore()
12 await store.clearAll()
13 })
14​
15 function createMockTransaction(id: string): Transaction {
16 return {
17 id,
18 state: `pending` as TransactionState,
19 createdAt: new Date(),
20 updatedAt: new Date(),
21 mutations: [],
22 strategy: { type: `ordered` },
23 metadata: {},
24 toObject: () => {
25 return {} as Transaction
26 },
27 }
28 }
29​
30 it(`should store and retrieve a transaction`, async () => {
31 const tx = createMockTransaction(`test-1`)
32 await store.putTransaction(tx)
33​
34 const transactions = await store.getTransactions()
35 expect(transactions).toHaveLength(1)
36 expect(transactions[0]?.id).toBe(`test-1`)
37 })
38​
39 it(`should update an existing transaction`, async () => {
40 const tx = createMockTransaction(`test-2`)
41 await store.putTransaction(tx)
42​
43 // Modify and update
44 const updatedTx = { ...tx, state: `completed` as const }
45 await store.putTransaction(updatedTx)
46​
47 const transactions = await store.getTransactions()
48 expect(transactions).toHaveLength(1)
49 expect(transactions[0]?.state).toBe(`completed`)
50 })
51​
52 it(`should delete a transaction`, async () => {
53 const tx = createMockTransaction(`test-3`)
54 await store.putTransaction(tx)
55​
56 await store.deleteTransaction(tx.id)
57 const transactions = await store.getTransactions()
58 expect(transactions).toHaveLength(0)
59 })
60​
61 it(`should handle multiple transactions`, async () => {
62 const tx1 = createMockTransaction(`test-4a`)
63 const tx2 = createMockTransaction(`test-4b`)
64 const tx3 = createMockTransaction(`test-4c`)
65​
66 await Promise.all([
67 store.putTransaction(tx1),
68 store.putTransaction(tx2),
69 store.putTransaction(tx3),
70 ])
71​
72 const transactions = await store.getTransactions()
73 expect(transactions).toHaveLength(3)
74 expect(transactions.map((t) => t.id).sort()).toEqual([
75 `test-4a`,
76 `test-4b`,
77 `test-4c`,
78 ])
79 })
80​
81 it(`should handle non-existent transaction deletion gracefully`, async () => {
82 await expect(store.deleteTransaction(`non-existent`)).resolves.not.toThrow()
83 })
84})