state-store-spec.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /** @babel */
  2. const StateStore = require('../src/state-store.js');
  3. describe('StateStore', () => {
  4. let databaseName = `test-database-${Date.now()}`;
  5. let version = 1;
  6. it('can save, load, and delete states', () => {
  7. const store = new StateStore(databaseName, version);
  8. return store
  9. .save('key', { foo: 'bar' })
  10. .then(() => store.load('key'))
  11. .then(state => {
  12. expect(state).toEqual({ foo: 'bar' });
  13. })
  14. .then(() => store.delete('key'))
  15. .then(() => store.load('key'))
  16. .then(value => {
  17. expect(value).toBeNull();
  18. })
  19. .then(() => store.count())
  20. .then(count => {
  21. expect(count).toBe(0);
  22. });
  23. });
  24. it('resolves with null when a non-existent key is loaded', () => {
  25. const store = new StateStore(databaseName, version);
  26. return store.load('no-such-key').then(value => {
  27. expect(value).toBeNull();
  28. });
  29. });
  30. it('can clear the state object store', () => {
  31. const store = new StateStore(databaseName, version);
  32. return store
  33. .save('key', { foo: 'bar' })
  34. .then(() => store.count())
  35. .then(count => expect(count).toBe(1))
  36. .then(() => store.clear())
  37. .then(() => store.count())
  38. .then(count => {
  39. expect(count).toBe(0);
  40. });
  41. });
  42. describe('when there is an error reading from the database', () => {
  43. it('rejects the promise returned by load', () => {
  44. const store = new StateStore(databaseName, version);
  45. const fakeErrorEvent = {
  46. target: { errorCode: 'Something bad happened' }
  47. };
  48. spyOn(IDBObjectStore.prototype, 'get').andCallFake(key => {
  49. let request = {};
  50. process.nextTick(() => request.onerror(fakeErrorEvent));
  51. return request;
  52. });
  53. return store
  54. .load('nonexistentKey')
  55. .then(() => {
  56. throw new Error('Promise should have been rejected');
  57. })
  58. .catch(event => {
  59. expect(event).toBe(fakeErrorEvent);
  60. });
  61. });
  62. });
  63. });