add storage service tests

This commit is contained in:
Joe Fleming 2014-07-23 13:05:16 -07:00
parent 3ddbabf6ba
commit c1bfdaa15b
3 changed files with 97 additions and 6 deletions

View file

@ -1,15 +1,15 @@
define(function (require) {
var modules = require('modules');
var store = localStorage;
modules.get('kibana/storage')
.service('storage', function () {
.service('storage', function ($window) {
function Storage() {
var storage = this;
storage.store = $window.localStorage;
storage.get = function (key) {
try {
return JSON.parse(store.getItem(key));
return JSON.parse(storage.store.getItem(key));
} catch (e) {
return null;
}
@ -17,18 +17,18 @@ define(function (require) {
storage.set = function (key, value) {
try {
return store.setItem(key, JSON.stringify(value));
return storage.store.setItem(key, JSON.stringify(value));
} catch (e) {
return false;
}
};
storage.remove = function (key) {
return store.removeItem(key);
return storage.store.removeItem(key);
};
storage.clear = function () {
return store.clear();
return storage.store.clear();
};
}

View file

@ -69,6 +69,7 @@
'specs/filters/uriescape',
'specs/filters/moment',
'specs/filters/start_from',
'specs/services/storage',
'specs/utils/datemath',
'specs/utils/interval',
'specs/utils/versionmath',

View file

@ -0,0 +1,90 @@
define(function (require) {
var mocks = require('angular-mocks');
var sinon = require('sinon/sinon');
var storage;
var $window;
var payload = { first: 'john', last: 'smith' };
require('components/storage/storage');
function init() {
module('kibana/storage', function ($provide) {
$provide.value('$window', {
localStorage: {
getItem: sinon.stub(),
setItem: sinon.spy(),
removeItem: sinon.spy(),
clear: sinon.spy()
}
});
});
inject(function ($injector) {
storage = $injector.get('storage');
$window = $injector.get('$window');
});
}
describe('StorageService', function () {
beforeEach(function () {
init();
});
describe('expected API', function () {
it('should have expected methods', function () {
expect(storage.get).to.be.a('function');
expect(storage.set).to.be.a('function');
expect(storage.remove).to.be.a('function');
expect(storage.clear).to.be.a('function');
});
});
describe('call behavior', function () {
it('should call getItem on the store', function () {
storage.get('name');
expect($window.localStorage.getItem.callCount).to.equal(1);
});
it('should parse JSON when reading from the store', function () {
var getItem = $window.localStorage.getItem;
getItem.returns(JSON.stringify(payload));
var data = storage.get('name');
expect(data).to.eql(payload);
});
it('should call setItem on the store', function () {
storage.set('name', 'john smith');
expect($window.localStorage.setItem.callCount).to.equal(1);
});
it('should write JSON string to the store', function () {
var setItem = $window.localStorage.setItem;
var key = 'name';
var value = payload;
storage.set(key, value);
var call = setItem.getCall(0);
expect(call.args[0]).to.equal(key);
expect(call.args[1]).to.equal(JSON.stringify(value));
});
it('should call removeItem on the store', function () {
storage.remove('name');
expect($window.localStorage.removeItem.callCount).to.equal(1);
});
it('should call clear on the store', function () {
storage.clear();
expect($window.localStorage.clear.callCount).to.equal(1);
});
});
});
});