Open History to see the last commit
1import { beforeEach, describe, expect, it } from "vitest"
2import { z } from "zod"
3import { Collection } from "../src/collection"
4import "fake-indexeddb/auto"
5
6describe(`Object-Key Association`, () => {
7 let collection: Collection<{ name: string; age: number }>
8
9 beforeEach(() => {
10 const schema = z.object({
11 name: z.string(),
12 age: z.number(),
13 })
14
15 collection = new Collection<{ name: string; age: number }>({
16 id: `test-sync`,
17 schema,
18 sync: {
19 sync: async () => {},
20 },
21 mutationFn: {
22 persist: async () => {},
23 awaitSync: async () => {},
24 },
25 })
26 })
27
28 it(`should associate an object with its key after insert`, () => {
29 // Insert an object
30 const data = { name: `John`, age: 30 }
31 collection.insert(data, { key: `user1` })
32
33 const item = collection.state.get(`user1`)
34
35 // Make sure item exists before using it as a key
36 expect(item).toBeDefined()
37
38 // Update using the object reference
39 collection.update(item, (draft) => {
40 draft.age = 31
41 })
42
43 // Verify the update worked
44 const updated = collection.state.get(`user1`)
45 expect(updated).toEqual({ name: `John`, age: 31 })
46 })
47
48 it(`should work with multiple objects`, () => {
49 // Insert multiple objects
50 const johnData = { name: `John`, age: 30 }
51 const janeData = { name: `Jane`, age: 28 }
52
53 collection.insert([johnData, janeData], {
54 key: [`user1`, `user2`],
55 })
56
57 const john = collection.state.get(`user1`)
58 const jane = collection.state.get(`user2`)
59
60 // Update multiple objects using their references
61 collection.update([john!, jane!], (items) => {
62 if (items[0] && items[1]) {
63 items[0].age = 31
64 items[1].name = `Jane Doe`
65 }
66 })
67
68 // Verify updates
69 expect(collection.state.get(`user1`)).toEqual({ name: `John`, age: 31 })
70 expect(collection.state.get(`user2`)).toEqual({ name: `Jane Doe`, age: 28 })
71 })
72
73 it(`should handle delete with object reference`, () => {
74 // Insert an object
75 const data = { name: `John`, age: 30 }
76 collection.insert(data, { key: `user1` })
77
78 const john = collection.state.get(`user1`)
79
80 // Delete using the object reference
81 collection.delete(john)
82
83 // Verify deletion
84 expect(collection.state.get(`user1`)).toBeUndefined()
85 })
86
87 it(`should maintain object-key association after updates`, () => {
88 // Insert an object
89 const data = { name: `John`, age: 30 }
90 collection.insert(data, { key: `user1` })
91
92 const john = collection.state.get(`user1`)
93
94 // First update
95 collection.update(john, (item) => {
96 item.age = 31
97 })
98
99 // Second update using the same object reference
100 collection.update(john, (item) => {
101 item.name = `John Doe`
102 })
103
104 // Verify both updates worked
105 const updated = collection.state.get(`user1`)
106 expect(updated).toEqual({ name: `John Doe`, age: 31 })
107 })
108
109 it(`should throw error when object is not associated with any key`, () => {
110 const unknownObject = { name: `Unknown`, age: 25 }
111
112 // Try to update using an object that wasn't inserted
113 expect(() => {
114 collection.update(unknownObject, (item) => {
115 item.age = 26
116 })
117 }).toThrow()
118 })
119
120 it(`should support bulk insert with multiple keys`, () => {
121 // Insert multiple objects
122 const johnData = { name: `John`, age: 30 }
123 const janeData = { name: `Jane`, age: 28 }
124 const bobData = { name: `Bob`, age: 35 }
125
126 collection.insert([johnData, janeData, bobData], {
127 key: [`user1`, `user2`, `user3`],
128 })
129
130 const john = collection.state.get(`user1`)
131 const jane = collection.state.get(`user2`)
132 const bob = collection.state.get(`user3`)
133
134 // Verify inserts
135 expect(john).toEqual({ name: `John`, age: 30 })
136 expect(jane).toEqual({ name: `Jane`, age: 28 })
137 expect(bob).toEqual({ name: `Bob`, age: 35 })
138 })
139})