mirror of https://github.com/BTCPrivate/copay.git
add tests
This commit is contained in:
parent
91ccde9cfc
commit
bc3fa3258b
27
src/mocks.ts
27
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);
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
|
@ -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');
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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) => {
|
||||
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;
|
||||
service.getProfile((err, profile) => {
|
||||
expect(err).toBeNull;
|
||||
console.log(JSON.stringify(profile));
|
||||
expect(profile.name).toEqual('john doe');
|
||||
expect(typeof profile).toEqual('object');
|
||||
expect(profile.name).toEqual('My profile');
|
||||
});
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue