pulsar/spec/state-store-spec.js

73 lines
2.0 KiB
JavaScript
Raw Normal View History

/** @babel */
2019-05-31 19:33:56 +03:00
const StateStore = require('../src/state-store.js');
2019-02-22 10:55:17 +03:00
describe('StateStore', () => {
2019-05-31 19:33:56 +03:00
let databaseName = `test-database-${Date.now()}`;
let version = 1;
2019-02-22 10:55:17 +03:00
it('can save, load, and delete states', () => {
2019-05-31 19:33:56 +03:00
const store = new StateStore(databaseName, version);
2019-02-22 10:55:17 +03:00
return store
.save('key', { foo: 'bar' })
.then(() => store.load('key'))
2019-02-22 10:55:17 +03:00
.then(state => {
2019-05-31 19:33:56 +03:00
expect(state).toEqual({ foo: 'bar' });
})
.then(() => store.delete('key'))
.then(() => store.load('key'))
2019-02-22 10:55:17 +03:00
.then(value => {
2019-05-31 19:33:56 +03:00
expect(value).toBeNull();
})
.then(() => store.count())
2019-02-22 10:55:17 +03:00
.then(count => {
2019-05-31 19:33:56 +03:00
expect(count).toBe(0);
});
});
2019-02-22 10:55:17 +03:00
it('resolves with null when a non-existent key is loaded', () => {
2019-05-31 19:33:56 +03:00
const store = new StateStore(databaseName, version);
2019-02-22 10:55:17 +03:00
return store.load('no-such-key').then(value => {
2019-05-31 19:33:56 +03:00
expect(value).toBeNull();
});
});
2019-02-22 10:55:17 +03:00
it('can clear the state object store', () => {
2019-05-31 19:33:56 +03:00
const store = new StateStore(databaseName, version);
2019-02-22 10:55:17 +03:00
return store
.save('key', { foo: 'bar' })
.then(() => store.count())
2019-02-22 10:55:17 +03:00
.then(count => expect(count).toBe(1))
.then(() => store.clear())
.then(() => store.count())
2019-02-22 10:55:17 +03:00
.then(count => {
2019-05-31 19:33:56 +03:00
expect(count).toBe(0);
});
});
2019-02-22 10:55:17 +03:00
describe('when there is an error reading from the database', () => {
it('rejects the promise returned by load', () => {
2019-05-31 19:33:56 +03:00
const store = new StateStore(databaseName, version);
2019-05-31 19:33:56 +03:00
const fakeErrorEvent = {
target: { errorCode: 'Something bad happened' }
};
2019-02-22 10:55:17 +03:00
spyOn(IDBObjectStore.prototype, 'get').andCallFake(key => {
2019-05-31 19:33:56 +03:00
let request = {};
process.nextTick(() => request.onerror(fakeErrorEvent));
return request;
});
2019-02-22 10:55:17 +03:00
return store
.load('nonexistentKey')
.then(() => {
2019-05-31 19:33:56 +03:00
throw new Error('Promise should have been rejected');
})
2019-02-22 10:55:17 +03:00
.catch(event => {
2019-05-31 19:33:56 +03:00
expect(event).toBe(fakeErrorEvent);
});
});
});
});