Merge pull request #50 from mwq27/master

Added check if stored value is a number to spyOn, since localStorage wil...
dev
Gregory Pike 11 years ago
commit a973003559

@ -10,13 +10,13 @@ module.exports = function(grunt) {
grunt.initConfig({
karma: {
options: {
autowatch: false,
autowatch: true,
browsers: [
'PhantomJS'
],
configFile: 'test/karma.conf.js',
reporters: [ 'dots' ],
singleRun: true
singleRun: false
},
unit: {}
},

@ -1,24 +1,41 @@
(function() {
/* Start angularLocalStorage */
var angularLocalStorage = angular.module('LocalStorageModule', []);
/* Start angularLocalStorage */
'use strict';
var angularLocalStorage = angular.module('LocalStorageModule', []);
angularLocalStorage.provider('localStorageService', function(){
// You should set a prefix to avoid overwriting any local storage variables from the rest of your app
// e.g. angularLocalStorage.constant('prefix', 'youAppName');
angularLocalStorage.value('prefix', 'ls');
this.prefix = 'ls';
// Cookie options (usually in case of fallback)
// expiry = Number of days before cookies expire // 0 = Does not expire
// path = The web path the cookie represents
angularLocalStorage.constant('cookie', { expiry:30, path: '/'});
angularLocalStorage.constant('notify', { setItem: true, removeItem: false} );
angularLocalStorage.service('localStorageService', [
'$rootScope',
'prefix',
'cookie',
'notify',
function($rootScope, prefix, cookie, notify) {
this.cookie = {
expiry: 30,
path: '/'
};
this.notify = {
setItem: true,
removeItem: false
};
this.setPrefix = function(prefix){
this.prefix = prefix;
};
this.setStorageCookie = function(exp, path){
this.cookie = {
expiry: exp,
path: path
};
};
this.setNotify = function(itemSet, itemRemove){
this.notify = {
setItem: itemSet,
removeItem: itemRemove
};
};
this.$get = ['$rootScope',function($rootScope){
var prefix = this.prefix;
// If there is a prefix set in the config lets use that with an appended period for readability
//var prefix = angularLocalStorage.constant;
if (prefix.substr(-1)!=='.') {
@ -28,7 +45,7 @@ angularLocalStorage.service('localStorageService', [
// Checks the browser to see if local storage is supported
var browserSupportsLocalStorage = function () {
try {
return ('localStorage' in window && window['localStorage'] !== null);
return ('localStorage' in window && window.localStorage !== null);
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
return false;
@ -50,7 +67,7 @@ angularLocalStorage.service('localStorageService', [
}
// Let's convert undefined values to null to get the value consistent
if (typeof value == "undefined") {
if (typeof value === "undefined") {
value = null;
}
@ -80,7 +97,7 @@ angularLocalStorage.service('localStorageService', [
var item = localStorage.getItem(prefix+key);
// angular.toJson will convert null to 'null', so a proper conversion is needed
// FIXME not a perfect solution, since a valid 'null' string can't be stored
if (!item || item === 'null') return null;
if (!item || item === 'null') {return null;}
if (item.charAt(0) === "{" || item.charAt(0) === "[") {
return angular.fromJson(item);
@ -179,7 +196,7 @@ angularLocalStorage.service('localStorageService', [
// Example use: localStorageService.cookie.add('library','angular');
var addToCookies = function (key, value) {
if (typeof value == "undefined") {
if (typeof value === "undefined") {
return false;
}
@ -221,7 +238,7 @@ angularLocalStorage.service('localStorageService', [
var cookies = document.cookie.split(';');
for(var i=0;i < cookies.length;i++) {
var thisCookie = cookies[i];
while (thisCookie.charAt(0)==' ') {
while (thisCookie.charAt(0)===' ') {
thisCookie = thisCookie.substring(1,thisCookie.length);
}
if (thisCookie.indexOf(prefix+key+'=') === 0) {
@ -241,7 +258,7 @@ angularLocalStorage.service('localStorageService', [
var cookies = document.cookie.split(';');
for(var i=0;i < cookies.length;i++) {
thisCookie = cookies[i];
while (thisCookie.charAt(0)==' ') {
while (thisCookie.charAt(0)===' ') {
thisCookie = thisCookie.substring(1,thisCookie.length);
}
key = thisCookie.substring(prefixLength,thisCookie.indexOf('='));
@ -265,6 +282,6 @@ angularLocalStorage.service('localStorageService', [
clearAll: clearAllFromCookies
}
};
}]);
}]
});
}).call(this);

@ -1,11 +1,12 @@
describe('Tests functionality of the localStorage module', function(){
beforeEach(module('LocalStorageModule'));
var ls, store = {};
beforeEach(module('LocalStorageModule', function(localStorageServiceProvider){
p = localStorageServiceProvider;
}));
var ls, store = [];
beforeEach(inject(function(_localStorageService_){
ls = _localStorageService_;
spyOn(ls, 'get').andCallFake(function(key){
if(store[key].charAt(0) ==="{" || store[key].charAt(0) === "["){
if(store[key].charAt(0) === "{" || store[key].charAt(0) === "["){
return angular.fromJson(store[key]);
}else{
return store[key];
@ -13,9 +14,12 @@ describe('Tests functionality of the localStorage module', function(){
});
spyOn(ls, 'set').andCallFake(function(key, val){
if(typeof val === "object"){
if(angular.isObject(val) || angular.isArray(val)){
val = angular.toJson(val);
}
if(angular.isNumber(val)){
val = val.toString();
}
return store[key] = val;
});
@ -26,8 +30,10 @@ describe('Tests functionality of the localStorage module', function(){
}));
it("Should add a value to my local storage", function(){
ls.set('test', 'MyTest Value');
expect(ls.get('test')).toBe('MyTest Value');
var n = 234;
ls.set('test', n);
//Since localStorage makes the value a string, we look for the '234' and not 234
expect(ls.get('test')).toBe('234');
var obj = { key: 'val' };
ls.set('object', obj);
@ -35,4 +41,15 @@ describe('Tests functionality of the localStorage module', function(){
expect(res.key).toBe('val');
});
it('Should allow me to set a prefix', function(){
p.setPrefix("myPref");
expect(p.prefix).toBe("myPref");
});
it('Should allow me to set the cookie values', function(){
p.setStorageCookie(60, '/path');
expect(p.cookie.expiry).toBe(60);
expect(p.cookie.path).toBe('/path');
});
});