11import { GoogleAnalyticsService } from './google-analytics.service' ;
2- import { Angulartics2GoogleAnalytics } from 'angulartics2' ;
2+ import { Angulartics2GoogleTagManager } from 'angulartics2' ;
33import { ConfigurationDataService } from '../core/data/configuration-data.service' ;
4- import {
5- createFailedRemoteDataObject$ ,
6- createSuccessfulRemoteDataObject$
7- } from '../shared/remote-data.utils' ;
4+ import { createFailedRemoteDataObject$ , createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils' ;
85import { ConfigurationProperty } from '../core/shared/configuration-property.model' ;
96
107describe ( 'GoogleAnalyticsService' , ( ) => {
118 const trackingIdProp = 'google.analytics.key' ;
129 const trackingIdTestValue = 'mock-tracking-id' ;
1310 const innerHTMLTestValue = 'mock-script-inner-html' ;
11+ const srcTestValue = 'mock-script-src' ;
1412 let service : GoogleAnalyticsService ;
15- let angularticsSpy : Angulartics2GoogleAnalytics ;
13+ let angularticsSpy : Angulartics2GoogleTagManager ;
1614 let configSpy : ConfigurationDataService ;
1715 let scriptElementMock : any ;
16+ let srcSpy : any ;
1817 let innerHTMLSpy : any ;
1918 let bodyElementSpy : HTMLBodyElement ;
2019 let documentSpy : Document ;
@@ -28,18 +27,21 @@ describe('GoogleAnalyticsService', () => {
2827 } ) ;
2928
3029 beforeEach ( ( ) => {
31- angularticsSpy = jasmine . createSpyObj ( 'angulartics2GoogleAnalytics ' , [
30+ angularticsSpy = jasmine . createSpyObj ( 'Angulartics2GoogleTagManager ' , [
3231 'startTracking' ,
3332 ] ) ;
3433
3534 configSpy = createConfigSuccessSpy ( trackingIdTestValue ) ;
3635
3736 scriptElementMock = {
37+ set src ( newVal ) { /* noop */ } ,
38+ get src ( ) { return innerHTMLTestValue ; } ,
3839 set innerHTML ( newVal ) { /* noop */ } ,
39- get innerHTML ( ) { return innerHTMLTestValue ; }
40+ get innerHTML ( ) { return srcTestValue ; }
4041 } ;
4142
4243 innerHTMLSpy = spyOnProperty ( scriptElementMock , 'innerHTML' , 'set' ) ;
44+ srcSpy = spyOnProperty ( scriptElementMock , 'src' , 'set' ) ;
4345
4446 bodyElementSpy = jasmine . createSpyObj ( 'body' , {
4547 appendChild : scriptElementMock ,
@@ -106,19 +108,22 @@ describe('GoogleAnalyticsService', () => {
106108 describe ( 'when the tracking id is non-empty' , ( ) => {
107109 it ( 'should create a script tag whose innerHTML contains the tracking id' , ( ) => {
108110 service . addTrackingIdToPage ( ) ;
109- expect ( documentSpy . createElement ) . toHaveBeenCalledTimes ( 1 ) ;
111+ expect ( documentSpy . createElement ) . toHaveBeenCalledTimes ( 2 ) ;
110112 expect ( documentSpy . createElement ) . toHaveBeenCalledWith ( 'script' ) ;
111113
112114 // sanity check
113115 expect ( documentSpy . createElement ( 'script' ) ) . toBe ( scriptElementMock ) ;
114116
117+ expect ( srcSpy ) . toHaveBeenCalledTimes ( 1 ) ;
118+ expect ( srcSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain ( trackingIdTestValue ) ;
119+
115120 expect ( innerHTMLSpy ) . toHaveBeenCalledTimes ( 1 ) ;
116121 expect ( innerHTMLSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain ( trackingIdTestValue ) ;
117122 } ) ;
118123
119124 it ( 'should add a script to the body' , ( ) => {
120125 service . addTrackingIdToPage ( ) ;
121- expect ( bodyElementSpy . appendChild ) . toHaveBeenCalledTimes ( 1 ) ;
126+ expect ( bodyElementSpy . appendChild ) . toHaveBeenCalledTimes ( 2 ) ;
122127 } ) ;
123128
124129 it ( 'should start tracking' , ( ) => {
0 commit comments