add tests

This commit is contained in:
Ivan Socolsky 2017-08-07 16:30:44 -03:00
parent 91ccde9cfc
commit bc3fa3258b
No known key found for this signature in database
GPG Key ID: FAECE6A05FAA4F56
5 changed files with 62 additions and 48 deletions

View File

@ -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);
}
})
};
}

View File

@ -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<IStorage>('storage');

View File

@ -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 {

View File

@ -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');
});
});
});
}));
}));
});
});

View File

@ -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) {