@@ -13,6 +13,17 @@ import { mockCookie, restoreCookie, getCookie } from './mock-cookie';
1313import { AmplitudeServerZone } from '../src/server-zone.js' ;
1414import Request from '../src/xhr' ;
1515
16+ const deleteAllCookies = ( ) =>
17+ document . cookie . split ( ';' ) . forEach ( function ( c ) {
18+ document . cookie = c . replace ( / ^ + / , '' ) . replace ( / = .* / , '=;expires=' + new Date ( ) . toUTCString ( ) + ';path=/' ) ;
19+ } ) ;
20+
21+ const getAllCookies = ( ) =>
22+ document . cookie
23+ . split ( ';' )
24+ . map ( ( c ) => c . trimStart ( ) )
25+ . filter ( ( c ) => ! utils . isEmptyString ( c ) ) ;
26+
1627// maintain for testing backwards compatability
1728describe ( 'AmplitudeClient' , function ( ) {
1829 var apiKey = '000000' ;
@@ -888,11 +899,11 @@ describe('AmplitudeClient', function () {
888899 var onErrorSpy = sinon . spy ( ) ;
889900
890901 var amplitude = new AmplitudeClient ( ) ;
891- sinon . stub ( amplitude . cookieStorage , 'options ' ) . throws ( ) ;
902+ sinon . stub ( amplitude , '_refreshDynamicConfig ' ) . throws ( ) ;
892903 amplitude . init ( apiKey , null , { onError : onErrorSpy } ) ;
893904 assert . isTrue ( onErrorSpy . calledOnce ) ;
894905
895- amplitude . cookieStorage . options . restore ( ) ;
906+ amplitude [ '_refreshDynamicConfig' ] . restore ( ) ;
896907 } ) ;
897908
898909 it ( 'should set observer plan options' , function ( ) {
@@ -2728,6 +2739,44 @@ describe('AmplitudeClient', function () {
27282739 } ) ;
27292740 } ) ;
27302741
2742+ it ( 'should not create any cookies if disabledCookies = true' , function ( ) {
2743+ deleteAllCookies ( ) ;
2744+ clock . tick ( 20 ) ;
2745+
2746+ var cookieArray = getAllCookies ( ) ;
2747+ assert . equal ( cookieArray . length , 0 ) ;
2748+
2749+ var deviceId = 'test_device_id' ;
2750+ var amplitude2 = new AmplitudeClient ( ) ;
2751+
2752+ amplitude2 . init ( apiKey , null , {
2753+ deviceId : deviceId ,
2754+ disableCookies : true ,
2755+ } ) ;
2756+
2757+ cookieArray = getAllCookies ( ) ;
2758+ assert . equal ( cookieArray . length , 0 ) ;
2759+ } ) ;
2760+
2761+ it ( 'should create cookies if disabledCookies = false' , function ( ) {
2762+ deleteAllCookies ( ) ;
2763+ clock . tick ( 20 ) ;
2764+
2765+ var cookieArray = getAllCookies ( ) ;
2766+ assert . equal ( cookieArray . length , 0 ) ;
2767+
2768+ var deviceId = 'test_device_id' ;
2769+ var amplitude2 = new AmplitudeClient ( ) ;
2770+
2771+ amplitude2 . init ( apiKey , null , {
2772+ deviceId : deviceId ,
2773+ disableCookies : false ,
2774+ } ) ;
2775+
2776+ cookieArray = getAllCookies ( ) ;
2777+ assert . equal ( cookieArray . length , 1 ) ;
2778+ } ) ;
2779+
27312780 it ( 'should validate event properties' , function ( ) {
27322781 var e = new Error ( 'oops' ) ;
27332782 clock . tick ( 1 ) ;
0 commit comments