@@ -22,7 +22,8 @@ import './testing/setup';
22
22
import {
23
23
settings as analyticsSettings ,
24
24
factory as analyticsFactory ,
25
- resetGlobalVars
25
+ resetGlobalVars ,
26
+ getGlobalVars
26
27
} from './index' ;
27
28
import {
28
29
getFakeApp ,
@@ -32,6 +33,7 @@ import { FirebaseApp } from '@firebase/app-types';
32
33
import { GtagCommand , EventName } from './src/constants' ;
33
34
import { findGtagScriptOnPage } from './src/helpers' ;
34
35
import { removeGtagScript } from './testing/gtag-script-util' ;
36
+ import { Deferred } from '@firebase/util' ;
35
37
36
38
let analyticsInstance : FirebaseAnalytics = { } as FirebaseAnalytics ;
37
39
const analyticsId = 'abcd-efgh' ;
@@ -57,10 +59,12 @@ describe('FirebaseAnalytics instance tests', () => {
57
59
} ) ;
58
60
describe ( 'Standard app, page already has user gtag script' , ( ) => {
59
61
let app : FirebaseApp = { } as FirebaseApp ;
62
+ let fidDeferred : Deferred < void > ;
60
63
before ( ( ) => {
61
64
resetGlobalVars ( ) ;
62
65
app = getFakeApp ( analyticsId ) ;
63
- const installations = getFakeInstallations ( ) ;
66
+ fidDeferred = new Deferred < void > ( ) ;
67
+ const installations = getFakeInstallations ( 'fid-1234' , ( ) => fidDeferred . resolve ( ) ) ;
64
68
65
69
window [ 'gtag' ] = gtagStub ;
66
70
window [ 'dataLayer' ] = [ ] ;
@@ -82,7 +86,7 @@ describe('FirebaseAnalytics instance tests', () => {
82
86
currency : 'USD'
83
87
} ) ;
84
88
// Clear event stack of async FID call.
85
- await Promise . resolve ( ) ;
89
+ await fidDeferred . promise ;
86
90
expect ( gtagStub ) . to . have . been . calledWith ( 'js' ) ;
87
91
expect ( gtagStub ) . to . have . been . calledWith (
88
92
GtagCommand . CONFIG ,
@@ -94,7 +98,9 @@ describe('FirebaseAnalytics instance tests', () => {
94
98
}
95
99
) ;
96
100
// Clear event stack of initialization promise.
97
- await Promise . resolve ( ) ;
101
+ const { initializedIdPromisesMap } = getGlobalVars ( ) ;
102
+ await Promise . all ( Object . values ( initializedIdPromisesMap ) ) ;
103
+ // await Promise.resolve().then(() => {});
98
104
expect ( gtagStub ) . to . have . been . calledWith (
99
105
GtagCommand . EVENT ,
100
106
EventName . ADD_PAYMENT_INFO ,
@@ -121,10 +127,12 @@ describe('FirebaseAnalytics instance tests', () => {
121
127
} ) ;
122
128
123
129
describe ( 'Page has user gtag script with custom gtag and dataLayer names' , ( ) => {
130
+ let fidDeferred : Deferred < void > ;
124
131
before ( ( ) => {
125
132
resetGlobalVars ( ) ;
126
133
const app = getFakeApp ( analyticsId ) ;
127
- const installations = getFakeInstallations ( ) ;
134
+ fidDeferred = new Deferred < void > ( ) ;
135
+ const installations = getFakeInstallations ( 'fid-1234' , ( ) => fidDeferred . resolve ( ) ) ;
128
136
window [ customGtagName ] = gtagStub ;
129
137
window [ customDataLayerName ] = [ ] ;
130
138
analyticsSettings ( {
@@ -146,7 +154,7 @@ describe('FirebaseAnalytics instance tests', () => {
146
154
currency : 'USD'
147
155
} ) ;
148
156
// Clear event stack of async FID call.
149
- await Promise . resolve ( ) ;
157
+ await fidDeferred . promise ;
150
158
expect ( gtagStub ) . to . have . been . calledWith ( 'js' ) ;
151
159
expect ( gtagStub ) . to . have . been . calledWith (
152
160
GtagCommand . CONFIG ,
@@ -158,7 +166,8 @@ describe('FirebaseAnalytics instance tests', () => {
158
166
}
159
167
) ;
160
168
// Clear event stack of initialization promise.
161
- await Promise . resolve ( ) ;
169
+ const { initializedIdPromisesMap } = getGlobalVars ( ) ;
170
+ await Promise . all ( Object . values ( initializedIdPromisesMap ) ) ;
162
171
expect ( gtagStub ) . to . have . been . calledWith (
163
172
GtagCommand . EVENT ,
164
173
EventName . ADD_PAYMENT_INFO ,
0 commit comments