Added database wrapper
All checks were successful
Build / Build NPM Project (push) Successful in 1m7s
Build / Tag Version (push) Successful in 14s
Build / Publish Documentation (push) Successful in 53s

This commit is contained in:
2025-05-25 23:02:56 -04:00
parent 97f2bcce2e
commit 6d706d4c15
7 changed files with 126 additions and 138 deletions

View File

@ -1,65 +0,0 @@
import {Collection} from '../src';
import 'fake-indexeddb/auto';
describe('IndexedDb and Collection', () => {
const data = {id: 1, name: 'Alice', age: 30, email: '<EMAIL>'};
const data2 = {id: 2, name: 'Bob', age: 39, email: '<EMAIL>'};
let collection = new Collection<number, typeof data>('database', 'collection');
afterEach(() => collection.clear());
test('can set and get an item', async () => {
await collection.put(data.id, data);
const result = await collection.get(data.id);
expect(result).toEqual(data);
});
test('can remove an item', async () => {
await collection.put(data.id, data);
await collection.put(data2.id, data2);
await collection.delete(data.id);
const result = await collection.get(data.id);
const result2 = await collection.get(data2.id);
expect(result).toBeUndefined();
expect(result2).toEqual(data2);
});
test('can clear all items', async () => {
await collection.put(data.id, data);
await collection.put(data2.id, data2);
await collection.clear();
const result = await collection.get(data.id);
const result2 = await collection.get(data2.id);
expect(result).toBeUndefined();
expect(result2).toBeUndefined();
});
test('getItem on missing key returns undefined', async () => {
const result = await collection.get(-1);
expect(result).toBeUndefined();
});
test('count returns number of items', async () => {
expect(await collection.count()).toBe(0);
await collection.put(data.id, data);
expect(await collection.count()).toBe(1);
await collection.delete(data.id);
expect(await collection.count()).toBe(0);
});
test('can get all items', async () => {
await collection.put(data.id, data);
await collection.put(data2.id, data2);
const allItems = await collection.getAll();
expect(allItems).toEqual(expect.arrayContaining([data, data2]));
expect(allItems.length).toBe(2);
});
test('can get all keys', async () => {
await collection.put(data.id, data);
await collection.put(data2.id, data2);
const keys = await collection.getAllKeys();
expect(keys).toEqual(expect.arrayContaining([data.id, data2.id]));
expect(keys.length).toBe(2);
});
});