Skip to content

Commit 2d4b402

Browse files
committed
test(localStorageSpec): upgrade coverage, improve mocks
1 parent 52d04ad commit 2d4b402

File tree

3 files changed

+127
-26
lines changed

3 files changed

+127
-26
lines changed

test/karma.conf.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ module.exports = function(config) {
2222
// - Safari (only Mac)
2323
// - PhantomJS
2424
// - IE (only Windows)
25-
browsers: ['Chrome'],
25+
browsers: ['PhantomJS'],
2626

2727
// list of files / patterns to load in the browser
2828
files: [
2929
bower + 'angular/angular.js',
3030
bower + 'angular-mocks/angular-mocks.js',
3131
'src/*.js',
32+
'test/mock/*.js',
3233
'test/spec/**/*.js'
3334
],
3435

@@ -44,6 +45,19 @@ module.exports = function(config) {
4445

4546
// Continuous Integration mode
4647
// if true, it capture browsers, run tests and exit
47-
singleRun: true
48+
singleRun: true,
49+
50+
reporters: ['progress', 'coverage'],
51+
52+
// preprocessors
53+
preprocessors: {
54+
'src/*.js': ['coverage']
55+
},
56+
57+
// configure the reporter
58+
coverageReporter: {
59+
type : 'lcov',
60+
dir : 'coverage/'
61+
}
4862
});
4963
};

test/mock/localStorageMock.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
//Mock localStorage
3+
function localStorageMock() {
4+
var storage = {};
5+
Object.defineProperties(storage, {
6+
setItem: {
7+
value: function(key, value) {
8+
storage[key] = value || '';
9+
},
10+
enumerable: false,
11+
writable: true
12+
},
13+
getItem: {
14+
value: function(key) {
15+
return storage[key];
16+
},
17+
enumerable: false,
18+
writable: true
19+
},
20+
removeItem: {
21+
value: function(key) {
22+
delete storage[key];
23+
},
24+
enumerable: false,
25+
writable: true
26+
},
27+
length: {
28+
get: function() {
29+
return Object.keys(storage).length;
30+
},
31+
enumerable: false
32+
},
33+
key: {
34+
value: function(i) {
35+
var aKeys = Object.keys(storage);
36+
return aKeys[i] || null;
37+
},
38+
enumerable: false
39+
}
40+
});
41+
return storage;
42+
}

test/spec/localStorageSpec.js

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,6 @@
33
describe('localStorageService', function() {
44
var elmSpy;
55

6-
//Mock
7-
function localStorageMock() {
8-
var keys = {};
9-
10-
return {
11-
setItem: function(key, value) {
12-
keys[key] = value || '';
13-
},
14-
getItem: function(key) {
15-
return keys[key];
16-
},
17-
removeItem: function(key) {
18-
delete keys[key];
19-
},
20-
get length() {
21-
return Object.keys(keys).length;
22-
},
23-
key: function(i) {
24-
var aKeys = Object.keys(keys);
25-
return aKeys[i] || null;
26-
}
27-
};
28-
}
29-
306
//Actions
317
function getItem(key) {
328
return function($window, localStorageService) {
@@ -155,6 +131,11 @@ describe('localStorageService', function() {
155131
expectAdding('ls.foo', 'bar')
156132
));
157133

134+
it('should add key to localeStorage null if value not provided', inject(
135+
addItem('foo'),
136+
expectAdding('ls.foo', null)
137+
));
138+
158139
it('should support to set custom prefix', function() {
159140
module(setPrefix('myApp'));
160141
inject(
@@ -266,6 +247,17 @@ describe('localStorageService', function() {
266247
});
267248
});
268249

250+
it('should be able to notify/broadcasting if set', function() {
251+
module(setNotify(true, true));
252+
inject(function($rootScope, localStorageService) {
253+
var spy = spyOn($rootScope, '$broadcast');
254+
255+
localStorageService.set('a8m', 'foobar');
256+
localStorageService.remove('a8m', 'foobar');
257+
expect(spy.callCount).toEqual(2);
258+
});
259+
});
260+
269261
it('should be able to bind to scope', inject(function($rootScope, localStorageService) {
270262

271263
localStorageService.set('property', 'oldValue');
@@ -348,6 +340,33 @@ describe('localStorageService', function() {
348340
expect($window.localStorage.length).toEqual(20);
349341
}));
350342

343+
it('should be able to clear all owned keys from storage',inject(function($window, localStorageService) {
344+
for(var i = 0; i < 10; i++) {
345+
localStorageService.set('key' + i, 'val' + i);
346+
$window.localStorage.setItem('key' + i, 'val' + i);
347+
}
348+
349+
localStorageService.clearAll();
350+
//remove only owned keys
351+
for(var l = 0; l < 10; l++) {
352+
expect(localStorageService.get('key' + l)).toEqual(null);
353+
expect($window.localStorage.getItem('key' + l)).toEqual('val' + l);
354+
}
355+
}));
356+
357+
it('should return array of all owned keys', inject(function($window, localStorageService) {
358+
//set keys
359+
for(var i = 0; i < 10; i++) {
360+
//localStorageService
361+
localStorageService.set('ownKey' + i, 'val' + i);
362+
//window.localStorage
363+
$window.localStorage.setItem('windowKey' + i, 'val' + i);
364+
}
365+
localStorageService.keys().forEach(function(el, i) {
366+
expect(el).toEqual('ownKey' + i);
367+
});
368+
}));
369+
351370
//sessionStorage
352371
describe('SessionStorage', function() {
353372

@@ -452,6 +471,32 @@ describe('localStorageService', function() {
452471
expect(localStorageService.cookie.get('cookieKey')).toEqual(['foo', 'bar']);
453472
}));
454473

474+
it('should be able to clear all owned keys from cookie', inject(function(localStorageService, $document) {
475+
localStorageService.set('ownKey1', 1);
476+
$document.cookie = "username=John Doe";
477+
localStorageService.clearAll();
478+
expect(localStorageService.get('ownKey1')).toEqual(null);
479+
expect($document.cookie).not.toEqual('');
480+
}));
481+
482+
it('should be broadcast on adding item', function() {
483+
module(setNotify(true, false));
484+
inject(function($rootScope, localStorageService) {
485+
var spy = spyOn($rootScope, '$broadcast');
486+
localStorageService.set('a8m', 'foobar');
487+
expect(spy).toHaveBeenCalled();
488+
});
489+
});
490+
491+
it('should be broadcast on removing item', function() {
492+
module(setNotify(false, true));
493+
inject(function($rootScope, localStorageService) {
494+
var spy = spyOn($rootScope, '$broadcast');
495+
localStorageService.remove('a8m', 'foobar');
496+
expect(spy).toHaveBeenCalled();
497+
});
498+
});
499+
455500
Date.prototype.addDays = function(days) {
456501
var date = new Date(this.getTime());
457502
date.setDate(date.getDate() + days);

0 commit comments

Comments
 (0)