@@ -11,16 +11,17 @@ function clear(): void {
11
11
localStorage . clear ( )
12
12
}
13
13
14
+ let store : LocalStorage
15
+ beforeEach ( function ( ) {
16
+ store = new LocalStorage ( )
17
+ clear ( )
18
+ } )
19
+
14
20
describe ( 'user' , ( ) => {
15
21
const cookieKey = User . defaults . cookie . key
16
22
const localStorageKey = User . defaults . localStorage . key
17
- const store = new LocalStorage ( )
18
23
19
24
describe ( '()' , ( ) => {
20
- beforeEach ( ( ) => {
21
- clear ( )
22
- } )
23
-
24
25
it ( 'should pick the old "_sio" anonymousId' , ( ) => {
25
26
jar . set ( '_sio' , 'anonymous-id----user-id' )
26
27
const user = new User ( )
@@ -61,7 +62,6 @@ describe('user', () => {
61
62
62
63
beforeEach ( ( ) => {
63
64
user = new User ( )
64
- clear ( )
65
65
} )
66
66
67
67
describe ( 'when cookies are disabled' , ( ) => {
@@ -294,15 +294,13 @@ describe('user', () => {
294
294
295
295
beforeEach ( ( ) => {
296
296
user = new User ( )
297
- clear ( )
298
297
} )
299
298
300
299
describe ( 'when cookies are disabled' , ( ) => {
301
300
beforeEach ( ( ) => {
302
301
jest . spyOn ( Cookie , 'available' ) . mockReturnValueOnce ( false )
303
302
304
303
user = new User ( )
305
- clear ( )
306
304
} )
307
305
308
306
it ( 'should get an id from the store' , ( ) => {
@@ -332,7 +330,6 @@ describe('user', () => {
332
330
jest . spyOn ( Cookie , 'available' ) . mockReturnValueOnce ( false )
333
331
334
332
user = new User ( )
335
- clear ( )
336
333
} )
337
334
338
335
it ( 'should get an id from memory' , ( ) => {
@@ -444,7 +441,6 @@ describe('user', () => {
444
441
445
442
beforeEach ( ( ) => {
446
443
user = new User ( )
447
- clear ( )
448
444
} )
449
445
450
446
it ( 'should get traits' , ( ) => {
@@ -537,7 +533,6 @@ describe('user', () => {
537
533
538
534
beforeEach ( ( ) => {
539
535
user = new User ( )
540
- clear ( )
541
536
} )
542
537
543
538
it ( 'should save an id to a cookie' , ( ) => {
@@ -604,7 +599,6 @@ describe('user', () => {
604
599
605
600
beforeEach ( ( ) => {
606
601
user = new User ( )
607
- clear ( )
608
602
} )
609
603
610
604
it ( 'should reset an id and traits' , ( ) => {
@@ -647,7 +641,6 @@ describe('user', () => {
647
641
648
642
beforeEach ( ( ) => {
649
643
user = new User ( )
650
- clear ( )
651
644
} )
652
645
653
646
it ( 'should save an id' , ( ) => {
@@ -704,7 +697,6 @@ describe('user', () => {
704
697
705
698
beforeEach ( ( ) => {
706
699
user = new User ( )
707
- clear ( )
708
700
} )
709
701
710
702
it ( 'should load an empty user' , ( ) => {
@@ -751,12 +743,6 @@ describe('user', () => {
751
743
} )
752
744
753
745
describe ( 'group' , ( ) => {
754
- const store = new LocalStorage ( )
755
-
756
- beforeEach ( ( ) => {
757
- clear ( )
758
- } )
759
-
760
746
it ( 'should not reset id and traits' , ( ) => {
761
747
let group = new Group ( )
762
748
group . id ( 'gid' )
@@ -865,19 +851,36 @@ describe('group', () => {
865
851
} )
866
852
867
853
describe ( 'store' , function ( ) {
868
- const store = new LocalStorage ( )
869
- beforeEach ( function ( ) {
870
- clear ( )
871
- } )
872
-
873
854
describe ( '#get' , function ( ) {
874
- it ( 'should not not get an empty record' , function ( ) {
875
- expect ( store . get ( 'abc' ) === undefined )
855
+ it ( 'should return null if localStorage throws an error (or does not exist)' , function ( ) {
856
+ const getItemSpy = jest
857
+ . spyOn ( global . Storage . prototype , 'getItem' )
858
+ . mockImplementationOnce ( ( ) => {
859
+ throw new Error ( 'getItem fail.' )
860
+ } )
861
+ store . set ( 'foo' , 'some value' )
862
+ expect ( store . get ( 'foo' ) ) . toBeNull ( )
863
+ expect ( getItemSpy ) . toBeCalledTimes ( 1 )
864
+ } )
865
+
866
+ it ( 'should not get an empty record' , function ( ) {
867
+ expect ( store . get ( 'abc' ) ) . toBe ( null )
876
868
} )
877
869
878
870
it ( 'should get an existing record' , function ( ) {
879
871
store . set ( 'x' , { a : 'b' } )
872
+ store . set ( 'a' , 'hello world' )
873
+ store . set ( 'b' , '' )
874
+ store . set ( 'c' , false )
875
+ store . set ( 'd' , null )
876
+ store . set ( 'e' , undefined )
877
+
880
878
expect ( store . get ( 'x' ) ) . toStrictEqual ( { a : 'b' } )
879
+ expect ( store . get ( 'a' ) ) . toBe ( 'hello world' )
880
+ expect ( store . get ( 'b' ) ) . toBe ( '' )
881
+ expect ( store . get ( 'c' ) ) . toBe ( false )
882
+ expect ( store . get ( 'd' ) ) . toBe ( null )
883
+ expect ( store . get ( 'e' ) ) . toBe ( 'undefined' )
881
884
} )
882
885
} )
883
886
@@ -900,16 +903,12 @@ describe('store', function () {
900
903
store . set ( 'x' , { a : 'b' } )
901
904
expect ( store . get ( 'x' ) ) . toStrictEqual ( { a : 'b' } )
902
905
store . remove ( 'x' )
903
- expect ( store . get ( 'x' ) === undefined )
906
+ expect ( store . get ( 'x' ) ) . toBe ( null )
904
907
} )
905
908
} )
906
909
} )
907
910
908
911
describe ( 'Custom cookie params' , ( ) => {
909
- beforeEach ( ( ) => {
910
- clear ( )
911
- } )
912
-
913
912
it ( 'allows for overriding keys' , ( ) => {
914
913
const customUser = new User (
915
914
{ } ,
0 commit comments