@@ -307,25 +307,54 @@ describe('Segment.io', function() {
307307 analytics . assert ( ! object . context . amp ) ;
308308 } ) ;
309309
310- it ( 'should set xid if available, is enabled , and context.traits is not defined' , function ( ) {
310+ it ( 'should set xid from cookie , and context.traits is not defined' , function ( ) {
311311 segment . cookie ( 'seg_xid' , 'test_id' ) ;
312312 segment . options . crossDomainIdServers = [
313313 'userdata.example1.com' ,
314314 'xid.domain2.com' ,
315315 'localhost'
316316 ] ;
317+ segment . options . saveCrossDomainIdInLocalStorage = false ;
317318
318319 segment . normalize ( object ) ;
319320 assert . equal ( object . context . traits . crossDomainId , 'test_id' ) ;
320321 } ) ;
321322
322- it ( 'should set xid if available, is enabled , and context.traits is defined' , function ( ) {
323+ it ( 'should set xid from cookie , and context.traits is defined' , function ( ) {
323324 segment . cookie ( 'seg_xid' , 'test_id' ) ;
324325 segment . options . crossDomainIdServers = [
325326 'userdata.example1.com' ,
326327 'xid.domain2.com' ,
327328 'localhost'
328329 ] ;
330+ segment . options . saveCrossDomainIdInLocalStorage = false ;
331+
332+ var msg = { context : { traits : { email : 'prateek@segment.com' } } } ;
333+ segment . normalize ( msg ) ;
334+ assert . equal ( msg . context . traits . crossDomainId , 'test_id' ) ;
335+ } ) ;
336+
337+ it ( 'should set xid from localStorage, and context.traits is not defined' , function ( ) {
338+ window . localStorage . setItem ( 'seg_xid' , 'test_id' ) ;
339+ segment . options . crossDomainIdServers = [
340+ 'userdata.example1.com' ,
341+ 'xid.domain2.com' ,
342+ 'localhost'
343+ ] ;
344+ segment . options . saveCrossDomainIdInLocalStorage = true ;
345+
346+ segment . normalize ( object ) ;
347+ assert . equal ( object . context . traits . crossDomainId , 'test_id' ) ;
348+ } ) ;
349+
350+ it ( 'should set xid from localStorage, is enabled, and context.traits is defined' , function ( ) {
351+ window . localStorage . setItem ( 'seg_xid' , 'test_id' ) ;
352+ segment . options . crossDomainIdServers = [
353+ 'userdata.example1.com' ,
354+ 'xid.domain2.com' ,
355+ 'localhost'
356+ ] ;
357+ segment . options . saveCrossDomainIdInLocalStorage = true ;
329358
330359 var msg = { context : { traits : { email : 'prateek@segment.com' } } } ;
331360 segment . normalize ( msg ) ;
@@ -998,16 +1027,24 @@ describe('Segment.io', function() {
9981027 } ) ;
9991028
10001029 it ( 'should obtain crossDomainId' , function ( ) {
1001- var res = null ;
1002- segment . retrieveCrossDomainId ( function ( err , response ) {
1003- res = response ;
1004- } ) ;
10051030 server . respondWith ( 'GET' , 'https://xid.domain2.com/v1/id/' + segment . options . apiKey , [
10061031 200 ,
10071032 { 'Content-Type' : 'application/json' } ,
10081033 '{ "id": "xdomain-id-1" }'
10091034 ] ) ;
1010- server . respond ( ) ;
1035+ if ( segment . options . saveCrossDomainIdInLocalStorage ) {
1036+ server . respondWith ( 'GET' , 'https://localhost/v1/saveId?writeKey=' + segment . options . apiKey + '&xid=xdomain-id-1' , [
1037+ 200 ,
1038+ { 'Content-Type' : 'text/plan' } ,
1039+ 'OK'
1040+ ] ) ;
1041+ }
1042+ server . respondImmediately = true ;
1043+
1044+ var res = null ;
1045+ segment . retrieveCrossDomainId ( function ( err , response ) {
1046+ res = response ;
1047+ } ) ;
10111048
10121049 var identify = segment . onidentify . args [ 0 ] ;
10131050 analytics . assert ( identify [ 0 ] . traits ( ) . crossDomainId === 'xdomain-id-1' ) ;
@@ -1019,11 +1056,6 @@ describe('Segment.io', function() {
10191056 } ) ;
10201057
10211058 it ( 'should generate crossDomainId if no server has it' , function ( ) {
1022- var res = null ;
1023- segment . retrieveCrossDomainId ( function ( err , response ) {
1024- res = response ;
1025- } ) ;
1026-
10271059 server . respondWith ( 'GET' , 'https://xid.domain2.com/v1/id/' + segment . options . apiKey , [
10281060 200 ,
10291061 { 'Content-Type' : 'application/json' } ,
@@ -1034,7 +1066,19 @@ describe('Segment.io', function() {
10341066 { 'Content-Type' : 'application/json' } ,
10351067 '{ "id": null }'
10361068 ] ) ;
1037- server . respond ( ) ;
1069+ if ( segment . options . saveCrossDomainIdInLocalStorage ) {
1070+ server . respondWith ( 'GET' , / h t t p s : \/ \/ l o c a l h o s t \/ v 1 \/ s a v e I d / , [
1071+ 200 ,
1072+ { 'Content-Type' : 'text/plan' } ,
1073+ 'OK'
1074+ ] ) ;
1075+ }
1076+ server . respondImmediately = true ;
1077+
1078+ var res = null ;
1079+ segment . retrieveCrossDomainId ( function ( err , response ) {
1080+ res = response ;
1081+ } ) ;
10381082
10391083 var identify = segment . onidentify . args [ 0 ] ;
10401084 var crossDomainId = identify [ 0 ] . traits ( ) . crossDomainId ;
@@ -1103,13 +1147,6 @@ describe('Segment.io', function() {
11031147 } ) ;
11041148
11051149 it ( 'should succeed even if one server fails' , function ( ) {
1106- var err = null ;
1107- var res = null ;
1108- segment . retrieveCrossDomainId ( function ( error , response ) {
1109- err = error ;
1110- res = response ;
1111- } ) ;
1112-
11131150 server . respondWith ( 'GET' , 'https://xid.domain2.com/v1/id/' + segment . options . apiKey , [
11141151 500 ,
11151152 { 'Content-Type' : 'application/json' } ,
@@ -1120,7 +1157,21 @@ describe('Segment.io', function() {
11201157 { 'Content-Type' : 'application/json' } ,
11211158 '{ "id": "xidxid" }'
11221159 ] ) ;
1123- server . respond ( ) ;
1160+ if ( segment . options . saveCrossDomainIdInLocalStorage ) {
1161+ server . respondWith ( 'GET' , 'https://localhost/v1/saveId?writeKey=' + segment . options . apiKey + '&xid=xidxid' , [
1162+ 200 ,
1163+ { 'Content-Type' : 'text/plan' } ,
1164+ 'OK'
1165+ ] ) ;
1166+ }
1167+ server . respondImmediately = true ;
1168+
1169+ var err = null ;
1170+ var res = null ;
1171+ segment . retrieveCrossDomainId ( function ( error , response ) {
1172+ err = error ;
1173+ res = response ;
1174+ } ) ;
11241175
11251176 var identify = segment . onidentify . args [ 0 ] ;
11261177 analytics . assert ( identify [ 0 ] . traits ( ) . crossDomainId === 'xidxid' ) ;
0 commit comments