|
3 | 3 | describe('localStorageService', function() { |
4 | 4 | var elmSpy; |
5 | 5 |
|
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 | | - |
30 | 6 | //Actions |
31 | 7 | function getItem(key) { |
32 | 8 | return function($window, localStorageService) { |
@@ -155,6 +131,11 @@ describe('localStorageService', function() { |
155 | 131 | expectAdding('ls.foo', 'bar') |
156 | 132 | )); |
157 | 133 |
|
| 134 | + it('should add key to localeStorage null if value not provided', inject( |
| 135 | + addItem('foo'), |
| 136 | + expectAdding('ls.foo', null) |
| 137 | + )); |
| 138 | + |
158 | 139 | it('should support to set custom prefix', function() { |
159 | 140 | module(setPrefix('myApp')); |
160 | 141 | inject( |
@@ -266,6 +247,17 @@ describe('localStorageService', function() { |
266 | 247 | }); |
267 | 248 | }); |
268 | 249 |
|
| 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 | + |
269 | 261 | it('should be able to bind to scope', inject(function($rootScope, localStorageService) { |
270 | 262 |
|
271 | 263 | localStorageService.set('property', 'oldValue'); |
@@ -348,6 +340,33 @@ describe('localStorageService', function() { |
348 | 340 | expect($window.localStorage.length).toEqual(20); |
349 | 341 | })); |
350 | 342 |
|
| 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 | + |
351 | 370 | //sessionStorage |
352 | 371 | describe('SessionStorage', function() { |
353 | 372 |
|
@@ -452,6 +471,32 @@ describe('localStorageService', function() { |
452 | 471 | expect(localStorageService.cookie.get('cookieKey')).toEqual(['foo', 'bar']); |
453 | 472 | })); |
454 | 473 |
|
| 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 | + |
455 | 500 | Date.prototype.addDays = function(days) { |
456 | 501 | var date = new Date(this.getTime()); |
457 | 502 | date.setDate(date.getDate() + days); |
|
0 commit comments