1
1
'use strict' ;
2
2
3
+ /*global localStorageMock*/
3
4
describe ( 'localStorageService' , function ( ) {
4
5
var elmSpy ;
5
6
@@ -126,9 +127,16 @@ describe('localStorageService', function() {
126
127
}
127
128
128
129
beforeEach ( module ( 'LocalStorageModule' , function ( $provide ) {
129
-
130
+ spyOn ( window , 'addEventListener' ) . andCallThrough ( ) ;
131
+ spyOn ( window , 'removeEventListener' ) . andCallThrough ( ) ;
130
132
$provide . value ( '$window' , {
131
- localStorage : localStorageMock ( )
133
+ localStorage : localStorageMock ( ) ,
134
+ addEventListener : function ( ) {
135
+ window . addEventListener . apply ( window , arguments ) ;
136
+ } ,
137
+ removeEventListener : function ( ) {
138
+ window . removeEventListener . apply ( window , arguments ) ;
139
+ }
132
140
} ) ;
133
141
134
142
} ) ) ;
@@ -267,7 +275,7 @@ describe('localStorageService', function() {
267
275
addItem ( 'key' , '777' ) ,
268
276
expectAdding ( 'ls.key' , angular . toJson ( '777' ) ) ,
269
277
expectMatching ( 'key' , '777' )
270
- )
278
+ ) ;
271
279
} ) ;
272
280
273
281
it ( 'should be able to get items' , inject (
@@ -359,6 +367,7 @@ describe('localStorageService', function() {
359
367
$rootScope . $digest ( ) ;
360
368
361
369
expect ( $rootScope . property ) . toEqual ( localStorageService . get ( 'property' ) ) ;
370
+ expect ( window . addEventListener ) . toHaveBeenCalled ( ) ;
362
371
} ) ) ;
363
372
364
373
it ( 'should be able to unbind from scope variable' , inject ( function ( $rootScope , localStorageService ) {
@@ -376,6 +385,7 @@ describe('localStorageService', function() {
376
385
$rootScope . $digest ( ) ;
377
386
378
387
expect ( $rootScope . property ) . not . toEqual ( localStorageService . get ( 'property' ) ) ;
388
+ expect ( window . removeEventListener ) . toHaveBeenCalled ( ) ;
379
389
} ) ) ;
380
390
381
391
it ( 'should be able to bind to properties of objects' , inject ( function ( $rootScope , localStorageService ) {
@@ -391,6 +401,47 @@ describe('localStorageService', function() {
391
401
expect ( $rootScope . obj . property ) . toEqual ( localStorageService . get ( 'obj.property' ) ) ;
392
402
} ) ) ;
393
403
404
+ it ( 'should update a bound value when local storage is updated' , inject ( function ( $rootScope , localStorageService , $window ) {
405
+ localStorageService . bind ( $rootScope , 'test' ) ;
406
+ $rootScope . $digest ( ) ;
407
+
408
+ // set the value in local storage mock to a value, then emit a changed event
409
+ var testValue = 'test' ;
410
+ $window . localStorage [ 'ls.test' ] = testValue ;
411
+ var evt = document . createEvent ( 'CustomEvent' ) ;
412
+ evt . key = 'ls.test' ;
413
+ evt . newValue = 'test value' ;
414
+ evt . initCustomEvent ( 'storage' , true , true , {
415
+ key : 'ls.test' ,
416
+ newValue : testValue
417
+ } ) ;
418
+ window . dispatchEvent ( evt ) ;
419
+ $rootScope . $digest ( ) ;
420
+
421
+ expect ( $rootScope . test ) . toEqual ( testValue ) ;
422
+ } ) ) ;
423
+
424
+ it ( 'should keep unserializable properties of bound objects when local storage is updated' , inject ( function ( $rootScope , localStorageService , $window ) {
425
+ localStorageService . bind ( $rootScope , 'test' ) ;
426
+ $rootScope . test = { obj : { a :1 } , func : function ( ) { } } ;
427
+ // set the value in local storage mock to a value, then emit a changed event
428
+ var testValue = JSON . stringify ( { obj :{ a :2 } } ) ;
429
+ $window . localStorage [ 'ls.test' ] = testValue ;
430
+ var evt = document . createEvent ( 'CustomEvent' ) ;
431
+ evt . key = 'ls.test' ;
432
+ evt . newValue = 'test value' ;
433
+ evt . initCustomEvent ( 'storage' , true , true , {
434
+ key : 'ls.test' ,
435
+ newValue : testValue
436
+ } ) ;
437
+ window . dispatchEvent ( evt ) ;
438
+ $rootScope . $digest ( ) ;
439
+
440
+ expect ( $rootScope . test . obj ) . toEqual ( { a :2 } ) ;
441
+ expect ( $rootScope . test . func ) . toBeDefined ( ) ;
442
+ expect ( typeof $rootScope . test . func ) . toBe ( 'function' ) ;
443
+ } ) ) ;
444
+
394
445
it ( 'should be able to bind to scope using different key' , inject ( function ( $rootScope , localStorageService ) {
395
446
396
447
localStorageService . set ( 'lsProperty' , 'oldValue' ) ;
@@ -402,6 +453,7 @@ describe('localStorageService', function() {
402
453
$rootScope . $digest ( ) ;
403
454
404
455
expect ( $rootScope . property ) . toEqual ( localStorageService . get ( 'lsProperty' ) ) ;
456
+ expect ( window . addEventListener ) . toHaveBeenCalled ( ) ;
405
457
} ) ) ;
406
458
407
459
it ( 'should $watch with deep comparison only for objects' , inject ( function ( $rootScope , localStorageService ) {
@@ -477,7 +529,7 @@ describe('localStorageService', function() {
477
529
localStorageService . set ( 'keyAlpha' , 'val' ) ;
478
530
479
531
localStorageService . clearAll ( / ^ k e y / ) ;
480
-
532
+
481
533
for ( var l = 0 ; l < 10 ; l ++ ) {
482
534
expect ( localStorageService . get ( 'key' + l ) ) . toEqual ( null ) ;
483
535
expect ( $window . localStorage . getItem ( 'key' + l ) ) . toEqual ( null ) ;
@@ -630,7 +682,7 @@ describe('localStorageService', function() {
630
682
631
683
it ( 'should be able to clear all owned keys from cookie' , inject ( function ( localStorageService , $document ) {
632
684
localStorageService . set ( 'ownKey1' , 1 ) ;
633
- $document . cookie = " username=John Doe" ;
685
+ $document . cookie = ' username=John Doe' ;
634
686
localStorageService . clearAll ( ) ;
635
687
expect ( localStorageService . get ( 'ownKey1' ) ) . toEqual ( null ) ;
636
688
expect ( $document . cookie ) . not . toEqual ( '' ) ;
0 commit comments