diff --git a/src/mocks.ts b/src/mocks.ts index e69de29bb..a7e78df40 100644 --- a/src/mocks.ts +++ b/src/mocks.ts @@ -0,0 +1,27 @@ +import { IStorage, KeyAlreadyExistsError } from './providers/storage/istorage'; + +export class StorageMock implements IStorage { + hash = {}; + + get(k: string, cb: (err: Error, v: string) => void) { + return cb(null, this.hash[k]); + }; + set(k: string, v: any, cb: (err: Error) => void) { + this.hash[k] = v; + return cb(null); + }; + remove(k: string, cb: (err: Error) => void) { + delete this.hash[k]; + return cb(null); + }; + create(k: string, v: any, cb: (err: Error) => void) { + this.get(k, + (err, data) => { + if (data) { + return cb(new KeyAlreadyExistsError()); + } else { + this.set(k, v, cb); + } + }) + }; +} diff --git a/src/providers/storage/istorage.ts b/src/providers/storage/istorage.ts index 794d57e27..829271eff 100644 --- a/src/providers/storage/istorage.ts +++ b/src/providers/storage/istorage.ts @@ -1,10 +1,16 @@ import { InjectionToken } from '@angular/core'; export interface IStorage { - get(k: string, cb: (err: Error, v: string) => void); + get(k: string, cb: (err: Error, v: any) => void); set(k: string, v: any, cb: (err: Error) => void); remove(k: string, cb: (err: Error) => void); create(k: string, v: any, cb: (err: Error) => void); } +export class KeyAlreadyExistsError extends Error { + constructor() { + super('Key already exists'); + } +} + export let ISTORAGE = new InjectionToken('storage'); diff --git a/src/providers/storage/local-storage.ts b/src/providers/storage/local-storage.ts index 741f2f52a..b0b995d71 100644 --- a/src/providers/storage/local-storage.ts +++ b/src/providers/storage/local-storage.ts @@ -3,13 +3,7 @@ import { PlatformProvider } from '../platform/platform'; import { Logger } from '@nsalaun/ng-logger'; import * as _ from 'lodash'; -import { IStorage } from './istorage'; - -export class KeyAlreadyExistsError extends Error { - constructor() { - super('Key already exists'); - } -} +import { IStorage, KeyAlreadyExistsError } from './istorage'; @Injectable() export class LocalStorage implements IStorage { @@ -19,8 +13,16 @@ export class LocalStorage implements IStorage { if (!this.ls) throw new Error('localstorage not available'); } - get(k: string, cb: (err: Error, v: string) => void) { - return cb(null, this.ls.getItem(k)); + get(k: string, cb: (err: Error, v: any) => void) { + let v = this.ls.getItem(k); + if (!v) return cb(null, null); + if (!_.isString(v)) return cb(null, v); + + try { + return cb(null, JSON.parse(v)); + } catch (e) { + return cb(null, v); + } } set(k: string, v: any, cb: (err: Error) => void) { @@ -42,7 +44,7 @@ export class LocalStorage implements IStorage { create(k: string, v: any, cb: (err: Error) => void) { this.get(k, - function (err, data) { + (err, data) => { if (data) { return cb(new KeyAlreadyExistsError()); } else { diff --git a/src/providers/storage/storage.spec.ts b/src/providers/storage/storage.spec.ts index 6bf673637..7db3d264e 100644 --- a/src/providers/storage/storage.spec.ts +++ b/src/providers/storage/storage.spec.ts @@ -1,36 +1,11 @@ import { TestBed, inject } from '@angular/core/testing'; import { StorageProvider } from './storage'; -import { LocalStorage, KeyAlreadyExistsError } from './local-storage'; +import { LocalStorage } from './local-storage'; import { IStorage, ISTORAGE } from './istorage'; - -class StorageMock implements IStorage { - hash = {}; - - get(k: string, cb: (err: Error, v: string) => void) { - return cb(null, this.hash[k]); - }; - set(k: string, v: any, cb: (err: Error) => void) { - this.hash[k] = v.toString(); - return cb(null); - }; - remove(k: string, cb: (err: Error) => void) { - delete this.hash[k]; - return cb(null); - }; - create(k: string, v: any, cb: (err: Error) => void) { - this.get(k, - function (err, data) { - if (data) { - return cb(new KeyAlreadyExistsError()); - } else { - this.set(k, v, cb); - } - }) - }; -} +import * as Mocks from '../../mocks'; describe('Storage Service', () => { - let storage: IStorage = new StorageMock(); + let storage: IStorage = new Mocks.StorageMock(); beforeEach(() => { TestBed.configureTestingModule({ providers: [ @@ -40,13 +15,17 @@ describe('Storage Service', () => { }); }); - it('should do nothing', inject([StorageProvider], (service: StorageProvider) => { - storage.set('profile', { name: 'john doe' }, (err) => { - service.getProfile((err, profile) => { + describe('#profile', () => { + it('should correctly perform a profile roundtrip', inject([StorageProvider], (service: StorageProvider) => { + var p = { name: 'My profile' }; + service.storeNewProfile(p, (err) => { expect(err).toBeNull; - console.log(JSON.stringify(profile)); - expect(profile.name).toEqual('john doe'); + service.getProfile((err, profile) => { + expect(err).toBeNull; + expect(typeof profile).toEqual('object'); + expect(profile.name).toEqual('My profile'); + }); }); - }); - })); + })); + }); }); \ No newline at end of file diff --git a/src/providers/storage/storage.ts b/src/providers/storage/storage.ts index 7e95f3eb5..351107d62 100644 --- a/src/providers/storage/storage.ts +++ b/src/providers/storage/storage.ts @@ -8,11 +8,11 @@ export class StorageProvider { } storeNewProfile(profile, cb) { - this.storage.create('profile', profile.toObj(), cb); + this.storage.create('profile', profile, cb); }; storeProfile(profile, cb) { - this.storage.set('profile', profile.toObj(), cb); + this.storage.set('profile', profile, cb); }; getProfile(cb) {