@@ -57,10 +57,28 @@ describe('user', function() {
57
57
// verify id() returns the id even when cookie is deleted.
58
58
assert . equal ( user . id ( ) , 'id' ) ;
59
59
60
- // verify cookie value is retored from localStorage.
60
+ // verify cookie value is restored from localStorage.
61
61
assert . equal ( cookie . get ( cookieKey ) , 'id' ) ;
62
62
} ) ;
63
63
64
+ it ( 'id() should not fallback to localStorage when disabled' , function ( ) {
65
+ var user = new User ( ) ;
66
+ user . options ( {
67
+ localStorageFallbackDisabled : true
68
+ } ) ;
69
+
70
+ user . id ( 'id' ) ;
71
+
72
+ // delete the cookie.
73
+ cookie . remove ( cookieKey ) ;
74
+
75
+ // verify cookie is deleted.
76
+ assert . equal ( cookie . get ( cookieKey ) , null ) ;
77
+
78
+ // verify id() does not return the id when cookie is deleted.
79
+ assert . equal ( user . id ( ) , null ) ;
80
+ } ) ;
81
+
64
82
it ( 'should pick the old "_sio" anonymousId' , function ( ) {
65
83
rawCookie ( '_sio' , 'anonymous-id----user-id' ) ;
66
84
var user = new User ( ) ;
@@ -345,13 +363,33 @@ describe('user', function() {
345
363
assert . equal ( store . get ( 'ajs_anonymous_id' ) , 'anon0' ) ;
346
364
} ) ;
347
365
366
+ it ( 'should not set anonymousId in localStorage when localStorage fallback is disabled' , function ( ) {
367
+ var user = new User ( ) ;
368
+ user . options ( {
369
+ localStorageFallbackDisabled : true
370
+ } ) ;
371
+ user . anonymousId ( 'anon0' ) ;
372
+ assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , 'anon0' ) ;
373
+ assert . equal ( store . get ( 'ajs_anonymous_id' ) , null ) ;
374
+ } ) ;
375
+
348
376
it ( 'should copy value from cookie to localStorage' , function ( ) {
349
377
var user = new User ( ) ;
350
378
cookie . set ( 'ajs_anonymous_id' , 'anon1' ) ;
351
379
assert . equal ( user . anonymousId ( ) , 'anon1' ) ;
352
380
assert . equal ( store . get ( 'ajs_anonymous_id' ) , 'anon1' ) ;
353
381
} ) ;
354
382
383
+ it ( 'should not copy value from cookie to localStorage when localStorage fallback is disabled' , function ( ) {
384
+ var user = new User ( ) ;
385
+ user . options ( {
386
+ localStorageFallbackDisabled : true
387
+ } ) ;
388
+ cookie . set ( 'ajs_anonymous_id' , 'anon1' ) ;
389
+ assert . equal ( user . anonymousId ( ) , 'anon1' ) ;
390
+ assert . equal ( store . get ( 'ajs_anonymous_id' ) , null ) ;
391
+ } ) ;
392
+
355
393
it ( 'should fall back to localStorage when cookie is not set' , function ( ) {
356
394
var user = new User ( ) ;
357
395
@@ -365,17 +403,47 @@ describe('user', function() {
365
403
// verify anonymousId() returns the correct id even when there's no cookie
366
404
assert . equal ( user . anonymousId ( ) , 'anon12' ) ;
367
405
368
- // verify cookie value is retored from localStorage
406
+ // verify cookie value is restored from localStorage
369
407
assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , 'anon12' ) ;
370
408
} ) ;
371
409
410
+ it ( 'should not fall back to localStorage when cookie is not set and localStorage fallback is disabled' , function ( ) {
411
+ var user = new User ( ) ;
412
+ user . options ( {
413
+ localStorageFallbackDisabled : true
414
+ } ) ;
415
+
416
+ user . anonymousId ( 'anon12' ) ;
417
+ assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , 'anon12' ) ;
418
+
419
+ // delete the cookie
420
+ cookie . remove ( 'ajs_anonymous_id' ) ;
421
+ assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , null ) ;
422
+
423
+ // verify anonymousId() does not return the id when there's no cookie.
424
+ assert . notEqual ( user . anonymousId ( ) , 'anon12' ) ;
425
+ } ) ;
426
+
372
427
it ( 'should write to both cookie and localStorage when generating a new anonymousId' , function ( ) {
373
428
var user = new User ( ) ;
374
429
var anonId = user . anonymousId ( ) ;
375
430
assert . notEqual ( anonId , null ) ;
376
431
assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , anonId ) ;
377
432
assert . equal ( store . get ( 'ajs_anonymous_id' ) , anonId ) ;
378
433
} ) ;
434
+
435
+ it ( 'should not write to both cookie and localStorage when generating a new anonymousId and localStorage fallback is disabled' , function ( ) {
436
+ var user = new User ( ) ;
437
+ user . options ( {
438
+ localStorageFallbackDisabled : true
439
+ } ) ;
440
+
441
+ var anonId = user . anonymousId ( ) ;
442
+
443
+ assert . notEqual ( anonId , null ) ;
444
+ assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , anonId ) ;
445
+ assert . equal ( store . get ( 'ajs_anonymous_id' ) , null ) ;
446
+ } ) ;
379
447
} ) ;
380
448
} ) ;
381
449
@@ -463,6 +531,17 @@ describe('user', function() {
463
531
assert . equal ( store . get ( cookieKey ) , 'id' ) ;
464
532
} ) ;
465
533
534
+ it ( 'should not save an id to localStorage when localStorage fallback is disabled' , function ( ) {
535
+ user . options ( {
536
+ localStorageFallbackDisabled : true
537
+ } ) ;
538
+ user . id ( 'id' ) ;
539
+
540
+ user . save ( ) ;
541
+
542
+ assert . equal ( store . get ( cookieKey ) , null ) ;
543
+ } ) ;
544
+
466
545
it ( 'should save traits to local storage' , function ( ) {
467
546
user . traits ( { trait : true } ) ;
468
547
user . save ( ) ;
0 commit comments