|
| 1 | +import { Store } from '../../../src/store'; |
| 2 | +import AppStorage from '../../../src/action/app-storage'; |
| 3 | +import * as logger from '../../../src/action/log'; |
| 4 | + |
| 5 | +describe('Action App Storage Unit Tests', () => { |
| 6 | + let sandbox; |
| 7 | + let store; |
| 8 | + let AsyncStorageStub; |
| 9 | + let db; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + sandbox = sinon.createSandbox({}); |
| 13 | + sandbox.stub(logger); |
| 14 | + store = new Store(); |
| 15 | + AsyncStorageStub = { |
| 16 | + getItem: sinon.stub(), |
| 17 | + setItem: sinon.stub(), |
| 18 | + clear: sinon.stub(), |
| 19 | + }; |
| 20 | + db = new AppStorage(store, AsyncStorageStub); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + sandbox.restore(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('restore()', () => { |
| 28 | + it('should use default if nothing is saved yet', async () => { |
| 29 | + AsyncStorageStub.getItem.resolves(undefined); |
| 30 | + await db.restore(AsyncStorageStub); |
| 31 | + expect(store.settings.unit, 'to equal', 'btc'); |
| 32 | + expect(logger.error, 'was not called'); |
| 33 | + expect(store.loaded, 'to be', true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should set supported setting', async () => { |
| 37 | + AsyncStorageStub.getItem |
| 38 | + .withArgs('settings') |
| 39 | + .resolves(JSON.stringify({ unit: 'sat' })); |
| 40 | + await db.restore(AsyncStorageStub); |
| 41 | + expect(store.settings.unit, 'to equal', 'sat'); |
| 42 | + expect(store.loaded, 'to be', true); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should set supported object', async () => { |
| 46 | + AsyncStorageStub.getItem.resolves( |
| 47 | + JSON.stringify({ exchangeRate: { usd: 0.1 } }) |
| 48 | + ); |
| 49 | + await db.restore(AsyncStorageStub); |
| 50 | + expect(store.settings.exchangeRate, 'to equal', { usd: 0.1 }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should not set supported setting', async () => { |
| 54 | + AsyncStorageStub.getItem.resolves(JSON.stringify({ invalid: 'bar' })); |
| 55 | + await db.restore(AsyncStorageStub); |
| 56 | + expect(store.settings.invalid, 'to be', undefined); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should log error', async () => { |
| 60 | + AsyncStorageStub.getItem.rejects(new Error('Boom!')); |
| 61 | + await db.restore(AsyncStorageStub); |
| 62 | + expect(logger.error, 'was called once'); |
| 63 | + expect(store.loaded, 'to be', true); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('save()', () => { |
| 68 | + it('should save all settings', async () => { |
| 69 | + store.settings = { foo: 'bar' }; |
| 70 | + await db.save(); |
| 71 | + const state = JSON.stringify(store.settings); |
| 72 | + expect(AsyncStorageStub.setItem, 'was called with', 'settings', state); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should log error', async () => { |
| 76 | + AsyncStorageStub.setItem.rejects(new Error('Boom!')); |
| 77 | + await db.save(); |
| 78 | + expect(logger.error, 'was called once'); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('clear()', () => { |
| 83 | + it('should clear all settings', async () => { |
| 84 | + await db.clear(); |
| 85 | + expect(AsyncStorageStub.clear, 'was called once'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should log error', async () => { |
| 89 | + AsyncStorageStub.clear.rejects(new Error('Boom!')); |
| 90 | + await db.clear(); |
| 91 | + expect(logger.error, 'was called once'); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments