Repository: googlemaps/js-api-loader
Pull request #207 (2021-03-16 19:14:16)

fix: clear onerror
The `onerrorEvent` wasn't being cleared internally. In some cases using the private interface in certain failure modes, this would prevent successful loading of the API.

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

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

```diff
@@            Coverage Diff            @@
##            master      #207   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          118       119    +1     
  Branches        24        24           
=========================================
+ Hits           118       119    +1     
```


| [Impacted Files](https://codecov.io/gh/googlemaps/js-api-loader/pull/207?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/index.ts](https://codecov.io/gh/googlemaps/js-api-loader/pull/207/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/207?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/207?src=pr&el=footer). Last update [6f428c6...da58c51](https://codecov.io/gh/googlemaps/js-api-loader/pull/207?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 5d562ba..e1b04dd 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -432,6 +432,7 @@ export class Loader {
       this.done = false;
       this.loading = false;
       this.errors = [];
+      this.onerrorEvent = null;
     }
   }
 


Test patch:
diff --git a/src/index.test.ts b/src/index.test.ts
index be58366..679e238 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -213,6 +213,27 @@ test("script onerror should not reset retry mechanism with parallel loaders", as
   expect(console.log).toHaveBeenCalledTimes(loader.retries);
 });
 
+test("resetIfRetryingFailed should clear state", async () => {
+  const loader = new Loader({ apiKey: "foo", retries: 0 });
+
+  // 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();
+
+  await Promise.all([rejection1]);
+  expect(loader["done"]).toBeTruthy();
+  expect(loader["loading"]).toBeFalsy();
+  expect(loader["errors"].length).toBe(1);
+
+  loader["resetIfRetryingFailed"]();
+  expect(loader["done"]).toBeFalsy();
+  expect(loader["loading"]).toBeFalsy();
+  expect(loader["onerrorEvent"]).toBe(null);
+});
+
 test("loader should not reset retry mechanism if successfully loaded", () => {
   const loader = new Loader({ apiKey: "foo", retries: 0 });
   const deleteScript = jest.spyOn(loader, "deleteScript");