-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.spec.ts
33 lines (26 loc) · 907 Bytes
/
test.spec.ts
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
import 'fake-indexeddb/auto'
import { it, expect } from 'vitest'
import { LazyCache } from '../src'
// @ts-ignore
globalThis.fetch = async (url: string) => ({
ok: url.length > 0,
arrayBuffer: async () => new ArrayBuffer(url.length)
})
it('IndexedDB', async () => {
const lazyCache = new LazyCache('test')
// network error
expect(() => lazyCache.get('key', 'hash', '')).rejects.toThrow('Fail to download')
// first load
let buffer = await lazyCache.get('key', 'hash', '1')
expect(buffer.byteLength).toEqual(1)
// cache hit
buffer = await lazyCache.get('key', 'hash', '22')
expect(buffer.byteLength).toEqual(1)
// cache miss
buffer = await lazyCache.get('key', 'hsah', '333')
expect(buffer.byteLength).toEqual(3)
const db = await lazyCache.getDB()
expect(await db?.count('hash')).toEqual(1)
await lazyCache.invalidate()
expect(await db?.count('hash')).toEqual(0)
})