@@ -13,6 +13,17 @@ import { mockCookie, restoreCookie, getCookie } from './mock-cookie';
13
13
import { AmplitudeServerZone } from '../src/server-zone.js' ;
14
14
import Request from '../src/xhr' ;
15
15
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
+
16
27
// maintain for testing backwards compatability
17
28
describe ( 'AmplitudeClient' , function ( ) {
18
29
var apiKey = '000000' ;
@@ -888,11 +899,11 @@ describe('AmplitudeClient', function () {
888
899
var onErrorSpy = sinon . spy ( ) ;
889
900
890
901
var amplitude = new AmplitudeClient ( ) ;
891
- sinon . stub ( amplitude . cookieStorage , 'options ' ) . throws ( ) ;
902
+ sinon . stub ( amplitude , '_refreshDynamicConfig ' ) . throws ( ) ;
892
903
amplitude . init ( apiKey , null , { onError : onErrorSpy } ) ;
893
904
assert . isTrue ( onErrorSpy . calledOnce ) ;
894
905
895
- amplitude . cookieStorage . options . restore ( ) ;
906
+ amplitude [ '_refreshDynamicConfig' ] . restore ( ) ;
896
907
} ) ;
897
908
898
909
it ( 'should set observer plan options' , function ( ) {
@@ -2728,6 +2739,44 @@ describe('AmplitudeClient', function () {
2728
2739
} ) ;
2729
2740
} ) ;
2730
2741
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
+
2731
2780
it ( 'should validate event properties' , function ( ) {
2732
2781
var e = new Error ( 'oops' ) ;
2733
2782
clock . tick ( 1 ) ;
0 commit comments