|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect } from 'chai'; |
| 19 | +import { SinonStub, stub } from 'sinon'; |
| 20 | +import '../testing/setup'; |
| 21 | +import { getFullApp } from '../testing/get-fake-firebase-services'; |
| 22 | +import { getAnalytics, initializeAnalytics } from './api'; |
| 23 | +import { FirebaseApp, deleteApp } from '@firebase/app-exp'; |
| 24 | +import { AnalyticsError } from './errors'; |
| 25 | +import * as init from './initialize-analytics'; |
| 26 | +const fakeAppParams = { appId: 'abcdefgh12345:23405', apiKey: 'AAbbCCdd12345' }; |
| 27 | + |
| 28 | +describe('FirebaseAnalytics API tests', () => { |
| 29 | + let initStub: SinonStub = stub(); |
| 30 | + let app: FirebaseApp; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + initStub = stub(init, '_initializeAnalytics').resolves( |
| 34 | + 'FAKE_MEASUREMENT_ID' |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(async () => { |
| 39 | + await initStub(); |
| 40 | + initStub.restore(); |
| 41 | + if (app) { |
| 42 | + return deleteApp(app); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + after(() => { |
| 47 | + delete window['gtag']; |
| 48 | + delete window['dataLayer']; |
| 49 | + }); |
| 50 | + |
| 51 | + it('initializeAnalytics() with same (no) options returns same instance', () => { |
| 52 | + app = getFullApp(fakeAppParams); |
| 53 | + const analyticsInstance = initializeAnalytics(app); |
| 54 | + const newInstance = initializeAnalytics(app); |
| 55 | + expect(analyticsInstance).to.equal(newInstance); |
| 56 | + }); |
| 57 | + it('initializeAnalytics() with same options returns same instance', () => { |
| 58 | + app = getFullApp(fakeAppParams); |
| 59 | + const analyticsInstance = initializeAnalytics(app, { |
| 60 | + config: { 'send_page_view': false } |
| 61 | + }); |
| 62 | + const newInstance = initializeAnalytics(app, { |
| 63 | + config: { 'send_page_view': false } |
| 64 | + }); |
| 65 | + expect(analyticsInstance).to.equal(newInstance); |
| 66 | + }); |
| 67 | + it('initializeAnalytics() with different options throws', () => { |
| 68 | + app = getFullApp(fakeAppParams); |
| 69 | + initializeAnalytics(app, { |
| 70 | + config: { 'send_page_view': false } |
| 71 | + }); |
| 72 | + expect(() => |
| 73 | + initializeAnalytics(app, { |
| 74 | + config: { 'send_page_view': true } |
| 75 | + }) |
| 76 | + ).to.throw(AnalyticsError.ALREADY_INITIALIZED); |
| 77 | + }); |
| 78 | + it('initializeAnalytics() with different options (one undefined) throws', () => { |
| 79 | + app = getFullApp(fakeAppParams); |
| 80 | + initializeAnalytics(app); |
| 81 | + expect(() => |
| 82 | + initializeAnalytics(app, { |
| 83 | + config: { 'send_page_view': true } |
| 84 | + }) |
| 85 | + ).to.throw(AnalyticsError.ALREADY_INITIALIZED); |
| 86 | + }); |
| 87 | + it('getAnalytics() returns same instance created by previous getAnalytics()', () => { |
| 88 | + app = getFullApp(fakeAppParams); |
| 89 | + const analyticsInstance = getAnalytics(app); |
| 90 | + expect(getAnalytics(app)).to.equal(analyticsInstance); |
| 91 | + }); |
| 92 | + it('getAnalytics() returns same instance created by initializeAnalytics()', () => { |
| 93 | + app = getFullApp(fakeAppParams); |
| 94 | + const analyticsInstance = initializeAnalytics(app); |
| 95 | + expect(getAnalytics(app)).to.equal(analyticsInstance); |
| 96 | + }); |
| 97 | +}); |
0 commit comments