forked from TanStack/db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal-only.test.ts
More file actions
438 lines (365 loc) · 12.7 KB
/
local-only.test.ts
File metadata and controls
438 lines (365 loc) · 12.7 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import { beforeEach, describe, expect, it, vi } from "vitest"
import { createCollection } from "../src/index"
import { localOnlyCollectionOptions } from "../src/local-only"
import type { LocalOnlyCollectionUtils } from "../src/local-only"
import type { Collection } from "../src/index"
interface TestItem extends Record<string, unknown> {
id: number
name: string
completed?: boolean
}
describe(`LocalOnly Collection`, () => {
let collection: Collection<TestItem, number, LocalOnlyCollectionUtils>
beforeEach(() => {
// Create collection with LocalOnly configuration
collection = createCollection<TestItem, number>(
localOnlyCollectionOptions({
id: `test-local-only`,
getKey: (item: TestItem) => item.id,
})
)
})
it(`should create an empty collection initially`, () => {
expect(collection.state).toEqual(new Map())
expect(collection.size).toBe(0)
})
it(`should handle insert operations optimistically`, () => {
// Insert an item
collection.insert({ id: 1, name: `Test Item` })
// The item should be immediately available in the collection
expect(collection.has(1)).toBe(true)
expect(collection.get(1)).toEqual({ id: 1, name: `Test Item` })
expect(collection.size).toBe(1)
})
it(`should handle multiple inserts`, () => {
// Insert multiple items
collection.insert([
{ id: 1, name: `Item 1` },
{ id: 2, name: `Item 2` },
{ id: 3, name: `Item 3` },
])
// All items should be immediately available
expect(collection.size).toBe(3)
expect(collection.get(1)).toEqual({ id: 1, name: `Item 1` })
expect(collection.get(2)).toEqual({ id: 2, name: `Item 2` })
expect(collection.get(3)).toEqual({ id: 3, name: `Item 3` })
})
it(`should handle update operations optimistically`, () => {
// Insert an item first
collection.insert({ id: 1, name: `Original Item` })
// Update the item
collection.update(1, (draft) => {
draft.name = `Updated Item`
draft.completed = true
})
// The update should be immediately reflected
expect(collection.get(1)).toEqual({
id: 1,
name: `Updated Item`,
completed: true,
})
})
it(`should handle delete operations optimistically`, () => {
// Insert items first
collection.insert([
{ id: 1, name: `Item 1` },
{ id: 2, name: `Item 2` },
])
expect(collection.size).toBe(2)
// Delete one item
collection.delete(1)
// The item should be immediately removed
expect(collection.has(1)).toBe(false)
expect(collection.has(2)).toBe(true)
expect(collection.size).toBe(1)
})
it(`should handle mixed operations`, () => {
// Perform a series of mixed operations
collection.insert({ id: 1, name: `Item 1` })
collection.insert({ id: 2, name: `Item 2` })
expect(collection.size).toBe(2)
// Update item 1
collection.update(1, (draft) => {
draft.completed = true
})
// Delete item 2
collection.delete(2)
// Insert item 3
collection.insert({ id: 3, name: `Item 3` })
// Check final state
expect(collection.size).toBe(2)
expect(collection.get(1)).toEqual({
id: 1,
name: `Item 1`,
completed: true,
})
expect(collection.has(2)).toBe(false)
expect(collection.get(3)).toEqual({ id: 3, name: `Item 3` })
})
it(`should support change subscriptions`, () => {
const changeHandler = vi.fn()
// Subscribe to changes
const unsubscribe = collection.subscribeChanges(changeHandler)
// Insert an item
collection.insert({ id: 1, name: `Test Item` })
// The change handler should have been called
expect(changeHandler).toHaveBeenCalledTimes(1)
expect(changeHandler).toHaveBeenCalledWith([
{
type: `insert`,
key: 1,
value: { id: 1, name: `Test Item` },
},
])
// Clean up
unsubscribe()
})
it(`should support toArray method`, () => {
// Insert some items
collection.insert([
{ id: 3, name: `Item 3` },
{ id: 1, name: `Item 1` },
{ id: 2, name: `Item 2` },
])
const array = collection.toArray
// Should contain all items
expect(array).toHaveLength(3)
expect(array).toEqual(
expect.arrayContaining([
{ id: 1, name: `Item 1` },
{ id: 2, name: `Item 2` },
{ id: 3, name: `Item 3` },
])
)
})
it(`should support entries and iteration`, () => {
// Insert some items
collection.insert([
{ id: 1, name: `Item 1` },
{ id: 2, name: `Item 2` },
])
// Test entries
const entries = Array.from(collection.entries())
expect(entries).toHaveLength(2)
expect(entries).toEqual(
expect.arrayContaining([
[1, { id: 1, name: `Item 1` }],
[2, { id: 2, name: `Item 2` }],
])
)
// Test values iteration
const items = []
for (const item of collection.values()) {
items.push(item)
}
expect(items).toHaveLength(2)
})
describe(`Pure optimistic behavior`, () => {
it(`should handle insert operations optimistically`, () => {
// Insert an item - this works purely optimistically
collection.insert({ id: 1, name: `Test Item` })
// The item should be available in the collection
expect(collection.has(1)).toBe(true)
expect(collection.get(1)).toEqual({ id: 1, name: `Test Item` })
})
it(`should handle update operations optimistically`, () => {
// Insert an item first
collection.insert({ id: 1, name: `Test Item` })
// Update the item - this works purely optimistically
collection.update(1, (draft) => {
draft.name = `Updated Item`
})
// The update should be reflected in the collection
expect(collection.get(1)).toEqual({ id: 1, name: `Updated Item` })
})
it(`should handle delete operations optimistically`, () => {
// Insert an item first
collection.insert({ id: 1, name: `Test Item` })
expect(collection.has(1)).toBe(true)
// Delete the item - this works purely optimistically
collection.delete(1)
// The item should be removed from the collection
expect(collection.has(1)).toBe(false)
})
})
describe(`Schema support`, () => {
it(`should work with schema configuration`, () => {
// This tests that the type system works correctly with schemas
// In a real implementation, you would provide a schema for validation
const testCollection = createCollection(
localOnlyCollectionOptions({
id: `test-schema`,
getKey: (item: TestItem) => item.id,
})
)
// Basic operations should still work
testCollection.insert({ id: 1, name: `Test with Schema` })
expect(testCollection.get(1)).toEqual({ id: 1, name: `Test with Schema` })
})
})
describe(`Utils object`, () => {
it(`should expose utils object (even if empty)`, () => {
expect(collection.utils).toBeDefined()
expect(typeof collection.utils).toBe(`object`)
})
})
describe(`Custom callbacks`, () => {
it(`should call custom onInsert callback when provided`, () => {
const onInsertSpy = vi.fn()
const testCollection = createCollection(
localOnlyCollectionOptions({
id: `test-custom-callbacks`,
getKey: (item: TestItem) => item.id,
onInsert: onInsertSpy,
})
)
testCollection.insert({ id: 1, name: `Test Item` })
expect(onInsertSpy).toHaveBeenCalledTimes(1)
expect(onInsertSpy).toHaveBeenCalledWith(
expect.objectContaining({
transaction: expect.objectContaining({
mutations: expect.arrayContaining([
expect.objectContaining({
type: `insert`,
modified: { id: 1, name: `Test Item` },
}),
]),
}),
})
)
// Collection should still work normally
expect(testCollection.get(1)).toEqual({ id: 1, name: `Test Item` })
})
it(`should call custom onUpdate callback when provided`, () => {
const onUpdateSpy = vi.fn()
const testCollection = createCollection(
localOnlyCollectionOptions({
id: `test-custom-update`,
getKey: (item: TestItem) => item.id,
onUpdate: onUpdateSpy,
})
)
testCollection.insert({ id: 1, name: `Test Item` })
testCollection.update(1, (draft) => {
draft.name = `Updated Item`
})
expect(onUpdateSpy).toHaveBeenCalledTimes(1)
expect(onUpdateSpy).toHaveBeenCalledWith(
expect.objectContaining({
transaction: expect.objectContaining({
mutations: expect.arrayContaining([
expect.objectContaining({
type: `update`,
modified: { id: 1, name: `Updated Item` },
}),
]),
}),
})
)
// Collection should still work normally
expect(testCollection.get(1)).toEqual({ id: 1, name: `Updated Item` })
})
it(`should call custom onDelete callback when provided`, () => {
const onDeleteSpy = vi.fn()
const testCollection = createCollection(
localOnlyCollectionOptions({
id: `test-custom-delete`,
getKey: (item: TestItem) => item.id,
onDelete: onDeleteSpy,
})
)
testCollection.insert({ id: 1, name: `Test Item` })
testCollection.delete(1)
expect(onDeleteSpy).toHaveBeenCalledTimes(1)
expect(onDeleteSpy).toHaveBeenCalledWith(
expect.objectContaining({
transaction: expect.objectContaining({
mutations: expect.arrayContaining([
expect.objectContaining({
type: `delete`,
original: { id: 1, name: `Test Item` },
}),
]),
}),
})
)
// Collection should still work normally
expect(testCollection.has(1)).toBe(false)
})
it(`should work without custom callbacks`, () => {
const testCollection = createCollection(
localOnlyCollectionOptions({
id: `test-no-callbacks`,
getKey: (item: TestItem) => item.id,
})
)
// Should work normally without callbacks
testCollection.insert({ id: 1, name: `Test Item` })
testCollection.update(1, (draft) => {
draft.name = `Updated Item`
})
testCollection.delete(1)
expect(testCollection.has(1)).toBe(false)
})
})
describe(`Initial data`, () => {
it(`should populate collection with initial data on creation`, () => {
const initialItems: Array<TestItem> = [
{ id: 10, name: `Initial Item 1` },
{ id: 20, name: `Initial Item 2` },
{ id: 30, name: `Initial Item 3` },
]
const testCollection = createCollection(
localOnlyCollectionOptions({
id: `test-initial-data`,
getKey: (item: TestItem) => item.id,
initialData: initialItems,
})
)
// Collection should be populated with initial data
expect(testCollection.size).toBe(3)
expect(testCollection.get(10)).toEqual({ id: 10, name: `Initial Item 1` })
expect(testCollection.get(20)).toEqual({ id: 20, name: `Initial Item 2` })
expect(testCollection.get(30)).toEqual({ id: 30, name: `Initial Item 3` })
})
it(`should work with empty initial data array`, () => {
const testCollection = createCollection(
localOnlyCollectionOptions({
id: `test-empty-initial-data`,
getKey: (item: TestItem) => item.id,
initialData: [],
})
)
// Collection should be empty
expect(testCollection.size).toBe(0)
})
it(`should work without initial data property`, () => {
const testCollection = createCollection(
localOnlyCollectionOptions({
id: `test-no-initial-data`,
getKey: (item: TestItem) => item.id,
})
)
// Collection should be empty
expect(testCollection.size).toBe(0)
})
it(`should allow adding more items after initial data`, () => {
const initialItems: Array<TestItem> = [{ id: 100, name: `Initial Item` }]
const testCollection = createCollection<TestItem, number>(
localOnlyCollectionOptions({
id: `test-initial-plus-more`,
getKey: (item: TestItem) => item.id,
initialData: initialItems,
})
)
// Should start with initial data
expect(testCollection.size).toBe(1)
expect(testCollection.get(100)).toEqual({ id: 100, name: `Initial Item` })
// Should be able to add more items
testCollection.insert({ id: 200, name: `Added Item` })
expect(testCollection.size).toBe(2)
expect(testCollection.get(100)).toEqual({ id: 100, name: `Initial Item` })
expect(testCollection.get(200)).toEqual({ id: 200, name: `Added Item` })
})
})
})