deno.land / x / replicache@v10.0.0-beta.0 / persist / idb-databases-store.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182import {expect} from '@esm-bundle/chai';import {TestMemStore} from '../kv/test-mem-store';import {IDBDatabasesStore, IndexedDBDatabase} from './idb-databases-store';import * as sinon from 'sinon';
teardown(() => { sinon.restore();});
test('getDatabases with no existing record in db', async () => { const store = new IDBDatabasesStore(_ => new TestMemStore()); expect(await store.getDatabases()).to.deep.equal({});});
test('putDatabase with no existing record in db', async () => { sinon.replace(Date, 'now', () => 1);
const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB = { name: 'testName', replicacheName: 'testReplicacheName', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion', }; expect(await store.putDatabase(testDB)).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 1), }); expect(await store.getDatabases()).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 1), });});
test('putDatabase updates lastOpenedTimestampMS', async () => { let now = 1; sinon.replace(Date, 'now', () => now);
const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB = { name: 'testName', replicacheName: 'testReplicacheName', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion', }; expect(await store.putDatabase(testDB)).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 1), }); expect(await store.getDatabases()).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 1), });
now = 2; expect(await store.putDatabase(testDB)).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 2), }); expect(await store.getDatabases()).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 2), });});
test('putDatabase ignores passed in lastOpenedTimestampMS', async () => { const now = 2; sinon.replace(Date, 'now', () => now);
const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB = { name: 'testName', replicacheName: 'testReplicacheName', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion', lastOpenedTimestampMS: 1, }; expect(await store.putDatabase(testDB)).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 2), }); expect(await store.getDatabases()).to.deep.equal({ testName: withLastOpenedTimestampMS(testDB, 2), });});
function withLastOpenedTimestampMS( db: IndexedDBDatabase, lastOpenedTimestampMS: number,): IndexedDBDatabase { return { ...db, lastOpenedTimestampMS, };}
test('putDatabase sequence', async () => { let now = 1; sinon.replace(Date, 'now', () => now); const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB1 = { name: 'testName1', replicacheName: 'testReplicacheName1', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion1', };
expect(await store.putDatabase(testDB1)).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, 1), }); expect(await store.getDatabases()).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, 1), });
const testDB2 = { name: 'testName2', replicacheName: 'testReplicacheName2', replicacheFormatVersion: 2, schemaVersion: 'testSchemaVersion2', };
now = 2;
expect(await store.putDatabase(testDB2)).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, 1), testName2: withLastOpenedTimestampMS(testDB2, 2), }); expect(await store.getDatabases()).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, 1), testName2: withLastOpenedTimestampMS(testDB2, 2), });});
test('close closes kv store', async () => { const memstore = new TestMemStore(); const store = new IDBDatabasesStore(_ => memstore); expect(memstore.closed).to.be.false; await store.close(); expect(memstore.closed).to.be.true;});
test('clear', async () => { let now = 1; sinon.replace(Date, 'now', () => now); const store = new IDBDatabasesStore(_ => new TestMemStore()); const testDB1 = { name: 'testName1', replicacheName: 'testReplicacheName1', replicacheFormatVersion: 1, schemaVersion: 'testSchemaVersion1', };
expect(await store.putDatabase(testDB1)).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, now), }); expect(await store.getDatabases()).to.deep.equal({ testName1: withLastOpenedTimestampMS(testDB1, now), });
await store.clearDatabases();
expect(await store.getDatabases()).to.deep.equal({});
const testDB2 = { name: 'testName2', replicacheName: 'testReplicacheName2', replicacheFormatVersion: 2, schemaVersion: 'testSchemaVersion2', };
now = 2;
expect(await store.putDatabase(testDB2)).to.deep.equal({ testName2: withLastOpenedTimestampMS(testDB2, now), }); expect(await store.getDatabases()).to.deep.equal({ testName2: withLastOpenedTimestampMS(testDB2, now), });});
test('getProfileID', async () => { const store = new IDBDatabasesStore(_ => new TestMemStore()); const profileID = await store.getProfileID(); expect(profileID).to.be.a('string'); expect(profileID).to.match(/^p[a-zA-Z0-9]+$/); const profileID2 = await store.getProfileID(); expect(profileID2).to.equal(profileID);});
Version Info