Skip to content

Commit 2cdbb7e

Browse files
committed
Merge pull request #92 from jburwell/master
Expand Unit Tests and add deriveKey
2 parents 50754b0 + 3948d7d commit 2cdbb7e

File tree

3 files changed

+74
-41
lines changed

3 files changed

+74
-41
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ bower_components
33
.tmp
44
.DS_Store
55
npm-debug.log
6-
6+
*.swp

angular-local-storage.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ angularLocalStorage.provider('localStorageService', function() {
6262
};
6363
};
6464

65+
66+
6567
this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document) {
6668

6769
var prefix = this.prefix;
@@ -79,7 +81,9 @@ angularLocalStorage.provider('localStorageService', function() {
7981
if (prefix.substr(-1) !== '.') {
8082
prefix = !!prefix ? prefix + '.' : '';
8183
}
82-
84+
var deriveQualifiedKey = function(key) {
85+
return prefix + key;
86+
}
8387
// Checks the browser to see if local storage is supported
8488
var browserSupportsLocalStorage = (function () {
8589
try {
@@ -90,7 +94,7 @@ angularLocalStorage.provider('localStorageService', function() {
9094
//
9195
// "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage
9296
// that exceeded the quota."
93-
var key = prefix + '__' + Math.round(Math.random() * 1e7);
97+
var key = deriveQualifiedKey('__' + Math.round(Math.random() * 1e7));
9498
if (supported) {
9599
webStorage.setItem(key, '');
96100
webStorage.removeItem(key);
@@ -127,7 +131,7 @@ angularLocalStorage.provider('localStorageService', function() {
127131
if (angular.isObject(value) || angular.isArray(value)) {
128132
value = angular.toJson(value);
129133
}
130-
webStorage.setItem(prefix + key, value);
134+
webStorage.setItem(deriveQualifiedKey(key), value);
131135
if (notify.setItem) {
132136
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: this.storageType});
133137
}
@@ -147,7 +151,7 @@ angularLocalStorage.provider('localStorageService', function() {
147151
return getFromCookies(key);
148152
}
149153

150-
var item = webStorage.getItem(prefix + key);
154+
var item = webStorage.getItem(deriveQualifiedKey(key));
151155
// angular.toJson will convert null to 'null', so a proper conversion is needed
152156
// FIXME not a perfect solution, since a valid 'null' string can't be stored
153157
if (!item || item === 'null') {
@@ -173,7 +177,7 @@ angularLocalStorage.provider('localStorageService', function() {
173177
}
174178

175179
try {
176-
webStorage.removeItem(prefix+key);
180+
webStorage.removeItem(deriveQualifiedKey(key));
177181
if (notify.removeItem) {
178182
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: this.storageType});
179183
}
@@ -286,7 +290,7 @@ angularLocalStorage.provider('localStorageService', function() {
286290
if(cookie.domain){
287291
cookieDomain = "; domain=" + cookie.domain;
288292
}
289-
$document.cookie = prefix + key + "=" + encodeURIComponent(value) + expiry + cookiePath + cookieDomain;
293+
$document.cookie = deriveQualifiedKey(key) + "=" + encodeURIComponent(value) + expiry + cookiePath + cookieDomain;
290294
}
291295
} catch (e) {
292296
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
@@ -309,7 +313,7 @@ angularLocalStorage.provider('localStorageService', function() {
309313
while (thisCookie.charAt(0) === ' ') {
310314
thisCookie = thisCookie.substring(1,thisCookie.length);
311315
}
312-
if (thisCookie.indexOf(prefix + key + '=') === 0) {
316+
if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) {
313317
return decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length));
314318
}
315319
}
@@ -366,6 +370,7 @@ angularLocalStorage.provider('localStorageService', function() {
366370
remove: removeFromLocalStorage,
367371
clearAll: clearAllFromLocalStorage,
368372
bind: bindToScope,
373+
deriveKey: deriveQualifiedKey,
369374
cookie: {
370375
set: addToCookies,
371376
add: addToCookies, //DEPRECATED

test/spec/test.js

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,79 @@ describe('Tests functionality of the localStorage module', function() {
99

1010
beforeEach(inject(function(_localStorageService_) {
1111
ls = _localStorageService_;
12-
spyOn(ls, 'get').andCallFake(function(key) {
13-
if (store[key].charAt(0) === '{' || store[key].charAt(0) === '[') {
14-
return angular.fromJson(store[key]);
15-
} else {
16-
return store[key];
17-
}
18-
});
19-
20-
spyOn(ls, 'set').andCallFake(function(key, val) {
21-
if (angular.isObject(val) || angular.isArray(val)) {
22-
val = angular.toJson(val);
23-
}
24-
if (angular.isNumber(val)){
25-
val = val.toString();
26-
}
27-
store[key] = val;
28-
return store[key];
29-
});
30-
31-
spyOn(ls, 'clearAll').andCallFake(function() {
32-
store = {};
33-
return store;
34-
});
12+
ls.clearAll();
13+
expect(ls.keys()).toEqual([]);
3514
}));
3615

37-
it('Should add a value to my local storage', function() {
38-
var n = 234;
39-
ls.set('test', n);
16+
it('A key should be derived to <prefix>.<key>', function() {
17+
var key = "foo";
18+
expect(ls.deriveKey(key)).toBe("ls." + key);
19+
});
20+
21+
it('Should be able to replace a key multiple times', function() {
22+
var key = "foo",
23+
expectedValues = [ "bar", "zoo", "aoo" ];
24+
25+
for (var expectedValue in expectedValues) {
26+
ls.set(key, expectedValue);
27+
expect(ls.get(key)).toBe(expectedValue);
28+
expect(ls.keys()).toEqual([key]);
29+
}
30+
});
31+
32+
it('Should delete a value from my local storage', function() {
33+
var key = "foo",
34+
expectedValue = "bar";
35+
36+
ls.set(key, expectedValue);
37+
expect(ls.get(key)).toBe(expectedValue);
38+
expect(ls.keys()).toEqual([key]);
39+
40+
expect(ls.remove(key)).toBe(true);
41+
expect(ls.get(key)).toBe(null);
42+
expect(ls.keys()).toEqual([]);
43+
});
44+
45+
it('Should add a integer value to my local storage', function() {
46+
var key = "test",
47+
expectedValue = 234;
48+
ls.set(key, expectedValue);
4049
//Since localStorage makes the value a string, we look for the '234' and not 234
41-
expect(ls.get('test')).toBe('234');
50+
expect(ls.get(key)).toBe(expectedValue.toString());
51+
expect(ls.keys()).toEqual([key]);
52+
});
53+
54+
it('Should add a String value to my local storage', function() {
55+
var key = "foo",
56+
expectedValue = "bar";
57+
ls.set(key, expectedValue);
58+
expect(ls.get(key)).toBe(expectedValue);
59+
expect(ls.keys()).toEqual([key]);
60+
});
4261

43-
var obj = { key: 'val' };
44-
ls.set('object', obj);
45-
var res = ls.get('object');
62+
it('Should add a JSON value to my local storage', function() {
63+
var key = "test",
64+
expectedValue = { key: 'val' };
65+
ls.set(key, expectedValue);
66+
67+
var res = ls.get(key);
68+
expect(res).toEqual(expectedValue);
4669
expect(res.key).toBe('val');
70+
expect(ls.keys()).toEqual([key]);
4771
});
4872

4973
it('Should allow me to set a prefix', function() {
50-
p.setPrefix('myPref');
51-
expect(p.prefix).toBe('myPref');
74+
75+
var expectedPrefix = "myPref";
76+
77+
p.setPrefix(expectedPrefix);
78+
expect(p.prefix).toBe(expectedPrefix);
79+
5280
});
5381

5482
it('Should allow me to set the cookie values', function() {
5583
p.setStorageCookie(60, '/path');
5684
expect(p.cookie.expiry).toBe(60);
5785
expect(p.cookie.path).toBe('/path');
5886
});
59-
});
87+
});

0 commit comments

Comments
 (0)