Skip to content

Commit 5485437

Browse files
committed
test: improve coverage
1 parent 33ed822 commit 5485437

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

__tests__/Collection.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { vi, beforeEach, describe, it, expect } from 'vitest'
2-
import { Collection } from '../src/index'
2+
import { Collection, createMemoryAdapter } from '../src/index'
33

44
describe('Collection', () => {
55
let collection: Collection<{ id: string, name: string }>
66

77
beforeEach(() => {
88
collection = new Collection<{ id: string, name: string }>({
9-
memory: [],
9+
memory: createMemoryAdapter([]),
1010
})
1111
})
1212

__tests__/Reactivity.spec.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ import S from 's-js'
3535
// onCleanup as solidCleanup,
3636
// createRoot as solidCreateRoot,
3737
// } from 'solid-js'
38-
import type { ReactivityAdapter } from '../src/index'
39-
import { Collection } from '../src/index'
38+
import { Collection, createReactivityAdapter } from '../src/index'
4039

4140
describe('Reactivity', () => {
4241
describe('Tracker', () => {
43-
const reactivity: ReactivityAdapter = {
42+
const reactivity = createReactivityAdapter({
4443
create: () => {
4544
const dep = new Tracker.Dependency()
4645
return {
@@ -56,7 +55,7 @@ describe('Reactivity', () => {
5655
if (!Tracker.active) return
5756
Tracker.onInvalidate(callback)
5857
}),
59-
}
58+
})
6059

6160
it('should be reactive with Tracker', () => {
6261
const collection = new Collection({ reactivity })
@@ -91,7 +90,7 @@ describe('Reactivity', () => {
9190
})
9291

9392
describe('@maverick-js/signals', () => {
94-
const reactivity: ReactivityAdapter = {
93+
const reactivity = createReactivityAdapter({
9594
create: () => {
9695
const dep = maverickSignal(0)
9796
return {
@@ -107,7 +106,7 @@ describe('Reactivity', () => {
107106
onDispose: vi.fn((callback) => {
108107
maverickOnDispose(callback)
109108
}),
110-
}
109+
})
111110

112111
it('should be reactive with @maverick-js/signals', () => {
113112
const collection = new Collection({ reactivity })
@@ -126,7 +125,7 @@ describe('Reactivity', () => {
126125
})
127126

128127
describe('oby', () => {
129-
const reactivity: ReactivityAdapter = {
128+
const reactivity = createReactivityAdapter({
130129
create: () => {
131130
const dep = $oby(0)
132131
return {
@@ -142,7 +141,7 @@ describe('Reactivity', () => {
142141
onDispose: vi.fn((callback) => {
143142
obyCleanup(callback)
144143
}),
145-
}
144+
})
146145

147146
it('should be reactive with oby', () => {
148147
const collection = new Collection({ reactivity })
@@ -161,7 +160,7 @@ describe('Reactivity', () => {
161160
})
162161

163162
describe('usignal', () => {
164-
const reactivity: ReactivityAdapter = {
163+
const reactivity = createReactivityAdapter({
165164
create: () => {
166165
const dep = uSignal(0)
167166
return {
@@ -174,7 +173,7 @@ describe('Reactivity', () => {
174173
},
175174
}
176175
},
177-
}
176+
})
178177

179178
it('should be reactive with usignal', () => {
180179
const collection = new Collection({ reactivity })
@@ -197,7 +196,7 @@ describe('Reactivity', () => {
197196
})
198197

199198
describe('sinuous', () => {
200-
const reactivity: ReactivityAdapter = {
199+
const reactivity = createReactivityAdapter({
201200
create: () => {
202201
const dep = sinuousObservable(0)
203202
return {
@@ -212,7 +211,7 @@ describe('Reactivity', () => {
212211
onDispose: vi.fn((callback) => {
213212
sinuousApi.cleanup(callback)
214213
}),
215-
}
214+
})
216215

217216
it('should be reactive with sinuous', () => {
218217
const collection = new Collection({ reactivity })
@@ -230,7 +229,7 @@ describe('Reactivity', () => {
230229
})
231230

232231
describe('preact', () => {
233-
const reactivity: ReactivityAdapter = {
232+
const reactivity = createReactivityAdapter({
234233
create: () => {
235234
const dep = preactSignal(0)
236235
return {
@@ -243,7 +242,7 @@ describe('Reactivity', () => {
243242
},
244243
}
245244
},
246-
}
245+
})
247246

248247
it('should be reactive with preact', () => {
249248
const collection = new Collection({ reactivity })
@@ -266,7 +265,7 @@ describe('Reactivity', () => {
266265
})
267266

268267
describe('@reactively/core', () => {
269-
const reactivity: ReactivityAdapter = {
268+
const reactivity = createReactivityAdapter({
270269
create: () => {
271270
const dep = reactively(0)
272271
return {
@@ -281,7 +280,7 @@ describe('Reactivity', () => {
281280
onDispose: vi.fn((callback) => {
282281
reactivelyOnCleanup(callback)
283282
}),
284-
}
283+
})
285284

286285
it('should be reactive with reactively', () => {
287286
const collection = new Collection({ reactivity })
@@ -302,7 +301,7 @@ describe('Reactivity', () => {
302301
})
303302

304303
describe('S.js', () => {
305-
const reactivity: ReactivityAdapter = {
304+
const reactivity = createReactivityAdapter({
306305
create: () => {
307306
const dep = S.data(true)
308307
return {
@@ -317,7 +316,7 @@ describe('Reactivity', () => {
317316
onDispose: vi.fn((callback) => {
318317
S.cleanup(callback)
319318
}),
320-
}
319+
})
321320

322321
it('should be reactive with S.js', () => {
323322
const collection = new Collection({ reactivity })
@@ -337,7 +336,7 @@ describe('Reactivity', () => {
337336
// solid doenst work in a node environment since createEffect won't run
338337
// eslint-disable-next-line vitest/no-commented-out-tests
339338
// describe('solid', () => {
340-
// const reactivity: ReactivityAdapter = {
339+
// const reactivity = createReactivityAdapter({
341340
// create: () => {
342341
// const [depend, rerun] = solidSignal(undefined, { equals: false })
343342
// return {
@@ -352,7 +351,7 @@ describe('Reactivity', () => {
352351
// onDispose: (callback) => {
353352
// solidCleanup(callback)
354353
// },
355-
// }
354+
// })
356355

357356
// eslint-disable-next-line vitest/no-commented-out-tests
358357
// it('should be reactive with solid', () => {

__tests__/persistence.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ describe('Persistence', () => {
179179
collection.on('persistence.error', fn)
180180
await waitForEvent(collection, 'persistence.init')
181181
collection.insert({ id: '1' })
182+
183+
collection.updateOne({ id: '1' }, { $set: { name: 'John' } })
184+
await waitForEvent(collection, 'persistence.error')
185+
186+
collection.removeOne({ id: '1' })
187+
await waitForEvent(collection, 'persistence.error')
188+
182189
await waitForEvent(collection, 'persistence.error')
183190
expect(fn).toHaveBeenCalledWith(new Error('test'))
184191
})
@@ -233,6 +240,13 @@ describe('Persistence', () => {
233240
await waitForEvent(collection, 'persistence.init')
234241
collection.insert({ id: '1' })
235242
await waitForEvent(collection, 'persistence.error')
243+
244+
collection.updateOne({ id: '1' }, { $set: { name: 'John' } })
245+
await waitForEvent(collection, 'persistence.error')
246+
247+
collection.removeOne({ id: '1' })
248+
await waitForEvent(collection, 'persistence.error')
249+
236250
expect(fn).toHaveBeenCalledWith(new Error('test'))
237251
})
238252
})

src/Collection/Observer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class Observer<T extends { id: any }> {
1818
constructor(
1919
callbacks: ObserveCallbacks<T>,
2020
bindEvents: () => () => void,
21-
skipInitial = false,
21+
skipInitial: boolean,
2222
) {
2323
this.callbacks = callbacks
2424
this.skipInitial = skipInitial

src/utils/sortItems.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ describe('sortItems', () => {
4545
{ id: 2, name: 'Charlie', age: 35 },
4646
])
4747
})
48+
49+
it('should sort items in descending order based on name', () => {
50+
const items: Item[] = [
51+
{ id: 3, name: 'Alice', age: 25 },
52+
{ id: 1, name: 'Bob', age: 30 },
53+
{ id: 2, name: 'Charlie', age: 35 },
54+
]
55+
const result = sortItems(items, { name: -1 })
56+
expect(result).toEqual([
57+
{ id: 2, name: 'Charlie', age: 35 },
58+
{ id: 1, name: 'Bob', age: 30 },
59+
{ id: 3, name: 'Alice', age: 25 },
60+
])
61+
})
4862
})

0 commit comments

Comments
 (0)