import { inject , TestBed , waitForAsync } from '@angular/core/testing' ; import { GoogleTagManagerService } from './angular-google-tag-manager.service' ; describe ( 'GoogleTagManagerService' , ( ) => { const testObject = { testKey : 'testValue' } ; beforeEach ( waitForAsync ( ( ) => { TestBed . configureTestingModule ( { providers : [ GoogleTagManagerService , { provide : 'googleTagManagerId' , useValue : 'TEST_GTM_ID' } ] } ) ; window . dataLayer = [ ] ; } ) ) ; it ( 'should be created' , inject ( [ GoogleTagManagerService ] , ( service ) => { expect ( service ) . toBeTruthy ( ) ; } ) ) ; it ( 'should provide a dataLayer taken from the global window variable' , inject ( [ GoogleTagManagerService ] , ( service ) => { expect ( service . getDataLayer ( ) ) . toEqual ( [ ] ) ; window . dataLayer = [ testObject ] ; expect ( service . getDataLayer ( ) ) . toEqual ( [ testObject ] ) ; } ) ) ; it ( 'should init the GTM dataLayer on first push item' , inject ( [ GoogleTagManagerService ] , ( service ) => { return service . pushTag ( testObject ) . then ( ( ) => { expect ( window . dataLayer [ 0 ] . event ) . toEqual ( 'gtm.js' ) ; } ) ; } ) ) ; it ( 'should be push objects in the dataLayer' , inject ( [ GoogleTagManagerService ] , ( service ) => { return service . pushTag ( testObject ) . then ( ( ) => { expect ( window . dataLayer [ 1 ] ) . toEqual ( testObject ) ; expect ( window . dataLayer [ 2 ] ) . toBeFalsy ( ) ; service . pushTag ( testObject ) ; expect ( window . dataLayer [ 1 ] ) . toEqual ( testObject ) ; expect ( window . dataLayer [ 3 ] ) . toEqual ( testObject ) ; } ) ; } ) ) ; it ( 'should be able to initialize the dom with an iframe and a script element' , inject ( [ GoogleTagManagerService ] , ( service ) => { service . pushTag ( testObject ) . then ( ( ) => { const script = document . querySelector ( '#GTMscript' ) ; expect ( script ) . toBeTruthy ( ) ; expect ( script . getAttribute ( 'src' ) ) . toContain ( 'https://www.googletagmanager.com/gtm.js?id=' ) ; } ) ; } ) ) ; it ( 'should be able to initialize the dataLayer with some defaults values of a page' , inject ( [ GoogleTagManagerService ] , ( service ) => { return service . pushTag ( testObject ) . then ( ( ) => { expect ( window . dataLayer [ 0 ] [ 'gtm.start' ] ) . toBeTruthy ( ) ; expect ( window . dataLayer [ 0 ] . event ) . toEqual ( 'gtm.js' ) ; } ) ; } ) ) ; } ) ;