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

OfflineExecutor.test.ts

76 lines1.9 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { LocalStorageAdapter, startOfflineExecutor } from '../src/index'
3import type { OfflineConfig } from '../src/types'
4​
5describe(`OfflineExecutor`, () => {
6 let mockCollection: any
7 let mockMutationFn: any
8 let config: OfflineConfig
9​
10 beforeEach(() => {
11 mockCollection = {
12 id: `test-collection`,
13 insert: vi.fn(),
14 update: vi.fn(),
15 delete: vi.fn(),
16 }
17​
18 mockMutationFn = vi.fn().mockResolvedValue(undefined)
19​
20 config = {
21 collections: {
22 'test-collection': mockCollection,
23 },
24 mutationFns: {
25 syncData: mockMutationFn,
26 },
27 storage: new LocalStorageAdapter(),
28 }
29 })
30​
31 it(`should create an offline executor`, () => {
32 const executor = startOfflineExecutor(config)
33​
34 expect(executor).toBeDefined()
35 expect(executor.isOfflineEnabled).toBeDefined()
36 })
37​
38 it(`should create offline transactions`, () => {
39 const executor = startOfflineExecutor(config)
40​
41 const transaction = executor.createOfflineTransaction({
42 mutationFnName: `syncData`,
43 })
44​
45 expect(transaction).toBeDefined()
46 expect(transaction.id).toBeDefined()
47 })
48​
49 it(`should create offline actions`, () => {
50 const executor = startOfflineExecutor(config)
51​
52 const action = executor.createOfflineAction({
53 mutationFnName: `syncData`,
54 onMutate: (data: any) => {
55 mockCollection.insert(data)
56 },
57 })
58​
59 expect(action).toBeDefined()
60 expect(typeof action).toBe(`function`)
61 })
62​
63 it(`should return empty outbox initially`, async () => {
64 const executor = startOfflineExecutor(config)
65​
66 const transactions = await executor.peekOutbox()
67​
68 expect(transactions).toEqual([])
69 })
70​
71 it(`should dispose cleanly`, () => {
72 const executor = startOfflineExecutor(config)
73​
74 expect(() => executor.dispose()).not.toThrow()
75 })
76})