@@ -29,6 +29,7 @@ import {
29
29
import { GtagCommand } from './constants' ;
30
30
import { Deferred } from '@firebase/util' ;
31
31
import { ConsentSettings } from './public-types' ;
32
+ import { removeGtagScripts } from '../testing/gtag-script-util' ;
32
33
33
34
const fakeMeasurementId = 'abcd-efgh-ijkl' ;
34
35
const fakeAppId = 'my-test-app-1234' ;
@@ -46,6 +47,10 @@ const fakeDynamicConfig: DynamicConfig = {
46
47
const fakeDynamicConfigPromises = [ Promise . resolve ( fakeDynamicConfig ) ] ;
47
48
48
49
describe ( 'Gtag wrapping functions' , ( ) => {
50
+ afterEach ( ( ) => {
51
+ removeGtagScripts ( ) ;
52
+ } ) ;
53
+
49
54
it ( 'getOrCreateDataLayer is able to create a new data layer if none exists' , ( ) => {
50
55
delete window [ 'dataLayer' ] ;
51
56
expect ( getOrCreateDataLayer ( 'dataLayer' ) ) . to . deep . equal ( [ ] ) ;
@@ -57,14 +62,24 @@ describe('Gtag wrapping functions', () => {
57
62
} ) ;
58
63
59
64
it ( 'insertScriptIfNeeded inserts script tag' , ( ) => {
60
- expect ( findGtagScriptOnPage ( ) ) . to . be . null ;
61
- insertScriptTag ( 'customDataLayerName' , fakeMeasurementId ) ;
62
- const scriptTag = findGtagScriptOnPage ( ) ;
65
+ const customDataLayerName = 'customDataLayerName' ;
66
+ expect ( findGtagScriptOnPage ( customDataLayerName ) ) . to . be . null ;
67
+ insertScriptTag ( customDataLayerName , fakeMeasurementId ) ;
68
+ const scriptTag = findGtagScriptOnPage ( customDataLayerName ) ;
63
69
expect ( scriptTag ) . to . not . be . null ;
64
70
expect ( scriptTag ! . src ) . to . contain ( `l=customDataLayerName` ) ;
65
71
expect ( scriptTag ! . src ) . to . contain ( `id=${ fakeMeasurementId } ` ) ;
66
72
} ) ;
67
73
74
+ // The test above essentially already touches this functionality but it is still valuable
75
+ it ( 'findGtagScriptOnPage returns gtag instance with matching data layer name' , ( ) => {
76
+ const defaultDataLayerName = 'dataLayer' ;
77
+ insertScriptTag ( defaultDataLayerName , fakeMeasurementId ) ;
78
+ const scriptTag = findGtagScriptOnPage ( defaultDataLayerName ) ;
79
+ expect ( scriptTag ! . src ) . to . contain ( `l=${ defaultDataLayerName } ` ) ;
80
+ expect ( findGtagScriptOnPage ( 'NON_EXISTENT_DATA_LAYER_ID' ) ) . to . be . null ;
81
+ } ) ;
82
+
68
83
describe ( 'wrapOrCreateGtag() when user has not previously inserted a gtag script tag on this page' , ( ) => {
69
84
afterEach ( ( ) => {
70
85
delete window [ 'gtag' ] ;
0 commit comments