import { LocalStorageService, Prefix } from './LocalStorageService'; import { LocalDateParser, BooleanParser, IntParser, JSONParser } from './Parsers'; import { StringMarshaller, JSONMarshaller, LocalDateMarshaller } from './Marshallers'; import { Mock, IMock, It, Times } from 'typemoq'; import { LocalDate } from 'js-joda'; import { expect } from 'chai'; describe('Local storage service', () => { let localStorage: IMock; let localStorageService: LocalStorageService; beforeEach(() => { localStorage = Mock.ofType(); const $window = Mock.ofType(); $window.setup(x => x.localStorage).returns(() => localStorage.object); localStorageService = new LocalStorageService($window.object); }); it('should return true if it contains a property', () => { localStorage.setup(x => x[`ovo.prop`]).returns(() => 'value'); expect(localStorageService.contains('prop')).to.be.true; }); it('should return false if it doesn\'t contain a property', () => { expect(localStorageService.contains('prop')).to.be.false; }); it('should get a string property', () => { localStorage.setup(x => x.getItem('ovo.prop')).returns(() => 'Roger'); expect(localStorageService.get('prop')).to.equal('Roger'); }); it('should get a boolean property', () => { localStorage.setup(x => x.getItem('ovo.prop')).returns(() => 'true'); expect(localStorageService.get('prop', BooleanParser)).to.be.true; }) it('should get an integer property', () => { localStorage.setup(x => x.getItem('ovo.prop')).returns(() => '987'); expect(localStorageService.get('prop', IntParser)).to.equal(987); }) it('should get a date property', () => { localStorage.setup(x => x.getItem('ovo.prop')).returns(() => '2012-01-11'); expect(localStorageService.get('prop', LocalDateParser)).to.deep.equal(LocalDate.of(2012, 1, 11)); }) it('should get an object property', () => { localStorage.setup(x => x.getItem('ovo.prop')).returns(() => '{"name":"Tom"}'); expect(localStorageService.get<{ name: string }>('prop', JSONParser)).to.deep.equal({ name: 'Tom' }); }) it('should return undefined for a missing property', () => { expect(localStorageService.get('prop')).to.be.undefined; }) it('should handle a parsing error', () => { localStorage.setup(x => x.getItem('ovo.prop')).returns(() => 'notadate'); expect(localStorageService.get('prop', LocalDateParser)).to.be.undefined; }) it('should set a string property', () => { localStorageService.set('prop', 'Fred'); localStorage.verify(x => x.setItem('ovo.prop', 'Fred'), Times.once()); }) it('should set a boolean property', () => { localStorageService.set('prop', false, StringMarshaller); localStorage.verify(x => x.setItem('ovo.prop', 'false'), Times.once()); }) it('should set an integer property', () => { localStorageService.set('prop', 123, StringMarshaller); localStorage.verify(x => x.setItem('ovo.prop', '123'), Times.once()); }) it('should set a date property', () => { localStorageService.set('prop', LocalDate.of(2012, 1, 11), LocalDateMarshaller); localStorage.verify(x => x.setItem('ovo.prop', '2012-01-11'), Times.once()); }) it('should set an object property', () => { localStorageService.set('prop', { name: 'Paul' }, JSONMarshaller); localStorage.verify(x => x.setItem('ovo.prop', '{"name":"Paul"}'), Times.once()); }) it('should remove a property', () => { localStorageService.set('prop', null); localStorage.verify(x => x.removeItem('ovo.prop'), Times.once()); }) });