1- import { GoogleAnalyticsService } from './google-analytics.service' ;
21import { Angulartics2GoogleTagManager } from 'angulartics2' ;
2+ import { of } from 'rxjs' ;
3+
4+ import { GoogleAnalyticsService } from './google-analytics.service' ;
35import { ConfigurationDataService } from '../core/data/configuration-data.service' ;
46import { createFailedRemoteDataObject$ , createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils' ;
57import { ConfigurationProperty } from '../core/shared/configuration-property.model' ;
8+ import { KlaroService } from '../shared/cookies/klaro.service' ;
9+ import { GOOGLE_ANALYTICS_KLARO_KEY } from '../shared/cookies/klaro-configuration' ;
610
711describe ( 'GoogleAnalyticsService' , ( ) => {
812 const trackingIdProp = 'google.analytics.key' ;
@@ -12,6 +16,7 @@ describe('GoogleAnalyticsService', () => {
1216 let service : GoogleAnalyticsService ;
1317 let angularticsSpy : Angulartics2GoogleTagManager ;
1418 let configSpy : ConfigurationDataService ;
19+ let klaroServiceSpy : jasmine . SpyObj < KlaroService > ;
1520 let scriptElementMock : any ;
1621 let srcSpy : any ;
1722 let innerHTMLSpy : any ;
@@ -31,6 +36,10 @@ describe('GoogleAnalyticsService', () => {
3136 'startTracking' ,
3237 ] ) ;
3338
39+ klaroServiceSpy = jasmine . createSpyObj ( 'KlaroService' , {
40+ 'getSavedPreferences' : jasmine . createSpy ( 'getSavedPreferences' )
41+ } ) ;
42+
3443 configSpy = createConfigSuccessSpy ( trackingIdTestValue ) ;
3544
3645 scriptElementMock = {
@@ -53,7 +62,11 @@ describe('GoogleAnalyticsService', () => {
5362 body : bodyElementSpy ,
5463 } ) ;
5564
56- service = new GoogleAnalyticsService ( angularticsSpy , configSpy , documentSpy ) ;
65+ klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( {
66+ GOOGLE_ANALYTICS_KLARO_KEY : true
67+ } ) ) ;
68+
69+ service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
5770 } ) ;
5871
5972 it ( 'should be created' , ( ) => {
@@ -73,7 +86,11 @@ describe('GoogleAnalyticsService', () => {
7386 findByPropertyName : createFailedRemoteDataObject$ ( ) ,
7487 } ) ;
7588
76- service = new GoogleAnalyticsService ( angularticsSpy , configSpy , documentSpy ) ;
89+ klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( {
90+ GOOGLE_ANALYTICS_KLARO_KEY : true
91+ } ) ) ;
92+
93+ service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
7794 } ) ;
7895
7996 it ( 'should NOT add a script to the body' , ( ) => {
@@ -91,7 +108,49 @@ describe('GoogleAnalyticsService', () => {
91108 describe ( 'when the tracking id is empty' , ( ) => {
92109 beforeEach ( ( ) => {
93110 configSpy = createConfigSuccessSpy ( ) ;
94- service = new GoogleAnalyticsService ( angularticsSpy , configSpy , documentSpy ) ;
111+ klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( {
112+ [ GOOGLE_ANALYTICS_KLARO_KEY ] : true
113+ } ) ) ;
114+ service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
115+ } ) ;
116+
117+ it ( 'should NOT add a script to the body' , ( ) => {
118+ service . addTrackingIdToPage ( ) ;
119+ expect ( bodyElementSpy . appendChild ) . toHaveBeenCalledTimes ( 0 ) ;
120+ } ) ;
121+
122+ it ( 'should NOT start tracking' , ( ) => {
123+ service . addTrackingIdToPage ( ) ;
124+ expect ( angularticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
125+ } ) ;
126+ } ) ;
127+
128+ describe ( 'when google-analytics cookie preferences are not existing' , ( ) => {
129+ beforeEach ( ( ) => {
130+ configSpy = createConfigSuccessSpy ( trackingIdTestValue ) ;
131+ klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( { } ) ) ;
132+ service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
133+ } ) ;
134+
135+ it ( 'should NOT add a script to the body' , ( ) => {
136+ service . addTrackingIdToPage ( ) ;
137+ expect ( bodyElementSpy . appendChild ) . toHaveBeenCalledTimes ( 0 ) ;
138+ } ) ;
139+
140+ it ( 'should NOT start tracking' , ( ) => {
141+ service . addTrackingIdToPage ( ) ;
142+ expect ( angularticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
143+ } ) ;
144+ } ) ;
145+
146+
147+ describe ( 'when google-analytics cookie preferences are set to false' , ( ) => {
148+ beforeEach ( ( ) => {
149+ configSpy = createConfigSuccessSpy ( trackingIdTestValue ) ;
150+ klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( {
151+ [ GOOGLE_ANALYTICS_KLARO_KEY ] : false
152+ } ) ) ;
153+ service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
95154 } ) ;
96155
97156 it ( 'should NOT add a script to the body' , ( ) => {
@@ -105,7 +164,16 @@ describe('GoogleAnalyticsService', () => {
105164 } ) ;
106165 } ) ;
107166
108- describe ( 'when the tracking id is non-empty' , ( ) => {
167+ describe ( 'when both google-analytics cookie and the tracking id are non-empty' , ( ) => {
168+
169+ beforeEach ( ( ) => {
170+ configSpy = createConfigSuccessSpy ( trackingIdTestValue ) ;
171+ klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( {
172+ [ GOOGLE_ANALYTICS_KLARO_KEY ] : true
173+ } ) ) ;
174+ service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
175+ } ) ;
176+
109177 it ( 'should create a script tag whose innerHTML contains the tracking id' , ( ) => {
110178 service . addTrackingIdToPage ( ) ;
111179 expect ( documentSpy . createElement ) . toHaveBeenCalledTimes ( 2 ) ;
0 commit comments