|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 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 | +import { expect, use } from 'chai'; |
| 18 | +import { AnalyticsService } from './service'; |
| 19 | +import { firebase } from '@firebase/app-compat'; |
| 20 | +import { FirebaseApp } from '@firebase/app-types'; |
| 21 | +import * as analyticsExp from '@firebase/analytics-exp'; |
| 22 | +import { stub, match, SinonStub } from 'sinon'; |
| 23 | +import * as sinonChai from 'sinon-chai'; |
| 24 | + |
| 25 | +use(sinonChai); |
| 26 | + |
| 27 | +function createTestService(app: FirebaseApp): AnalyticsService { |
| 28 | + return new AnalyticsService(app, analyticsExp.getAnalytics(app)); |
| 29 | +} |
| 30 | + |
| 31 | +describe('Firebase Analytics > Service', () => { |
| 32 | + let app: FirebaseApp; |
| 33 | + let service: AnalyticsService; |
| 34 | + let logEventStub: SinonStub = stub(); |
| 35 | + let setUserIdStub: SinonStub = stub(); |
| 36 | + let setCurrentScreenStub: SinonStub = stub(); |
| 37 | + let setUserPropertiesStub: SinonStub = stub(); |
| 38 | + let setAnalyticsCollectionEnabledStub: SinonStub = stub(); |
| 39 | + |
| 40 | + before(() => { |
| 41 | + logEventStub = stub(analyticsExp, 'logEvent'); |
| 42 | + setUserIdStub = stub(analyticsExp, 'setUserId'); |
| 43 | + setCurrentScreenStub = stub(analyticsExp, 'setCurrentScreen'); |
| 44 | + setUserPropertiesStub = stub(analyticsExp, 'setUserProperties'); |
| 45 | + setAnalyticsCollectionEnabledStub = stub( |
| 46 | + analyticsExp, |
| 47 | + 'setAnalyticsCollectionEnabled' |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + app = firebase.initializeApp({ |
| 53 | + apiKey: '456_LETTERS_AND_1234NUMBERS', |
| 54 | + appId: '123lettersand:numbers', |
| 55 | + projectId: 'my-project', |
| 56 | + messagingSenderId: 'messaging-sender-id' |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(async () => { |
| 61 | + await app.delete(); |
| 62 | + }); |
| 63 | + |
| 64 | + after(() => { |
| 65 | + logEventStub.restore(); |
| 66 | + setUserIdStub.restore(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('logEvent() calls modular logEvent() with only event name', () => { |
| 70 | + service = createTestService(app); |
| 71 | + service.logEvent('begin_checkout'); |
| 72 | + expect(logEventStub).to.be.calledWith(match.any, 'begin_checkout'); |
| 73 | + logEventStub.resetHistory(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('logEvent() calls modular logEvent() with 2 args', () => { |
| 77 | + service = createTestService(app); |
| 78 | + service.logEvent('begin_checkout', { 'currency': 'USD' }); |
| 79 | + expect(logEventStub).to.be.calledWith(match.any, 'begin_checkout', { |
| 80 | + 'currency': 'USD' |
| 81 | + }); |
| 82 | + logEventStub.resetHistory(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('logEvent() calls modular logEvent() with all args', () => { |
| 86 | + service = createTestService(app); |
| 87 | + service.logEvent('begin_checkout', { 'currency': 'USD' }, { global: true }); |
| 88 | + expect(logEventStub).to.be.calledWith( |
| 89 | + match.any, |
| 90 | + 'begin_checkout', |
| 91 | + { 'currency': 'USD' }, |
| 92 | + { global: true } |
| 93 | + ); |
| 94 | + logEventStub.resetHistory(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('setUserId() calls modular setUserId()', () => { |
| 98 | + service = createTestService(app); |
| 99 | + service.setUserId('user123'); |
| 100 | + expect(setUserIdStub).to.be.calledWith(match.any, 'user123'); |
| 101 | + setUserIdStub.resetHistory(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('setUserId() calls modular setUserId() with options if provided', () => { |
| 105 | + service = createTestService(app); |
| 106 | + service.setUserId('user123', { global: true }); |
| 107 | + expect(setUserIdStub).to.be.calledWith(match.any, 'user123', { |
| 108 | + global: true |
| 109 | + }); |
| 110 | + setUserIdStub.resetHistory(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('setCurrentScreen() calls modular setCurrentScreen()', () => { |
| 114 | + service = createTestService(app); |
| 115 | + service.setCurrentScreen('some_screen'); |
| 116 | + expect(setCurrentScreenStub).to.be.calledWith(match.any, 'some_screen'); |
| 117 | + setCurrentScreenStub.resetHistory(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('setCurrentScreen() calls modular setCurrentScreen() with options if provided', () => { |
| 121 | + service = createTestService(app); |
| 122 | + service.setCurrentScreen('some_screen', { global: true }); |
| 123 | + expect(setCurrentScreenStub).to.be.calledWith(match.any, 'some_screen', { |
| 124 | + global: true |
| 125 | + }); |
| 126 | + setCurrentScreenStub.resetHistory(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('setUserProperties() calls modular setUserProperties()', () => { |
| 130 | + service = createTestService(app); |
| 131 | + service.setUserProperties({ 'my_custom_property': 'abc' }); |
| 132 | + expect(setUserPropertiesStub).to.be.calledWith(match.any, { |
| 133 | + 'my_custom_property': 'abc' |
| 134 | + }); |
| 135 | + setUserPropertiesStub.resetHistory(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('setUserProperties() calls modular setUserProperties() with options if provided', () => { |
| 139 | + service = createTestService(app); |
| 140 | + service.setUserProperties( |
| 141 | + { 'my_custom_property': 'abc' }, |
| 142 | + { global: true } |
| 143 | + ); |
| 144 | + expect(setUserPropertiesStub).to.be.calledWith( |
| 145 | + match.any, |
| 146 | + { 'my_custom_property': 'abc' }, |
| 147 | + { |
| 148 | + global: true |
| 149 | + } |
| 150 | + ); |
| 151 | + setCurrentScreenStub.resetHistory(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('setAnalyticsCollectionEnabled() calls modular setAnalyticsCollectionEnabled()', () => { |
| 155 | + service = createTestService(app); |
| 156 | + service.setAnalyticsCollectionEnabled(false); |
| 157 | + expect(setAnalyticsCollectionEnabledStub).to.be.calledWith( |
| 158 | + match.any, |
| 159 | + false |
| 160 | + ); |
| 161 | + setAnalyticsCollectionEnabledStub.resetHistory(); |
| 162 | + }); |
| 163 | +}); |
0 commit comments