Skip to content

Commit

Permalink
Fix promises in Analytics tests (#2673)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 authored Mar 2, 2020
1 parent f24ddc4 commit fcbd8a9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 85 deletions.
23 changes: 16 additions & 7 deletions packages/analytics/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import './testing/setup';
import {
settings as analyticsSettings,
factory as analyticsFactory,
resetGlobalVars
resetGlobalVars,
getGlobalVars
} from './index';
import {
getFakeApp,
Expand All @@ -32,6 +33,7 @@ import { FirebaseApp } from '@firebase/app-types';
import { GtagCommand, EventName } from './src/constants';
import { findGtagScriptOnPage } from './src/helpers';
import { removeGtagScript } from './testing/gtag-script-util';
import { Deferred } from '@firebase/util';

let analyticsInstance: FirebaseAnalytics = {} as FirebaseAnalytics;
const analyticsId = 'abcd-efgh';
Expand All @@ -57,10 +59,12 @@ describe('FirebaseAnalytics instance tests', () => {
});
describe('Standard app, page already has user gtag script', () => {
let app: FirebaseApp = {} as FirebaseApp;
let fidDeferred: Deferred<void>;
before(() => {
resetGlobalVars();
app = getFakeApp(analyticsId);
const installations = getFakeInstallations();
fidDeferred = new Deferred<void>();
const installations = getFakeInstallations('fid-1234', () => fidDeferred.resolve());

window['gtag'] = gtagStub;
window['dataLayer'] = [];
Expand All @@ -82,7 +86,7 @@ describe('FirebaseAnalytics instance tests', () => {
currency: 'USD'
});
// Clear event stack of async FID call.
await Promise.resolve();
await fidDeferred.promise;
expect(gtagStub).to.have.been.calledWith('js');
expect(gtagStub).to.have.been.calledWith(
GtagCommand.CONFIG,
Expand All @@ -94,7 +98,9 @@ describe('FirebaseAnalytics instance tests', () => {
}
);
// Clear event stack of initialization promise.
await Promise.resolve();
const { initializedIdPromisesMap } = getGlobalVars();
await Promise.all(Object.values(initializedIdPromisesMap));
// await Promise.resolve().then(() => {});
expect(gtagStub).to.have.been.calledWith(
GtagCommand.EVENT,
EventName.ADD_PAYMENT_INFO,
Expand All @@ -121,10 +127,12 @@ describe('FirebaseAnalytics instance tests', () => {
});

describe('Page has user gtag script with custom gtag and dataLayer names', () => {
let fidDeferred: Deferred<void>;
before(() => {
resetGlobalVars();
const app = getFakeApp(analyticsId);
const installations = getFakeInstallations();
fidDeferred = new Deferred<void>();
const installations = getFakeInstallations('fid-1234', () => fidDeferred.resolve());
window[customGtagName] = gtagStub;
window[customDataLayerName] = [];
analyticsSettings({
Expand All @@ -146,7 +154,7 @@ describe('FirebaseAnalytics instance tests', () => {
currency: 'USD'
});
// Clear event stack of async FID call.
await Promise.resolve();
await fidDeferred.promise;
expect(gtagStub).to.have.been.calledWith('js');
expect(gtagStub).to.have.been.calledWith(
GtagCommand.CONFIG,
Expand All @@ -158,7 +166,8 @@ describe('FirebaseAnalytics instance tests', () => {
}
);
// Clear event stack of initialization promise.
await Promise.resolve();
const { initializedIdPromisesMap } = getGlobalVars();
await Promise.all(Object.values(initializedIdPromisesMap));
expect(gtagStub).to.have.been.calledWith(
GtagCommand.EVENT,
EventName.ADD_PAYMENT_INFO,
Expand Down
4 changes: 2 additions & 2 deletions packages/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import '@firebase/installations';
import { FirebaseAnalytics } from '@firebase/analytics-types';
import { FirebaseAnalyticsInternal } from '@firebase/analytics-interop-types';
import { _FirebaseNamespace } from '@firebase/app-types/private';
import { factory, settings, resetGlobalVars } from './src/factory';
import { factory, settings, resetGlobalVars, getGlobalVars } from './src/factory';
import { EventName } from './src/constants';
import {
Component,
Expand Down Expand Up @@ -82,7 +82,7 @@ export function registerAnalytics(instance: _FirebaseNamespace): void {
}
}

export { factory, settings, resetGlobalVars };
export { factory, settings, resetGlobalVars, getGlobalVars };

registerAnalytics(firebase as _FirebaseNamespace);

Expand Down
9 changes: 9 additions & 0 deletions packages/analytics/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ export function resetGlobalVars(
gtagName = 'gtag';
}

/**
* For testing
*/
export function getGlobalVars(): { initializedIdPromisesMap: { [gaId: string]: Promise<void> }} {
return {
initializedIdPromisesMap
};
}

/**
* This must be run before calling firebase.analytics() or it won't
* have any effect.
Expand Down
Loading

0 comments on commit fcbd8a9

Please sign in to comment.