mirror of https://github.com/BTCPrivate/copay.git
add tests to plugin.localstorage
This commit is contained in:
parent
bd698257f9
commit
5db7695d89
|
@ -2,11 +2,16 @@
|
|||
var _ = require('lodash');
|
||||
var preconditions = require('preconditions').singleton();
|
||||
|
||||
function LocalStorage() {
|
||||
function LocalStorage(opts) {
|
||||
this.type = 'DB';
|
||||
opts = opts || {};
|
||||
|
||||
|
||||
preconditions.checkState(typeof localStorage !== 'undefined',
|
||||
'localstorage not available, cannot run plugin');
|
||||
this.ls = opts.ls
|
||||
|| ( (typeof localStorage !== 'undefined') ? localStorage : null );
|
||||
|
||||
preconditions.checkState(this.ls,
|
||||
'localstorage not available, cannot run plugin');
|
||||
};
|
||||
|
||||
LocalStorage.prototype.init = function() {
|
||||
|
@ -18,31 +23,31 @@ LocalStorage.prototype.setCredentials = function(email, password, opts) {
|
|||
};
|
||||
|
||||
LocalStorage.prototype.getItem = function(k,cb) {
|
||||
return cb(null, localStorage.getItem(k));
|
||||
return cb(null, this.ls.getItem(k));
|
||||
};
|
||||
|
||||
/**
|
||||
* Same as setItem, but fails if an item already exists
|
||||
*/
|
||||
LocalStorage.prototype.createItem = function(name, value, callback) {
|
||||
if (localStorage.getItem(name)) {
|
||||
if (this.ls.getItem(name)) {
|
||||
return callback('EEXISTS');
|
||||
}
|
||||
return this.setItem(name, value, callback);
|
||||
};
|
||||
|
||||
LocalStorage.prototype.setItem = function(k,v,cb) {
|
||||
localStorage.setItem(k,v);
|
||||
this.ls.setItem(k,v);
|
||||
return cb();
|
||||
};
|
||||
|
||||
LocalStorage.prototype.removeItem = function(k,cb) {
|
||||
localStorage.removeItem(k);
|
||||
this.ls.removeItem(k);
|
||||
return cb();
|
||||
};
|
||||
|
||||
LocalStorage.prototype.clear = function(cb) {
|
||||
localStorage.clear();
|
||||
this.ls.clear();
|
||||
return cb();
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
var LocalStorage = require('../js/plugins/LocalStorage');
|
||||
var assert = require('assert');
|
||||
|
||||
describe('local storage plugin', function() {
|
||||
|
||||
var storage, storageMock, VALUE=123;
|
||||
|
||||
beforeEach(function() {
|
||||
storageMock = {};
|
||||
storageMock.getItem = sinon.stub().returns(VALUE);
|
||||
storageMock.setItem = sinon.stub().returns();
|
||||
storageMock.removeItem = sinon.stub().returns();
|
||||
storageMock.clear = sinon.stub().returns();
|
||||
storage = new LocalStorage({
|
||||
ls: storageMock
|
||||
});
|
||||
});
|
||||
|
||||
it('#getItem', function(done) {
|
||||
storage.getItem('hola', function(err, value) {
|
||||
assert(!err);
|
||||
storageMock.getItem.getCall(0).args[0].should.equal('hola');
|
||||
value.should.equal(VALUE);
|
||||
return done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('#removeItem', function(done) {
|
||||
storage.removeItem('pepe', function(err) {
|
||||
assert(!err);
|
||||
storageMock.removeItem.getCall(0).args[0].should.equal('pepe');
|
||||
return done();
|
||||
});
|
||||
});
|
||||
|
||||
it('#setItem', function(done) {
|
||||
storage.setItem('hola', 'chau', function(err) {
|
||||
assert(!err);
|
||||
storageMock.setItem.getCall(0).args[0].should.equal('hola');
|
||||
storageMock.setItem.getCall(0).args[1].should.equal('chau');
|
||||
return done();
|
||||
});
|
||||
});
|
||||
|
||||
it('#clear', function(done) {
|
||||
storage.clear(function(err) {
|
||||
assert(!err);
|
||||
storageMock.clear.calledOnce.should.equal(true);
|
||||
return done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
Loading…
Reference in New Issue