@@ -44,14 +44,14 @@ describe('getInstallationEntry', () => {
44
44
> ;
45
45
46
46
beforeEach ( ( ) => {
47
- clock = useFakeTimers ( { now : 1_000_000 , shouldAdvanceTime : true } ) ;
47
+ clock = useFakeTimers ( { now : 1_000_000 } ) ;
48
48
appConfig = getFakeAppConfig ( ) ;
49
49
createInstallationRequestSpy = stub (
50
50
createInstallationRequestModule ,
51
51
'createInstallationRequest'
52
52
) . callsFake (
53
53
async ( _ , installationEntry ) : Promise < RegisteredInstallationEntry > => {
54
- await sleep ( 100 ) ; // Request would take some time
54
+ await sleep ( 500 ) ; // Request would take some time
55
55
const registeredInstallationEntry : RegisteredInstallationEntry = {
56
56
// Returns new FID if client FID is invalid.
57
57
fid : installationEntry . fid || FID ,
@@ -69,6 +69,11 @@ describe('getInstallationEntry', () => {
69
69
) ;
70
70
} ) ;
71
71
72
+ afterEach ( ( ) => {
73
+ // Clean up all pending requests.
74
+ clock . runAll ( ) ;
75
+ } ) ;
76
+
72
77
it ( 'saves the InstallationEntry in the database before returning it' , async ( ) => {
73
78
const oldDbEntry = await get ( appConfig ) ;
74
79
expect ( oldDbEntry ) . to . be . undefined ;
@@ -113,7 +118,7 @@ describe('getInstallationEntry', () => {
113
118
114
119
it ( 'saves the InstallationEntry in the database when registration fails' , async ( ) => {
115
120
createInstallationRequestSpy . callsFake ( async ( ) => {
116
- await sleep ( 100 ) ; // Request would take some time
121
+ await sleep ( 500 ) ; // Request would take some time
117
122
throw ERROR_FACTORY . create ( ErrorCode . REQUEST_FAILED , {
118
123
requestName : 'Create Installation' ,
119
124
serverCode : 500 ,
@@ -143,7 +148,7 @@ describe('getInstallationEntry', () => {
143
148
144
149
it ( 'removes the InstallationEntry from the database when registration fails with 409' , async ( ) => {
145
150
createInstallationRequestSpy . callsFake ( async ( ) => {
146
- await sleep ( 100 ) ; // Request would take some time
151
+ await sleep ( 500 ) ; // Request would take some time
147
152
throw ERROR_FACTORY . create ( ErrorCode . REQUEST_FAILED , {
148
153
requestName : 'Create Installation' ,
149
154
serverCode : 409 ,
@@ -259,6 +264,12 @@ describe('getInstallationEntry', () => {
259
264
} ) ;
260
265
261
266
it ( 'waits for the FID from the server if FID generation fails' , async ( ) => {
267
+ clock . restore ( ) ;
268
+ clock = useFakeTimers ( {
269
+ now : 1_000_000 ,
270
+ shouldAdvanceTime : true /* Needed to allow the createInstallation request to complete. */
271
+ } ) ;
272
+
262
273
// FID generation fails.
263
274
generateInstallationEntrySpy . returns ( generateFidModule . INVALID_FID ) ;
264
275
@@ -340,12 +351,16 @@ describe('getInstallationEntry', () => {
340
351
} ) ;
341
352
342
353
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.
354
+ clock . restore ( ) ;
355
+ clock = useFakeTimers ( {
356
+ now : 1_001_000 /* One second after the request was initiated. */ ,
357
+ shouldAdvanceTime : true /* Needed to allow the createInstallation request to complete. */
358
+ } ) ;
344
359
345
360
const installationEntryPromise = getInstallationEntry ( appConfig ) ;
346
361
347
362
// The pending request fails after a while.
348
- clock . tick ( 500 ) ;
363
+ clock . tick ( 3000 ) ;
349
364
await set ( appConfig , {
350
365
fid : FID ,
351
366
registrationStatus : RequestStatus . NOT_STARTED
@@ -354,13 +369,13 @@ describe('getInstallationEntry', () => {
354
369
const { registrationPromise } = await installationEntryPromise ;
355
370
356
371
// Let the new getInstallationEntry process start.
357
- await sleep ( 50 ) ;
372
+ await sleep ( 250 ) ;
358
373
359
374
const tokenDetails = ( await get (
360
375
appConfig
361
376
) ) as InProgressInstallationEntry ;
362
377
expect ( tokenDetails . registrationTime ) . to . be . at . least (
363
- /* When the first pending request failed. */ 1_001_500
378
+ /* When the first pending request failed. */ 1_004_000
364
379
) ;
365
380
expect ( tokenDetails ) . to . deep . equal ( {
366
381
fid : FID ,
@@ -377,12 +392,16 @@ describe('getInstallationEntry', () => {
377
392
it ( 'updates the InstallationEntry if the request fails and the app is offline' , async ( ) => {
378
393
stub ( navigator , 'onLine' ) . value ( false ) ;
379
394
380
- clock . now = 1_001_000 ; // One second after the request was initiated.
395
+ clock . restore ( ) ;
396
+ clock = useFakeTimers ( {
397
+ now : 1_001_000 /* One second after the request was initiated. */ ,
398
+ shouldAdvanceTime : true /* Needed to allow the createInstallation request to complete. */
399
+ } ) ;
381
400
382
401
const installationEntryPromise = getInstallationEntry ( appConfig ) ;
383
402
384
403
// The pending request fails after a while.
385
- clock . tick ( 500 ) ;
404
+ clock . tick ( 3000 ) ;
386
405
await set ( appConfig , {
387
406
fid : FID ,
388
407
registrationStatus : RequestStatus . NOT_STARTED
@@ -391,7 +410,7 @@ describe('getInstallationEntry', () => {
391
410
const { registrationPromise } = await installationEntryPromise ;
392
411
393
412
// Let the new getInstallationEntry process start.
394
- await sleep ( 50 ) ;
413
+ await sleep ( 250 ) ;
395
414
396
415
expect ( await get ( appConfig ) ) . to . deep . equal ( {
397
416
fid : FID ,
0 commit comments