forked from TanStack/db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction-types.test.ts
More file actions
103 lines (90 loc) · 3.15 KB
/
transaction-types.test.ts
File metadata and controls
103 lines (90 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { describe, expect, it } from "vitest"
import type { Collection } from "../src/collection"
import type {
MutationFn,
PendingMutation,
Transaction,
TransactionConfig,
} from "../src/types"
describe(`Transaction Types`, () => {
it(`should validate PendingMutation structure with collection`, () => {
// Create a mock collection
const mockCollection = {} as Collection<{ id: number; name: string }>
// Type assertion test - this will fail at compile time if the type is incorrect
const pendingMutation: PendingMutation<{ id: number; name: string }> = {
mutationId: `test-mutation-1`,
original: { id: 1, name: `Original` },
modified: { id: 1, name: `Modified` },
changes: { name: `Modified` },
key: `1`,
globalKey: `1`,
type: `update`,
metadata: null,
syncMetadata: {},
optimistic: true,
createdAt: new Date(),
updatedAt: new Date(),
collection: mockCollection,
}
expect(pendingMutation.collection).toBe(mockCollection)
expect(pendingMutation.type).toBe(`update`)
})
it(`should validate TransactionConfig structure`, () => {
// Minimal config with required mutationFn
const mockMutationFn: MutationFn = async () => Promise.resolve()
const minimalConfig: TransactionConfig = {
mutationFn: mockMutationFn,
}
expect(minimalConfig).toBeDefined()
// Full config
const fullConfig: TransactionConfig = {
id: `custom-transaction-id`,
mutationFn: mockMutationFn,
metadata: { source: `user-form` },
}
expect(fullConfig.id).toBe(`custom-transaction-id`)
expect(fullConfig.metadata).toEqual({ source: `user-form` })
})
it(`should validate Transaction structure`, () => {
// Create a mock Transaction object with all required properties
const mockMutationFn: MutationFn = async () => Promise.resolve()
// Create a complete mock Transaction object with all required methods
const transaction = {
id: `test-transaction`,
state: `pending`,
createdAt: new Date(),
mutations: [],
metadata: {},
mutationFn: mockMutationFn,
isPersisted: false,
autoCommit: false,
setState: () => {},
commit: async () => Promise.resolve(),
rollback: async () => Promise.resolve(),
reset: () => {},
addMutation: () => {},
// Add missing methods
mutate: async () => Promise.resolve(),
applyMutations: () => {},
touchCollection: () => {},
} as unknown as Transaction
expect(transaction.id).toBe(`test-transaction`)
})
it(`should validate TransactionConfig with metadata`, () => {
// Minimal config with just the required mutationFn
const minimalConfig: TransactionConfig = {
mutationFn: async () => {
return Promise.resolve({ success: true })
},
}
expect(typeof minimalConfig.mutationFn).toBe(`function`)
// Full config with metadata
const fullConfig: TransactionConfig = {
mutationFn: async () => {
return Promise.resolve({ success: true })
},
metadata: { source: `signup-form` },
}
expect(fullConfig.metadata).toEqual({ source: `signup-form` })
})
})