(autoIndex: `off` | `eager`)
| 107 | } |
| 108 | |
| 109 | function createBasicTests(autoIndex: `off` | `eager`) { |
| 110 | describe(`with autoIndex ${autoIndex}`, () => { |
| 111 | let usersCollection: ReturnType<typeof createUsersCollection> |
| 112 | |
| 113 | beforeEach(() => { |
| 114 | usersCollection = createUsersCollection(autoIndex) |
| 115 | }) |
| 116 | |
| 117 | test(`should create, update and delete a live query collection with config`, () => { |
| 118 | const liveCollection = createLiveQueryCollection({ |
| 119 | startSync: true, |
| 120 | query: (q) => |
| 121 | q.from({ user: usersCollection }).select(({ user }) => ({ |
| 122 | id: user.id, |
| 123 | name: user.name, |
| 124 | age: user.age, |
| 125 | email: user.email, |
| 126 | active: user.active, |
| 127 | })), |
| 128 | }) |
| 129 | |
| 130 | const results = liveCollection.toArray |
| 131 | |
| 132 | expect(results).toHaveLength(4) |
| 133 | expect(results.map((u) => u.name)).toEqual( |
| 134 | expect.arrayContaining([`Alice`, `Bob`, `Charlie`, `Dave`]), |
| 135 | ) |
| 136 | |
| 137 | // Insert a new user |
| 138 | const newUser = { |
| 139 | id: 5, |
| 140 | name: `Eve`, |
| 141 | age: 28, |
| 142 | email: `eve@example.com`, |
| 143 | active: true, |
| 144 | } |
| 145 | usersCollection.utils.begin() |
| 146 | usersCollection.utils.write({ |
| 147 | type: `insert`, |
| 148 | value: newUser, |
| 149 | }) |
| 150 | usersCollection.utils.commit() |
| 151 | |
| 152 | expect(liveCollection.size).toBe(5) |
| 153 | expect(liveCollection.get(5)).toMatchObject(newUser) |
| 154 | |
| 155 | // Update the new user |
| 156 | const updatedUser = { ...newUser, name: `Eve Updated` } |
| 157 | usersCollection.utils.begin() |
| 158 | usersCollection.utils.write({ |
| 159 | type: `update`, |
| 160 | value: updatedUser, |
| 161 | }) |
| 162 | usersCollection.utils.commit() |
| 163 | |
| 164 | expect(liveCollection.size).toBe(5) |
| 165 | expect(liveCollection.get(5)).toMatchObject(updatedUser) |
| 166 |
no test coverage detected
searching dependent graphs…