- Adds the deriveQualifiedKey and deriveKey methods to ca;culate the key

with the prefix -- supporting deterministic internal operation and
loosely coupled white box testing
- Adds tests for remove, deriveKey, and key replacement
- Splits the add tests into 3 scenerios -- add JSON, add string, and add
  integer
- Remove the test stubs for get, set, and clearAll to properly test the
  underlying methods
revert-117-master
John Burwell 10 years ago
parent 7c1515a253
commit 9129451a27

@ -62,6 +62,8 @@ angularLocalStorage.provider('localStorageService', function() {
};
};
this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document) {
var prefix = this.prefix;
@ -79,7 +81,9 @@ angularLocalStorage.provider('localStorageService', function() {
if (prefix.substr(-1) !== '.') {
prefix = !!prefix ? prefix + '.' : '';
}
var deriveQualifiedKey = function(key) {
return prefix + key;
}
// Checks the browser to see if local storage is supported
var browserSupportsLocalStorage = (function () {
try {
@ -90,7 +94,7 @@ angularLocalStorage.provider('localStorageService', function() {
//
// "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage
// that exceeded the quota."
var key = prefix + '__' + Math.round(Math.random() * 1e7);
var key = deriveQualifiedKey('__' + Math.round(Math.random() * 1e7));
if (supported) {
webStorage.setItem(key, '');
webStorage.removeItem(key);
@ -127,7 +131,7 @@ angularLocalStorage.provider('localStorageService', function() {
if (angular.isObject(value) || angular.isArray(value)) {
value = angular.toJson(value);
}
webStorage.setItem(prefix + key, value);
webStorage.setItem(deriveQualifiedKey(key), value);
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: this.storageType});
}
@ -147,7 +151,7 @@ angularLocalStorage.provider('localStorageService', function() {
return getFromCookies(key);
}
var item = webStorage.getItem(prefix + key);
var item = webStorage.getItem(deriveQualifiedKey(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') {
@ -173,7 +177,7 @@ angularLocalStorage.provider('localStorageService', function() {
}
try {
webStorage.removeItem(prefix+key);
webStorage.removeItem(deriveQualifiedKey(key));
if (notify.removeItem) {
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: this.storageType});
}
@ -286,7 +290,7 @@ angularLocalStorage.provider('localStorageService', function() {
if(cookie.domain){
cookieDomain = "; domain=" + cookie.domain;
}
$document.cookie = prefix + key + "=" + encodeURIComponent(value) + expiry + cookiePath + cookieDomain;
$document.cookie = deriveQualifiedKey(key) + "=" + encodeURIComponent(value) + expiry + cookiePath + cookieDomain;
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
@ -309,7 +313,7 @@ angularLocalStorage.provider('localStorageService', function() {
while (thisCookie.charAt(0) === ' ') {
thisCookie = thisCookie.substring(1,thisCookie.length);
}
if (thisCookie.indexOf(prefix + key + '=') === 0) {
if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) {
return decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length));
}
}
@ -366,6 +370,7 @@ angularLocalStorage.provider('localStorageService', function() {
remove: removeFromLocalStorage,
clearAll: clearAllFromLocalStorage,
bind: bindToScope,
deriveKey: deriveQualifiedKey,
cookie: {
set: addToCookies,
add: addToCookies, //DEPRECATED

@ -9,46 +9,67 @@ describe('Tests functionality of the localStorage module', function() {
beforeEach(inject(function(_localStorageService_) {
ls = _localStorageService_;
spyOn(ls, 'get').andCallFake(function(key) {
if (store[key].charAt(0) === '{' || store[key].charAt(0) === '[') {
return angular.fromJson(store[key]);
} else {
return store[key];
}
});
spyOn(ls, 'set').andCallFake(function(key, val) {
if (angular.isObject(val) || angular.isArray(val)) {
val = angular.toJson(val);
}
if (angular.isNumber(val)){
val = val.toString();
}
store[key] = val;
return store[key];
});
spyOn(ls, 'clearAll').andCallFake(function() {
store = {};
return store;
});
}));
it('Should add a value to my local storage', function() {
var n = 234;
ls.set('test', n);
it('A key should be derived to <prefix>.<key>', function() {
var key = "foo";
expect(ls.deriveKey(key)).toBe("ls." + key);
});
it('Should be able to replace a key multiple times', function() {
var key = "foo",
expectedValues = [ "bar", "zoo", "aoo" ];
for (var expectedValue in expectedValues) {
ls.set(key, expectedValue);
expect(ls.get(key)).toBe(expectedValue);
}
});
it('Should delete a value from my local storage', function() {
var key = "foo",
expected_value = "bar";
ls.set(key, expected_value);
expect(ls.get(key)).toBe(expected_value);
expect(ls.remove(key)).toBe(true);
expect(ls.get(key)).toBe(null);
});
it('Should add a integer value to my local storage', function() {
var key = "test",
expectedValue = 234;
ls.set(key, expectedValue);
//Since localStorage makes the value a string, we look for the '234' and not 234
expect(ls.get('test')).toBe('234');
expect(ls.get(key)).toBe(expectedValue.toString());
});
it('Should add a String value to my local storage', function() {
var key = "foo",
expectedValue = "bar";
ls.set(key, expectedValue);
expect(ls.get(key)).toBe(expectedValue);
});
var obj = { key: 'val' };
ls.set('object', obj);
var res = ls.get('object');
it('Should add a JSON value to my local storage', function() {
var key = "test",
expectedValue = { key: 'val' };
ls.set(key, expectedValue);
var res = ls.get(key);
expect(res).toEqual(expectedValue);
expect(res.key).toBe('val');
});
it('Should allow me to set a prefix', function() {
p.setPrefix('myPref');
expect(p.prefix).toBe('myPref');
var expectedPrefix = "myPref";
p.setPrefix(expectedPrefix);
expect(p.prefix).toBe(expectedPrefix);
});
it('Should allow me to set the cookie values', function() {
@ -56,4 +77,4 @@ describe('Tests functionality of the localStorage module', function() {
expect(p.cookie.expiry).toBe(60);
expect(p.cookie.path).toBe('/path');
});
});
});