add tests localStorage

This commit is contained in:
Matias Alejo Garcia 2014-12-03 09:40:54 -03:00
parent 5db7695d89
commit fccc970146
2 changed files with 23 additions and 2 deletions

View File

@ -18,11 +18,11 @@ LocalStorage.prototype.init = function() {
};
LocalStorage.prototype.setCredentials = function(email, password, opts) {
this.email = email;
this.password = password;
// NOP
};
LocalStorage.prototype.getItem = function(k,cb) {
preconditions.checkArgument(_.isFunction(cb));
return cb(null, this.ls.getItem(k));
};
@ -30,6 +30,7 @@ LocalStorage.prototype.getItem = function(k,cb) {
* Same as setItem, but fails if an item already exists
*/
LocalStorage.prototype.createItem = function(name, value, callback) {
preconditions.checkArgument(_.isFunction(callback));
if (this.ls.getItem(name)) {
return callback('EEXISTS');
}
@ -37,16 +38,19 @@ LocalStorage.prototype.createItem = function(name, value, callback) {
};
LocalStorage.prototype.setItem = function(k,v,cb) {
preconditions.checkArgument(_.isFunction(cb));
this.ls.setItem(k,v);
return cb();
};
LocalStorage.prototype.removeItem = function(k,cb) {
preconditions.checkArgument(_.isFunction(cb));
this.ls.removeItem(k);
return cb();
};
LocalStorage.prototype.clear = function(cb) {
preconditions.checkArgument(_.isFunction(cb));
this.ls.clear();
return cb();
};

View File

@ -8,6 +8,7 @@ describe('local storage plugin', function() {
beforeEach(function() {
storageMock = {};
storageMock.getItem = sinon.stub().returns(VALUE);
storageMock.createItem = sinon.stub().returns();
storageMock.setItem = sinon.stub().returns();
storageMock.removeItem = sinon.stub().returns();
storageMock.clear = sinon.stub().returns();
@ -25,6 +26,22 @@ describe('local storage plugin', function() {
});
});
it('#createItem', function(done) {
storageMock.getItem = sinon.stub().returns(null);
storage.createItem('hola', 'value', function(err) {
assert(!err);
return done();
});
});
it('#createItem (Exists)', function(done) {
storage.createItem('hola', 'value', function(err) {
err.should.contain('EEXISTS');
return done();
});
});
it('#removeItem', function(done) {
storage.removeItem('pepe', function(err) {