@@ -43,6 +43,24 @@ describe('user', function() {
4343 assert ( user . traits ( ) . trait === true ) ;
4444 } ) ;
4545
46+ it ( 'id() should fallback to localStorage' , function ( ) {
47+ var user = new User ( ) ;
48+
49+ user . id ( 'id' ) ;
50+
51+ // delete the cookie.
52+ cookie . remove ( cookieKey ) ;
53+
54+ // verify cookie is deleted.
55+ assert . equal ( cookie . get ( cookieKey ) , null ) ;
56+
57+ // verify id() returns the id even when cookie is deleted.
58+ assert . equal ( user . id ( ) , 'id' ) ;
59+
60+ // verify cookie value is retored from localStorage.
61+ assert . equal ( cookie . get ( cookieKey ) , 'id' ) ;
62+ } ) ;
63+
4664 it ( 'should pick the old "_sio" anonymousId' , function ( ) {
4765 rawCookie ( '_sio' , 'anonymous-id----user-id' ) ;
4866 var user = new User ( ) ;
@@ -319,6 +337,45 @@ describe('user', function() {
319337 } ;
320338 assert ( user . anonymousId ( ) === undefined ) ;
321339 } ) ;
340+
341+ it ( 'should set anonymousId in both cookie and localStorage' , function ( ) {
342+ var user = new User ( ) ;
343+ user . anonymousId ( 'anon0' ) ;
344+ assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , 'anon0' ) ;
345+ assert . equal ( store . get ( 'ajs_anonymous_id' ) , 'anon0' ) ;
346+ } ) ;
347+
348+ it ( 'should copy value from cookie to localStorage' , function ( ) {
349+ var user = new User ( ) ;
350+ cookie . set ( 'ajs_anonymous_id' , 'anon1' ) ;
351+ assert . equal ( user . anonymousId ( ) , 'anon1' ) ;
352+ assert . equal ( store . get ( 'ajs_anonymous_id' ) , 'anon1' ) ;
353+ } ) ;
354+
355+ it ( 'should fall back to localStorage when cookie is not set' , function ( ) {
356+ var user = new User ( ) ;
357+
358+ user . anonymousId ( 'anon12' ) ;
359+ assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , 'anon12' ) ;
360+
361+ // delete the cookie
362+ cookie . remove ( 'ajs_anonymous_id' ) ;
363+ assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , null ) ;
364+
365+ // verify anonymousId() returns the correct id even when there's no cookie
366+ assert . equal ( user . anonymousId ( ) , 'anon12' ) ;
367+
368+ // verify cookie value is retored from localStorage
369+ assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , 'anon12' ) ;
370+ } ) ;
371+
372+ it ( 'should write to both cookie and localStorage when generating a new anonymousId' , function ( ) {
373+ var user = new User ( ) ;
374+ var anonId = user . anonymousId ( ) ;
375+ assert . notEqual ( anonId , null ) ;
376+ assert . equal ( cookie . get ( 'ajs_anonymous_id' ) , anonId ) ;
377+ assert . equal ( store . get ( 'ajs_anonymous_id' ) , anonId ) ;
378+ } ) ;
322379 } ) ;
323380 } ) ;
324381
@@ -400,6 +457,12 @@ describe('user', function() {
400457 assert ( cookie . get ( cookieKey ) === 'id' ) ;
401458 } ) ;
402459
460+ it ( 'should save an id to localStorage' , function ( ) {
461+ user . id ( 'id' ) ;
462+ user . save ( ) ;
463+ assert . equal ( store . get ( cookieKey ) , 'id' ) ;
464+ } ) ;
465+
403466 it ( 'should save traits to local storage' , function ( ) {
404467 user . traits ( { trait : true } ) ;
405468 user . save ( ) ;
@@ -425,14 +488,21 @@ describe('user', function() {
425488 assert ( user . traits ( ) , { } ) ;
426489 } ) ;
427490
428- it ( 'should clear a cookie' , function ( ) {
491+ it ( 'should clear id in cookie' , function ( ) {
429492 user . id ( 'id' ) ;
430493 user . save ( ) ;
431494 user . logout ( ) ;
432495 assert ( cookie . get ( cookieKey ) === null ) ;
433496 } ) ;
434497
435- it ( 'should clear local storage' , function ( ) {
498+ it ( 'should clear id in local storage' , function ( ) {
499+ user . id ( 'id' ) ;
500+ user . save ( ) ;
501+ user . logout ( ) ;
502+ assert ( store . get ( cookieKey ) === undefined ) ;
503+ } ) ;
504+
505+ it ( 'should clear traits in local storage' , function ( ) {
436506 user . traits ( { trait : true } ) ;
437507 user . save ( ) ;
438508 user . logout ( ) ;
0 commit comments