Skip to content

Commit fcbd8a9

Browse files
authored
Fix promises in Analytics tests (#2673)
1 parent f24ddc4 commit fcbd8a9

File tree

5 files changed

+88
-85
lines changed

5 files changed

+88
-85
lines changed

packages/analytics/index.test.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import './testing/setup';
2222
import {
2323
settings as analyticsSettings,
2424
factory as analyticsFactory,
25-
resetGlobalVars
25+
resetGlobalVars,
26+
getGlobalVars
2627
} from './index';
2728
import {
2829
getFakeApp,
@@ -32,6 +33,7 @@ import { FirebaseApp } from '@firebase/app-types';
3233
import { GtagCommand, EventName } from './src/constants';
3334
import { findGtagScriptOnPage } from './src/helpers';
3435
import { removeGtagScript } from './testing/gtag-script-util';
36+
import { Deferred } from '@firebase/util';
3537

3638
let analyticsInstance: FirebaseAnalytics = {} as FirebaseAnalytics;
3739
const analyticsId = 'abcd-efgh';
@@ -57,10 +59,12 @@ describe('FirebaseAnalytics instance tests', () => {
5759
});
5860
describe('Standard app, page already has user gtag script', () => {
5961
let app: FirebaseApp = {} as FirebaseApp;
62+
let fidDeferred: Deferred<void>;
6063
before(() => {
6164
resetGlobalVars();
6265
app = getFakeApp(analyticsId);
63-
const installations = getFakeInstallations();
66+
fidDeferred = new Deferred<void>();
67+
const installations = getFakeInstallations('fid-1234', () => fidDeferred.resolve());
6468

6569
window['gtag'] = gtagStub;
6670
window['dataLayer'] = [];
@@ -82,7 +86,7 @@ describe('FirebaseAnalytics instance tests', () => {
8286
currency: 'USD'
8387
});
8488
// Clear event stack of async FID call.
85-
await Promise.resolve();
89+
await fidDeferred.promise;
8690
expect(gtagStub).to.have.been.calledWith('js');
8791
expect(gtagStub).to.have.been.calledWith(
8892
GtagCommand.CONFIG,
@@ -94,7 +98,9 @@ describe('FirebaseAnalytics instance tests', () => {
9498
}
9599
);
96100
// 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(() => {});
98104
expect(gtagStub).to.have.been.calledWith(
99105
GtagCommand.EVENT,
100106
EventName.ADD_PAYMENT_INFO,
@@ -121,10 +127,12 @@ describe('FirebaseAnalytics instance tests', () => {
121127
});
122128

123129
describe('Page has user gtag script with custom gtag and dataLayer names', () => {
130+
let fidDeferred: Deferred<void>;
124131
before(() => {
125132
resetGlobalVars();
126133
const app = getFakeApp(analyticsId);
127-
const installations = getFakeInstallations();
134+
fidDeferred = new Deferred<void>();
135+
const installations = getFakeInstallations('fid-1234', () => fidDeferred.resolve());
128136
window[customGtagName] = gtagStub;
129137
window[customDataLayerName] = [];
130138
analyticsSettings({
@@ -146,7 +154,7 @@ describe('FirebaseAnalytics instance tests', () => {
146154
currency: 'USD'
147155
});
148156
// Clear event stack of async FID call.
149-
await Promise.resolve();
157+
await fidDeferred.promise;
150158
expect(gtagStub).to.have.been.calledWith('js');
151159
expect(gtagStub).to.have.been.calledWith(
152160
GtagCommand.CONFIG,
@@ -158,7 +166,8 @@ describe('FirebaseAnalytics instance tests', () => {
158166
}
159167
);
160168
// Clear event stack of initialization promise.
161-
await Promise.resolve();
169+
const { initializedIdPromisesMap } = getGlobalVars();
170+
await Promise.all(Object.values(initializedIdPromisesMap));
162171
expect(gtagStub).to.have.been.calledWith(
163172
GtagCommand.EVENT,
164173
EventName.ADD_PAYMENT_INFO,

packages/analytics/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import '@firebase/installations';
1919
import { FirebaseAnalytics } from '@firebase/analytics-types';
2020
import { FirebaseAnalyticsInternal } from '@firebase/analytics-interop-types';
2121
import { _FirebaseNamespace } from '@firebase/app-types/private';
22-
import { factory, settings, resetGlobalVars } from './src/factory';
22+
import { factory, settings, resetGlobalVars, getGlobalVars } from './src/factory';
2323
import { EventName } from './src/constants';
2424
import {
2525
Component,
@@ -82,7 +82,7 @@ export function registerAnalytics(instance: _FirebaseNamespace): void {
8282
}
8383
}
8484

85-
export { factory, settings, resetGlobalVars };
85+
export { factory, settings, resetGlobalVars, getGlobalVars };
8686

8787
registerAnalytics(firebase as _FirebaseNamespace);
8888

packages/analytics/src/factory.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ export function resetGlobalVars(
8585
gtagName = 'gtag';
8686
}
8787

88+
/**
89+
* For testing
90+
*/
91+
export function getGlobalVars(): { initializedIdPromisesMap: { [gaId: string]: Promise<void> }} {
92+
return {
93+
initializedIdPromisesMap
94+
};
95+
}
96+
8897
/**
8998
* This must be run before calling firebase.analytics() or it won't
9099
* have any effect.

0 commit comments

Comments
 (0)