@@ -35,6 +35,7 @@ import { findGtagScriptOnPage } from './src/helpers';
35
35
import { removeGtagScript } from './testing/gtag-script-util' ;
36
36
import { Deferred } from '@firebase/util' ;
37
37
import { AnalyticsError } from './src/errors' ;
38
+ import { FirebaseInstallations } from '@firebase/installations-types' ;
38
39
39
40
let analyticsInstance : FirebaseAnalytics = { } as FirebaseAnalytics ;
40
41
const fakeMeasurementId = 'abcd-efgh' ;
@@ -104,51 +105,6 @@ describe('FirebaseAnalytics instance tests', () => {
104
105
) ;
105
106
warnStub . restore ( ) ;
106
107
} ) ;
107
- it ( 'Warns if cookies are not enabled' , ( ) => {
108
- const warnStub = stub ( console , 'warn' ) ;
109
- const cookieStub = stub ( navigator , 'cookieEnabled' ) . value ( false ) ;
110
- const app = getFakeApp ( {
111
- appId : fakeAppParams . appId ,
112
- apiKey : fakeAppParams . apiKey
113
- } ) ;
114
- const installations = getFakeInstallations ( ) ;
115
- analyticsFactory ( app , installations ) ;
116
- expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include (
117
- AnalyticsError . COOKIES_NOT_ENABLED
118
- ) ;
119
- warnStub . restore ( ) ;
120
- cookieStub . restore ( ) ;
121
- } ) ;
122
- it ( 'Warns if browser extension environment' , ( ) => {
123
- const warnStub = stub ( console , 'warn' ) ;
124
- window . chrome = { runtime : { id : 'blah' } } ;
125
- const app = getFakeApp ( {
126
- appId : fakeAppParams . appId ,
127
- apiKey : fakeAppParams . apiKey
128
- } ) ;
129
- const installations = getFakeInstallations ( ) ;
130
- analyticsFactory ( app , installations ) ;
131
- expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include (
132
- AnalyticsError . INVALID_ANALYTICS_CONTEXT
133
- ) ;
134
- warnStub . restore ( ) ;
135
- window . chrome = undefined ;
136
- } ) ;
137
- it ( 'Warns if indexedDB does not exist' , ( ) => {
138
- const warnStub = stub ( console , 'warn' ) ;
139
- const idbStub = stub ( window , 'indexedDB' ) . value ( undefined ) ;
140
- const app = getFakeApp ( {
141
- appId : fakeAppParams . appId ,
142
- apiKey : fakeAppParams . apiKey
143
- } ) ;
144
- const installations = getFakeInstallations ( ) ;
145
- analyticsFactory ( app , installations ) ;
146
- expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include (
147
- AnalyticsError . INDEXED_DB_UNSUPPORTED
148
- ) ;
149
- warnStub . restore ( ) ;
150
- idbStub . restore ( ) ;
151
- } ) ;
152
108
it ( 'Throws if creating an instance with already-used appId' , ( ) => {
153
109
const app = getFakeApp ( fakeAppParams ) ;
154
110
const installations = getFakeInstallations ( ) ;
@@ -230,45 +186,98 @@ describe('FirebaseAnalytics instance tests', () => {
230
186
} ) ;
231
187
} ) ;
232
188
233
- describe ( 'Standard app, indexedDB.open not available ' , ( ) => {
189
+ describe ( 'Standard app, mismatched environment ' , ( ) => {
234
190
let app : FirebaseApp = { } as FirebaseApp ;
235
- let fidDeferred : Deferred < void > ;
191
+ let installations : FirebaseInstallations = { } as FirebaseInstallations ;
236
192
const gtagStub : SinonStub = stub ( ) ;
193
+ let fidDeferred : Deferred < void > ;
237
194
let warnStub : SinonStub ;
238
- before ( ( ) => {
195
+ let cookieStub : SinonStub ;
196
+ beforeEach ( ( ) => {
239
197
clock = useFakeTimers ( ) ;
240
198
resetGlobalVars ( ) ;
241
199
app = getFakeApp ( fakeAppParams ) ;
242
200
fidDeferred = new Deferred < void > ( ) ;
243
- const installations = getFakeInstallations ( 'fid-1234' , ( ) =>
201
+ installations = getFakeInstallations ( 'fid-1234' , ( ) =>
244
202
fidDeferred . resolve ( )
245
203
) ;
246
204
window [ 'gtag' ] = gtagStub ;
247
205
window [ 'dataLayer' ] = [ ] ;
248
206
stubFetch ( 200 , { measurementId : fakeMeasurementId } ) ;
249
207
warnStub = stub ( console , 'warn' ) ;
250
- idbOpenStub = stub ( indexedDB , 'open' ) . throws ( 'idb open error' ) ;
251
- analyticsInstance = analyticsFactory ( app , installations ) ;
208
+ stubIdbOpen ( ) ;
252
209
} ) ;
253
- after ( ( ) => {
210
+ afterEach ( ( ) => {
254
211
delete window [ 'gtag' ] ;
255
212
delete window [ 'dataLayer' ] ;
256
- removeGtagScript ( ) ;
257
213
fetchStub . restore ( ) ;
258
214
clock . restore ( ) ;
259
- idbOpenStub . restore ( ) ;
260
215
warnStub . restore ( ) ;
216
+ idbOpenStub . restore ( ) ;
261
217
} ) ;
262
- it ( 'Does not call gtag on logEvent but does not throw' , async ( ) => {
218
+ it ( 'Warns on logEvent if cookies not available' , async ( ) => {
219
+ cookieStub = stub ( navigator , 'cookieEnabled' ) . value ( false ) ;
220
+ analyticsInstance = analyticsFactory ( app , installations ) ;
263
221
analyticsInstance . logEvent ( EventName . ADD_PAYMENT_INFO , {
264
222
currency : 'USD'
265
223
} ) ;
224
+ // Successfully resolves fake IDB open request.
225
+ fakeRequest . onsuccess ( ) ;
266
226
// Clear promise chain started by logEvent.
267
227
await clock . runAllAsync ( ) ;
268
228
expect ( gtagStub ) . to . not . have . been . called ;
269
229
expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include (
270
- AnalyticsError . INVALID_INDEXED_DB_CONTEXT
230
+ AnalyticsError . INVALID_ANALYTICS_CONTEXT
231
+ ) ;
232
+ expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include ( 'Cookies' ) ;
233
+ cookieStub . restore ( ) ;
234
+ } ) ;
235
+ it ( 'Warns on logEvent if in browser extension' , async ( ) => {
236
+ window . chrome = { runtime : { id : 'blah' } } ;
237
+ analyticsInstance = analyticsFactory ( app , installations ) ;
238
+ analyticsInstance . logEvent ( EventName . ADD_PAYMENT_INFO , {
239
+ currency : 'USD'
240
+ } ) ;
241
+ // Successfully resolves fake IDB open request.
242
+ fakeRequest . onsuccess ( ) ;
243
+ // Clear promise chain started by logEvent.
244
+ await clock . runAllAsync ( ) ;
245
+ expect ( gtagStub ) . to . not . have . been . called ;
246
+ expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include (
247
+ AnalyticsError . INVALID_ANALYTICS_CONTEXT
248
+ ) ;
249
+ expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include ( 'browser extension' ) ;
250
+ window . chrome = undefined ;
251
+ } ) ;
252
+ it ( 'Warns on logEvent if indexedDB API not available' , async ( ) => {
253
+ const idbStub = stub ( window , 'indexedDB' ) . value ( undefined ) ;
254
+ analyticsInstance = analyticsFactory ( app , installations ) ;
255
+ analyticsInstance . logEvent ( EventName . ADD_PAYMENT_INFO , {
256
+ currency : 'USD'
257
+ } ) ;
258
+ // Clear promise chain started by logEvent.
259
+ await clock . runAllAsync ( ) ;
260
+ expect ( gtagStub ) . to . not . have . been . called ;
261
+ expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include (
262
+ AnalyticsError . INVALID_ANALYTICS_CONTEXT
263
+ ) ;
264
+ expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include ( 'IndexedDB is not available' ) ;
265
+ idbStub . restore ( ) ;
266
+ } ) ;
267
+ it ( 'Warns on logEvent if indexedDB.open() not allowed' , async ( ) => {
268
+ idbOpenStub . restore ( ) ;
269
+ idbOpenStub = stub ( indexedDB , 'open' ) . throws ( 'idb open error test' ) ;
270
+ analyticsInstance = analyticsFactory ( app , installations ) ;
271
+ analyticsInstance . logEvent ( EventName . ADD_PAYMENT_INFO , {
272
+ currency : 'USD'
273
+ } ) ;
274
+ // Clear promise chain started by logEvent.
275
+ await clock . runAllAsync ( ) ;
276
+ expect ( gtagStub ) . to . not . have . been . called ;
277
+ expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include (
278
+ AnalyticsError . INVALID_ANALYTICS_CONTEXT
271
279
) ;
280
+ expect ( warnStub . args [ 0 ] [ 1 ] ) . to . include ( 'idb open error test' ) ;
272
281
} ) ;
273
282
} ) ;
274
283
0 commit comments