@@ -44,7 +44,7 @@ describe('getInstallationEntry', () => {
44
44
> ;
45
45
46
46
beforeEach ( ( ) => {
47
- clock = useFakeTimers ( ) ;
47
+ clock = useFakeTimers ( { now : 1_000_000 , shouldAdvanceTime : true } ) ;
48
48
appConfig = getFakeAppConfig ( ) ;
49
49
createInstallationRequestSpy = stub (
50
50
createInstallationRequestModule ,
@@ -259,10 +259,6 @@ describe('getInstallationEntry', () => {
259
259
} ) ;
260
260
261
261
it ( 'waits for the FID from the server if FID generation fails' , async ( ) => {
262
- clock . restore ( ) ;
263
- // Needed to allow the createInstallation request to complete.
264
- clock = useFakeTimers ( { shouldAdvanceTime : true } ) ;
265
-
266
262
// FID generation fails.
267
263
generateInstallationEntrySpy . returns ( generateFidModule . INVALID_FID ) ;
268
264
@@ -331,7 +327,7 @@ describe('getInstallationEntry', () => {
331
327
} ) ;
332
328
333
329
it ( "returns the same InstallationEntry if the request hasn't timed out" , async ( ) => {
334
- stub ( Date , 'now' ) . returns ( 1_001_000 ) ; // One second later
330
+ clock . now = 1_001_000 ; // One second after the request was initiated.
335
331
336
332
const { installationEntry } = await getInstallationEntry ( appConfig ) ;
337
333
@@ -343,8 +339,67 @@ describe('getInstallationEntry', () => {
343
339
expect ( createInstallationRequestSpy ) . not . to . be . called ;
344
340
} ) ;
345
341
346
- it ( 'returns a new pending InstallationEntry and triggers createInstallation if the request timed out' , async ( ) => {
347
- stub ( Date , 'now' ) . returns ( 1_015_000 ) ; // Fifteen seconds later
342
+ it ( 'updates the InstallationEntry and triggers createInstallation if the request fails' , async ( ) => {
343
+ clock . now = 1_001_000 ; // One second after the request was initiated.
344
+
345
+ const installationEntryPromise = getInstallationEntry ( appConfig ) ;
346
+
347
+ // The pending request fails after a while.
348
+ clock . tick ( 500 ) ;
349
+ await set ( appConfig , {
350
+ fid : FID ,
351
+ registrationStatus : RequestStatus . NOT_STARTED
352
+ } ) ;
353
+
354
+ const { registrationPromise } = await installationEntryPromise ;
355
+
356
+ // Let the new getInstallationEntry process start.
357
+ await sleep ( 10 ) ;
358
+
359
+ expect ( await get ( appConfig ) ) . to . deep . equal ( {
360
+ fid : FID ,
361
+ registrationStatus : RequestStatus . IN_PROGRESS ,
362
+ registrationTime : 1_001_500 // Started when the first pending request failed.
363
+ } ) ;
364
+
365
+ expect ( registrationPromise ) . to . be . an . instanceOf ( Promise ) ;
366
+ await registrationPromise ;
367
+ expect ( createInstallationRequestSpy ) . to . be . calledOnce ;
368
+ } ) ;
369
+
370
+ it ( 'updates the InstallationEntry if the request fails and the app is offline' , async ( ) => {
371
+ stub ( navigator , 'onLine' ) . value ( false ) ;
372
+
373
+ clock . now = 1_001_000 ; // One second after the request was initiated.
374
+
375
+ const installationEntryPromise = getInstallationEntry ( appConfig ) ;
376
+
377
+ // The pending request fails after a while.
378
+ clock . tick ( 500 ) ;
379
+ await set ( appConfig , {
380
+ fid : FID ,
381
+ registrationStatus : RequestStatus . NOT_STARTED
382
+ } ) ;
383
+
384
+ const { registrationPromise } = await installationEntryPromise ;
385
+
386
+ // Let the new getInstallationEntry process start.
387
+ await sleep ( 10 ) ;
388
+
389
+ expect ( await get ( appConfig ) ) . to . deep . equal ( {
390
+ fid : FID ,
391
+ registrationStatus : RequestStatus . NOT_STARTED
392
+ } ) ;
393
+
394
+ expect ( registrationPromise ) . to . be . an . instanceOf ( Promise ) ;
395
+ await expect ( registrationPromise ) . to . be . rejectedWith (
396
+ 'Application offline'
397
+ ) ;
398
+ expect ( createInstallationRequestSpy ) . not . to . be . called ;
399
+ } ) ;
400
+
401
+ it ( 'returns a new pending InstallationEntry and triggers createInstallation if the request had already timed out' , async ( ) => {
402
+ clock . now = 1_015_000 ; // Fifteen seconds after the request was initiated.
348
403
349
404
const { installationEntry } = await getInstallationEntry ( appConfig ) ;
350
405
@@ -356,9 +411,9 @@ describe('getInstallationEntry', () => {
356
411
expect ( createInstallationRequestSpy ) . to . be . calledOnce ;
357
412
} ) ;
358
413
359
- it ( 'returns a new unregistered InstallationEntry if the request timed out and the app is offline' , async ( ) => {
414
+ it ( 'returns a new unregistered InstallationEntry if the request had already timed out and the app is offline' , async ( ) => {
360
415
stub ( navigator , 'onLine' ) . value ( false ) ;
361
- stub ( Date , 'now' ) . returns ( 1_015_000 ) ; // Fifteen seconds later
416
+ clock . now = 1_015_000 ; // Fifteen seconds after the request was initiated.
362
417
363
418
const { installationEntry } = await getInstallationEntry ( appConfig ) ;
364
419
0 commit comments