@@ -57,10 +57,28 @@ describe('user', function() {
5757 // verify id() returns the id even when cookie is deleted.
5858 assert . equal ( user . id ( ) , 'id' ) ;
5959
60- // verify cookie value is retored from localStorage.
60+ // verify cookie value is restored from localStorage.
6161 assert . equal ( cookie . get ( cookieKey ) , 'id' ) ;
6262 } ) ;
6363
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+
6482 it ( 'should pick the old "_sio" anonymousId' , function ( ) {
6583 rawCookie ( '_sio' , 'anonymous-id----user-id' ) ;
6684 var user = new User ( ) ;
@@ -345,13 +363,33 @@ describe('user', function() {
345363 assert . equal ( store . get ( 'ajs_anonymous_id' ) , 'anon0' ) ;
346364 } ) ;
347365
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+
348376 it ( 'should copy value from cookie to localStorage' , function ( ) {
349377 var user = new User ( ) ;
350378 cookie . set ( 'ajs_anonymous_id' , 'anon1' ) ;
351379 assert . equal ( user . anonymousId ( ) , 'anon1' ) ;
352380 assert . equal ( store . get ( 'ajs_anonymous_id' ) , 'anon1' ) ;
353381 } ) ;
354382
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+
355393 it ( 'should fall back to localStorage when cookie is not set' , function ( ) {
356394 var user = new User ( ) ;
357395
@@ -365,17 +403,47 @@ describe('user', function() {
365403 // verify anonymousId() returns the correct id even when there's no cookie
366404 assert . equal ( user . anonymousId ( ) , 'anon12' ) ;
367405
368- // verify cookie value is retored from localStorage
406+ // verify cookie value is restored from localStorage
369407 assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , 'anon12' ) ;
370408 } ) ;
371409
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+
372427 it ( 'should write to both cookie and localStorage when generating a new anonymousId' , function ( ) {
373428 var user = new User ( ) ;
374429 var anonId = user . anonymousId ( ) ;
375430 assert . notEqual ( anonId , null ) ;
376431 assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , anonId ) ;
377432 assert . equal ( store . get ( 'ajs_anonymous_id' ) , anonId ) ;
378433 } ) ;
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+ } ) ;
379447 } ) ;
380448 } ) ;
381449
@@ -463,6 +531,17 @@ describe('user', function() {
463531 assert . equal ( store . get ( cookieKey ) , 'id' ) ;
464532 } ) ;
465533
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+
466545 it ( 'should save traits to local storage' , function ( ) {
467546 user . traits ( { trait : true } ) ;
468547 user . save ( ) ;
0 commit comments