Repository: googlemaps/js-api-loader
Pull request #209 (2021-03-16 22:48:55)

refactor: reset logic to testable units

codecov[bot]: # [Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/209?src=pr&el=h1) Report
> Merging [#209](https://codecov.io/gh/googlemaps/js-api-loader/pull/209?src=pr&el=desc) (4e4709e) into [master](https://codecov.io/gh/googlemaps/js-api-loader/commit/cf9206afa08b68b2a54dfab55338dd40dadbed2d?el=desc) (cf9206a) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/googlemaps/js-api-loader/pull/209/graphs/tree.svg?width=650&height=150&src=pr&token=aAmgcWFXMs)](https://codecov.io/gh/googlemaps/js-api-loader/pull/209?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master      #209   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          119       122    +3     
  Branches        24        25    +1     
=========================================
+ Hits           119       122    +3     
```


| [Impacted Files](https://codecov.io/gh/googlemaps/js-api-loader/pull/209?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/index.ts](https://codecov.io/gh/googlemaps/js-api-loader/pull/209/diff?src=pr&el=tree#diff-c3JjL2luZGV4LnRz) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/209?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/209?src=pr&el=footer). Last update [676c082...4e4709e](https://codecov.io/gh/googlemaps/js-api-loader/pull/209?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/index.ts b/src/index.ts
index e1b04dd..acf1ce0 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -315,6 +315,10 @@ export class Loader {
     };
   }
 
+  private get failed(): boolean {
+    return this.done && !this.loading && this.errors.length >= this.retries + 1;
+  }
+
   /**
    * CreateUrl returns the Google Maps JavaScript API script url given the [[LoaderOptions]].
    *
@@ -425,14 +429,20 @@ export class Loader {
     }
   }
 
+  /**
+   * Reset the loader state.
+   */
+  private reset(): void {
+    this.deleteScript();
+    this.done = false;
+    this.loading = false;
+    this.errors = [];
+    this.onerrorEvent = null;
+  }
+
   private resetIfRetryingFailed(): void {
-    const possibleAttempts = this.retries + 1;
-    if (this.done && !this.loading && this.errors.length >= possibleAttempts) {
-      this.deleteScript();
-      this.done = false;
-      this.loading = false;
-      this.errors = [];
-      this.onerrorEvent = null;
+    if (this.failed) {
+      this.reset();
     }
   }
 


Test patch:
diff --git a/src/index.test.ts b/src/index.test.ts
index 679e238..40bdaa8 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -213,25 +213,51 @@ test("script onerror should not reset retry mechanism with parallel loaders", as
   expect(console.log).toHaveBeenCalledTimes(loader.retries);
 });
 
-test("resetIfRetryingFailed should clear state", async () => {
+test("reset should clear state", () => {
   const loader = new Loader({ apiKey: "foo", retries: 0 });
+  const deleteScript = jest.spyOn(loader, "deleteScript");
 
-  // eslint-disable-next-line @typescript-eslint/no-empty-function
-  console.log = jest.fn();
-
-  const rejection1 = expect(loader.load()).rejects.toBeInstanceOf(ErrorEvent);
-  loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
-  jest.runAllTimers();
+  loader["done"] = true;
+  loader["loading"] = false;
+  loader["errors"] = [new ErrorEvent("foo")];
 
-  await Promise.all([rejection1]);
-  expect(loader["done"]).toBeTruthy();
-  expect(loader["loading"]).toBeFalsy();
-  expect(loader["errors"].length).toBe(1);
+  loader["reset"]();
 
-  loader["resetIfRetryingFailed"]();
   expect(loader["done"]).toBeFalsy();
   expect(loader["loading"]).toBeFalsy();
   expect(loader["onerrorEvent"]).toBe(null);
+  expect(deleteScript).toHaveBeenCalledTimes(1);
+});
+
+test("failed getter should be correct", () => {
+  const loader = new Loader({ apiKey: "foo", retries: 0 });
+
+  // initial
+  expect(loader["failed"]).toBeFalsy();
+
+  // not done
+  loader["done"] = false;
+  loader["loading"] = false;
+  loader["errors"] = [new ErrorEvent("foo")];
+  expect(loader["failed"]).toBeFalsy();
+
+  // still loading
+  loader["done"] = false;
+  loader["loading"] = true;
+  loader["errors"] = [new ErrorEvent("foo")];
+  expect(loader["failed"]).toBeFalsy();
+
+  // no errors
+  loader["done"] = true;
+  loader["loading"] = false;
+  loader["errors"] = [];
+  expect(loader["failed"]).toBeFalsy();
+
+  // done with errors
+  loader["done"] = true;
+  loader["loading"] = false;
+  loader["errors"] = [new ErrorEvent("foo")];
+  expect(loader["failed"]).toBeTruthy();
 });
 
 test("loader should not reset retry mechanism if successfully loaded", () => {