@@ -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 { removeGtagScript } 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,11 @@ const fakeDynamicConfig: DynamicConfig = {
46
47
const fakeDynamicConfigPromises = [ Promise . resolve ( fakeDynamicConfig ) ] ;
47
48
48
49
describe ( 'Gtag wrapping functions' , ( ) => {
50
+ afterEach ( ( ) => {
51
+ delete window [ 'gtag' ] ;
52
+ removeGtagScript ( ) ;
53
+ } ) ;
54
+
49
55
it ( 'getOrCreateDataLayer is able to create a new data layer if none exists' , ( ) => {
50
56
delete window [ 'dataLayer' ] ;
51
57
expect ( getOrCreateDataLayer ( 'dataLayer' ) ) . to . deep . equal ( [ ] ) ;
@@ -66,6 +72,15 @@ describe('Gtag wrapping functions', () => {
66
72
expect ( scriptTag ! . src ) . to . contain ( `id=${ fakeMeasurementId } ` ) ;
67
73
} ) ;
68
74
75
+ // The test above essentially already touches this functionality but it is still valuable
76
+ it ( 'findGtagScriptOnPage returns gtag instance with matching data layer name' , ( ) => {
77
+ const defaultDataLayerName = 'dataLayer' ;
78
+ insertScriptTag ( defaultDataLayerName , fakeMeasurementId ) ;
79
+ const scriptTag = findGtagScriptOnPage ( defaultDataLayerName ) ;
80
+ expect ( scriptTag ! . src ) . to . contain ( `l=${ defaultDataLayerName } ` ) ;
81
+ expect ( findGtagScriptOnPage ( 'NON_EXISTENT_DATA_LAYER_ID' ) ) . to . be . null ;
82
+ } ) ;
83
+
69
84
describe ( 'wrapOrCreateGtag() when user has not previously inserted a gtag script tag on this page' , ( ) => {
70
85
afterEach ( ( ) => {
71
86
delete window [ 'gtag' ] ;
0 commit comments