import { appConfig } from '../../entities/AppConfig.fixture'; import { StorageService } from './StorageService'; import { CachedStorageService } from './CachedStorageService'; import { StringMarshaller } from './Marshallers'; import { Mock, IMock, It, Times } from 'typemoq'; import { expect } from 'chai'; describe('Cached storage service', () => { let localStorage: IMock; let cachedStorage: StorageService; context('in customer mode', () => { beforeEach(() => { localStorage = Mock.ofType(); cachedStorage = new CachedStorageService({ ...appConfig, csa: false }, localStorage.object); }); it('should return true if it contains a property in memory', () => { cachedStorage['_prop'] = 'value'; expect(cachedStorage.contains('prop')).to.be.true; }); it('should return true if it contains a property in local storage', () => { localStorage.setup(x => x.contains('prop')).returns(() => true); expect(cachedStorage.contains('prop')).to.be.true; }); it('should return false if it doesn\'t contain a property', () => { localStorage.setup(x => x.contains('prop')).returns(() => false); expect(cachedStorage.contains('prop')).to.be.false; }); it('should get a property from memory', () => { cachedStorage['_prop'] = 'fine'; localStorage.setup(x => x.get('prop')).returns(() => 'ok'); expect(cachedStorage.get('prop')).to.equal('fine'); }); it('should get a property from local storage', () => { localStorage.setup(x => x.get('prop', It.isAny())).returns(() => '123'); expect(cachedStorage.get('prop')).to.equal('123'); expect(cachedStorage['_prop']).to.equal('123'); }); it('should set a property to memory and local storage', () => { cachedStorage.set('prop', 50, StringMarshaller); expect(cachedStorage['_prop']).to.equal(50); localStorage.verify(x => x.set('prop', 50, StringMarshaller), Times.once()); }); }); context('in CSA mode', () => { it('should set a property to memory but not local storage', () => { let localStorage = Mock.ofType(); let cachedStorage = new CachedStorageService({ ...appConfig, csa: true }, localStorage.object); cachedStorage.set('prop', 'wow'); expect(cachedStorage['_prop']).to.equal('wow'); localStorage.verify(x => x.set(It.isAnyString(), It.isAny()), Times.never()); }); }); });