diff --git a/js/plugins/LocalStorage.js b/js/plugins/LocalStorage.js index 12533ef5a..f60235d10 100644 --- a/js/plugins/LocalStorage.js +++ b/js/plugins/LocalStorage.js @@ -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(); }; diff --git a/test/plugin.localstorage.js b/test/plugin.localstorage.js new file mode 100644 index 000000000..a49e317ce --- /dev/null +++ b/test/plugin.localstorage.js @@ -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(); + }); + }); + + + +});