@@ -99,38 +99,37 @@ describe('HeartbeatServiceImpl', () => {
99
99
*/
100
100
it ( `triggerHeartbeat() stores a heartbeat` , async ( ) => {
101
101
await heartbeatService . triggerHeartbeat ( ) ;
102
- expect ( heartbeatService . _heartbeatsCache ?. length ) . to . equal ( 1 ) ;
103
- const heartbeat1 = heartbeatService . _heartbeatsCache ?. [ 0 ] ;
102
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats . length ) . to . equal ( 1 ) ;
103
+ const heartbeat1 = heartbeatService . _heartbeatsCache ?. heartbeats [ 0 ] ;
104
104
expect ( heartbeat1 ?. userAgent ) . to . equal ( USER_AGENT_STRING_1 ) ;
105
105
expect ( heartbeat1 ?. date ) . to . equal ( '1970-01-01' ) ;
106
- expect ( writeStub ) . to . be . calledWith ( [ heartbeat1 ] ) ;
106
+ expect ( writeStub ) . to . be . calledWith ( { heartbeats : [ heartbeat1 ] } ) ;
107
107
} ) ;
108
108
it ( `triggerHeartbeat() doesn't store another heartbeat on the same day` , async ( ) => {
109
- expect ( heartbeatService . _heartbeatsCache ?. length ) . to . equal ( 1 ) ;
109
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats . length ) . to . equal ( 1 ) ;
110
110
await heartbeatService . triggerHeartbeat ( ) ;
111
- expect ( heartbeatService . _heartbeatsCache ?. length ) . to . equal ( 1 ) ;
111
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats . length ) . to . equal ( 1 ) ;
112
112
} ) ;
113
113
it ( `triggerHeartbeat() does store another heartbeat on a different day` , async ( ) => {
114
- expect ( heartbeatService . _heartbeatsCache ?. length ) . to . equal ( 1 ) ;
114
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats . length ) . to . equal ( 1 ) ;
115
115
clock . tick ( 24 * 60 * 60 * 1000 ) ;
116
116
await heartbeatService . triggerHeartbeat ( ) ;
117
- expect ( heartbeatService . _heartbeatsCache ?. length ) . to . equal ( 2 ) ;
118
- expect ( heartbeatService . _heartbeatsCache ?. [ 1 ] . date ) . to . equal (
117
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats . length ) . to . equal ( 2 ) ;
118
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats [ 1 ] . date ) . to . equal (
119
119
'1970-01-02'
120
120
) ;
121
121
} ) ;
122
122
it ( `triggerHeartbeat() stores another entry for a different user agent` , async ( ) => {
123
123
userAgentString = USER_AGENT_STRING_2 ;
124
- expect ( heartbeatService . _heartbeatsCache ?. length ) . to . equal ( 2 ) ;
124
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats . length ) . to . equal ( 2 ) ;
125
125
clock . tick ( 2 * 24 * 60 * 60 * 1000 ) ;
126
126
await heartbeatService . triggerHeartbeat ( ) ;
127
- expect ( heartbeatService . _heartbeatsCache ?. length ) . to . equal ( 3 ) ;
128
- expect ( heartbeatService . _heartbeatsCache ?. [ 2 ] . date ) . to . equal (
127
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats . length ) . to . equal ( 3 ) ;
128
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats [ 2 ] . date ) . to . equal (
129
129
'1970-01-03'
130
130
) ;
131
131
} ) ;
132
132
it ( 'getHeartbeatHeaders() gets stored heartbeats and clears heartbeats' , async ( ) => {
133
- const deleteStub = stub ( heartbeatService . _storage , 'deleteAll' ) ;
134
133
const heartbeatHeaders = firebaseUtil . base64Decode (
135
134
await heartbeatService . getHeartbeatsHeader ( )
136
135
) ;
@@ -140,10 +139,13 @@ describe('HeartbeatServiceImpl', () => {
140
139
expect ( heartbeatHeaders ) . to . include ( '1970-01-02' ) ;
141
140
expect ( heartbeatHeaders ) . to . include ( '1970-01-03' ) ;
142
141
expect ( heartbeatHeaders ) . to . include ( `"version":2` ) ;
143
- expect ( heartbeatService . _heartbeatsCache ) . to . equal ( null ) ;
142
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats ) . to . be . empty ;
143
+ expect ( writeStub ) . to . be . calledWith ( {
144
+ lastSentHeartbeatDate : '1970-01-01' ,
145
+ heartbeats : [ ]
146
+ } ) ;
144
147
const emptyHeaders = await heartbeatService . getHeartbeatsHeader ( ) ;
145
148
expect ( emptyHeaders ) . to . equal ( '' ) ;
146
- expect ( deleteStub ) . to . be . called ;
147
149
} ) ;
148
150
} ) ;
149
151
describe ( 'If IndexedDB has entries' , ( ) => {
@@ -198,35 +200,43 @@ describe('HeartbeatServiceImpl', () => {
198
200
it ( `new heartbeat service reads from indexedDB cache` , async ( ) => {
199
201
const promiseResult = await heartbeatService . _heartbeatsCachePromise ;
200
202
if ( isIndexedDBAvailable ( ) ) {
201
- expect ( promiseResult ) . to . deep . equal ( mockIndexedDBHeartbeats ) ;
202
- expect ( heartbeatService . _heartbeatsCache ) . to . deep . equal (
203
- mockIndexedDBHeartbeats
204
- ) ;
203
+ expect ( promiseResult ) . to . deep . equal ( {
204
+ heartbeats : mockIndexedDBHeartbeats
205
+ } ) ;
206
+ expect ( heartbeatService . _heartbeatsCache ) . to . deep . equal ( {
207
+ heartbeats : mockIndexedDBHeartbeats
208
+ } ) ;
205
209
} else {
206
210
// In Node or other no-indexed-db environments it will fail the
207
211
// `canUseIndexedDb` check and return an empty array.
208
- expect ( promiseResult ) . to . deep . equal ( [ ] ) ;
209
- expect ( heartbeatService . _heartbeatsCache ) . to . deep . equal ( [ ] ) ;
212
+ expect ( promiseResult ) . to . deep . equal ( {
213
+ heartbeats : [ ]
214
+ } ) ;
215
+ expect ( heartbeatService . _heartbeatsCache ) . to . deep . equal ( {
216
+ heartbeats : [ ]
217
+ } ) ;
210
218
}
211
219
} ) ;
212
220
it ( `triggerHeartbeat() writes new heartbeats and retains old ones newer than 30 days` , async ( ) => {
213
221
userAgentString = USER_AGENT_STRING_2 ;
214
222
clock . tick ( 3 * 24 * 60 * 60 * 1000 ) ;
215
223
await heartbeatService . triggerHeartbeat ( ) ;
216
224
if ( isIndexedDBAvailable ( ) ) {
217
- expect ( writeStub ) . to . be . calledWith ( [
218
- // The first entry exceeds the 30 day retention limit.
219
- mockIndexedDBHeartbeats [ 1 ] ,
220
- { userAgent : USER_AGENT_STRING_2 , date : '1970-01-04' }
221
- ] ) ;
225
+ expect ( writeStub ) . to . be . calledWith ( {
226
+ heartbeats : [
227
+ // The first entry exceeds the 30 day retention limit.
228
+ mockIndexedDBHeartbeats [ 1 ] ,
229
+ { userAgent : USER_AGENT_STRING_2 , date : '1970-01-04' }
230
+ ]
231
+ } ) ;
222
232
} else {
223
- expect ( writeStub ) . to . be . calledWith ( [
224
- { userAgent : USER_AGENT_STRING_2 , date : '1970-01-04' }
225
- ] ) ;
233
+ expect ( writeStub ) . to . be . calledWith ( {
234
+ heartbeats : [ { userAgent : USER_AGENT_STRING_2 , date : '1970-01-04' } ]
235
+ } ) ;
226
236
}
227
237
} ) ;
228
238
it ( 'getHeartbeatHeaders() gets stored heartbeats and clears heartbeats' , async ( ) => {
229
- const deleteStub = stub ( heartbeatService . _storage , 'deleteAll' ) ;
239
+
230
240
const heartbeatHeaders = firebaseUtil . base64Decode (
231
241
await heartbeatService . getHeartbeatsHeader ( )
232
242
) ;
@@ -237,10 +247,91 @@ describe('HeartbeatServiceImpl', () => {
237
247
expect ( heartbeatHeaders ) . to . include ( USER_AGENT_STRING_2 ) ;
238
248
expect ( heartbeatHeaders ) . to . include ( '1970-01-04' ) ;
239
249
expect ( heartbeatHeaders ) . to . include ( `"version":2` ) ;
240
- expect ( heartbeatService . _heartbeatsCache ) . to . equal ( null ) ;
250
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats ) . to . be . empty ;
251
+ expect ( writeStub ) . to . be . calledWith ( {
252
+ lastSentHeartbeatDate : '1970-01-01' ,
253
+ heartbeats : [ ]
254
+ } ) ;
241
255
const emptyHeaders = await heartbeatService . getHeartbeatsHeader ( ) ;
242
256
expect ( emptyHeaders ) . to . equal ( '' ) ;
243
- expect ( deleteStub ) . to . be . called ;
257
+ } ) ;
258
+ } ) ;
259
+
260
+ describe ( 'If IndexedDB records that a header was sent today' , ( ) => {
261
+ let heartbeatService : HeartbeatServiceImpl ;
262
+ let writeStub : SinonStub ;
263
+ const userAgentString = USER_AGENT_STRING_1 ;
264
+ const mockIndexedDBHeartbeats = [
265
+ // Chosen so one will exceed 30 day limit and one will not.
266
+ {
267
+ userAgent : 'old-user-agent' ,
268
+ date : '1969-12-01'
269
+ } ,
270
+ {
271
+ userAgent : 'old-user-agent' ,
272
+ date : '1969-12-31'
273
+ }
274
+ ] ;
275
+ before ( ( ) => {
276
+ const container = new ComponentContainer ( 'heartbeatTestContainer' ) ;
277
+ container . addComponent (
278
+ new Component (
279
+ 'app' ,
280
+ ( ) =>
281
+ ( {
282
+ options : { appId : 'an-app-id' } ,
283
+ name : 'an-app-name'
284
+ } as FirebaseApp ) ,
285
+ ComponentType . VERSION
286
+ )
287
+ ) ;
288
+ container . addComponent (
289
+ new Component (
290
+ 'platform-logger' ,
291
+ ( ) => ( { getPlatformInfoString : ( ) => userAgentString } ) ,
292
+ ComponentType . VERSION
293
+ )
294
+ ) ;
295
+ stub ( indexedDb , 'readHeartbeatsFromIndexedDB' ) . resolves ( {
296
+ lastSentHeartbeatDate : '1970-01-01' ,
297
+ heartbeats : [ ...mockIndexedDBHeartbeats ]
298
+ } ) ;
299
+ heartbeatService = new HeartbeatServiceImpl ( container ) ;
300
+ } ) ;
301
+ beforeEach ( ( ) => {
302
+ useFakeTimers ( ) ;
303
+ writeStub = stub ( heartbeatService . _storage , 'overwrite' ) ;
304
+ } ) ;
305
+ it ( `new heartbeat service reads from indexedDB cache` , async ( ) => {
306
+ const promiseResult = await heartbeatService . _heartbeatsCachePromise ;
307
+ if ( isIndexedDBAvailable ( ) ) {
308
+ expect ( promiseResult ) . to . deep . equal ( {
309
+ lastSentHeartbeatDate : '1970-01-01' ,
310
+ heartbeats : mockIndexedDBHeartbeats
311
+ } ) ;
312
+ expect ( heartbeatService . _heartbeatsCache ) . to . deep . equal ( {
313
+ lastSentHeartbeatDate : '1970-01-01' ,
314
+ heartbeats : mockIndexedDBHeartbeats
315
+ } ) ;
316
+ } else {
317
+ // In Node or other no-indexed-db environments it will fail the
318
+ // `canUseIndexedDb` check and return an empty array.
319
+ expect ( promiseResult ) . to . deep . equal ( {
320
+ heartbeats : [ ]
321
+ } ) ;
322
+ expect ( heartbeatService . _heartbeatsCache ) . to . deep . equal ( {
323
+ heartbeats : [ ]
324
+ } ) ;
325
+ }
326
+ } ) ;
327
+ it ( `triggerHeartbeat() will skip storing new data` , async ( ) => {
328
+ await heartbeatService . triggerHeartbeat ( ) ;
329
+ expect ( writeStub ) . to . not . be . called ;
330
+ if ( isIndexedDBAvailable ( ) ) {
331
+ expect ( heartbeatService . _heartbeatsCache ?. heartbeats ) . to . deep . equal (
332
+ mockIndexedDBHeartbeats
333
+ ) ;
334
+ }
244
335
} ) ;
245
336
} ) ;
246
337
0 commit comments