From fdbbd9fcf338844ca05cb5131038c486f6849658 Mon Sep 17 00:00:00 2001 From: Louis Christopher Date: Tue, 17 Nov 2020 21:31:31 +0530 Subject: [PATCH 1/2] fix: loader must not reset if successfully loaded --- src/index.test.ts | 18 +++++++++++++++--- src/index.ts | 7 ++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index b0c86254..be583665 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -213,6 +213,18 @@ test("script onerror should not reset retry mechanism with parallel loaders", as expect(console.log).toHaveBeenCalledTimes(loader.retries); }); +test("loader should not reset retry mechanism if successfully loaded", () => { + const loader = new Loader({ apiKey: "foo", retries: 0 }); + const deleteScript = jest.spyOn(loader, "deleteScript"); + + loader["done"] = true; + expect(loader.load()).resolves.toBeUndefined(); + + expect(loader["done"]).toBeTruthy(); + expect(loader["loading"]).toBeFalsy(); + expect(deleteScript).toHaveBeenCalledTimes(0); +}); + test("singleton should be used", () => { const loader = new Loader({ apiKey: "foo" }); const extraLoader = new Loader({ apiKey: "foo" }); @@ -231,7 +243,7 @@ test("singleton should throw with different options", () => { test("loader should resolve immediately when successfully loaded", async () => { // use await/async pattern since the promise resolves without trigger - const loader = new Loader({ apiKey: "foo" }); + const loader = new Loader({ apiKey: "foo", retries: 0 }); loader["done"] = true; await expect(loader.loadPromise()).resolves.toBeUndefined(); @@ -239,7 +251,7 @@ test("loader should resolve immediately when successfully loaded", async () => { test("loader should resolve immediately when failed loading", async () => { // use await/async pattern since the promise rejects without trigger - const loader = new Loader({ apiKey: "foo" }); + const loader = new Loader({ apiKey: "foo", retries: 0 }); loader["done"] = true; loader["onerrorEvent"] = document.createEvent("ErrorEvent"); @@ -247,7 +259,7 @@ test("loader should resolve immediately when failed loading", async () => { }); test("loader should wait if already loading", () => { - const loader = new Loader({ apiKey: "foo" }); + const loader = new Loader({ apiKey: "foo", retries: 0 }); loader["loading"] = true; loader.load(); diff --git a/src/index.ts b/src/index.ts index f8a4ed7b..2ad02a2e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -427,7 +427,12 @@ export class Loader { } private resetIfRetryingFailed(): void { - if (this.done && !this.loading && this.errors.length >= this.retries) { + const totalPossibleAttempts = this.retries + 1; + if ( + this.done && + !this.loading && + this.errors.length >= totalPossibleAttempts + ) { this.deleteScript(); this.done = false; this.loading = false; From ca961bec31c5fe7baa0a1deb50c2db7020b6a46f Mon Sep 17 00:00:00 2001 From: Louis Christopher Date: Tue, 17 Nov 2020 21:40:21 +0530 Subject: [PATCH 2/2] refactor: rename variable --- src/index.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2ad02a2e..0085af71 100644 --- a/src/index.ts +++ b/src/index.ts @@ -427,12 +427,8 @@ export class Loader { } private resetIfRetryingFailed(): void { - const totalPossibleAttempts = this.retries + 1; - if ( - this.done && - !this.loading && - this.errors.length >= totalPossibleAttempts - ) { + const possibleAttempts = this.retries + 1; + if (this.done && !this.loading && this.errors.length >= possibleAttempts) { this.deleteScript(); this.done = false; this.loading = false;