Skip to content

Commit af162f9

Browse files
authored
Add test for failed createInstallation in separate request (#2485)
1 parent 7250d25 commit af162f9

File tree

1 file changed

+65
-10
lines changed

1 file changed

+65
-10
lines changed

packages/installations/src/helpers/get-installation-entry.test.ts

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('getInstallationEntry', () => {
4444
>;
4545

4646
beforeEach(() => {
47-
clock = useFakeTimers();
47+
clock = useFakeTimers({ now: 1_000_000, shouldAdvanceTime: true });
4848
appConfig = getFakeAppConfig();
4949
createInstallationRequestSpy = stub(
5050
createInstallationRequestModule,
@@ -259,10 +259,6 @@ describe('getInstallationEntry', () => {
259259
});
260260

261261
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-
266262
// FID generation fails.
267263
generateInstallationEntrySpy.returns(generateFidModule.INVALID_FID);
268264

@@ -331,7 +327,7 @@ describe('getInstallationEntry', () => {
331327
});
332328

333329
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.
335331

336332
const { installationEntry } = await getInstallationEntry(appConfig);
337333

@@ -343,8 +339,67 @@ describe('getInstallationEntry', () => {
343339
expect(createInstallationRequestSpy).not.to.be.called;
344340
});
345341

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.
348403

349404
const { installationEntry } = await getInstallationEntry(appConfig);
350405

@@ -356,9 +411,9 @@ describe('getInstallationEntry', () => {
356411
expect(createInstallationRequestSpy).to.be.calledOnce;
357412
});
358413

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 () => {
360415
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.
362417

363418
const { installationEntry } = await getInstallationEntry(appConfig);
364419

0 commit comments

Comments
 (0)