Repository: googlemaps/js-api-loader
Pull request #81 (2020-11-01 05:51:43)

feat: abort if google.maps.version defined

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

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

```diff
@@            Coverage Diff            @@
##            master       #81   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           89        92    +3     
  Branches        15        16    +1     
=========================================
+ Hits            89        92    +3     
```


| [Impacted Files](https://codecov.io/gh/googlemaps/js-api-loader/pull/81?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/index.ts](https://codecov.io/gh/googlemaps/js-api-loader/pull/81/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/81?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/81?src=pr&el=footer). Last update [f2d533a...ab22da1](https://codecov.io/gh/googlemaps/js-api-loader/pull/81?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 ef7df05..59f3137 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -398,6 +398,14 @@ export class Loader {
   }
 
   private execute(): void {
+    if (window.google && window.google.maps && window.google.maps.version) {
+      console.warn(
+        "Aborted attempt to load Google Maps JS with @googlemaps/js-api-loader." +
+          "This may result in undesirable behavior as script parameters may not match."
+      );
+      this.callback();
+    }
+
     if (this.done) {
       this.callback();
     } else {


Test patch:
diff --git a/src/index.test.ts b/src/index.test.ts
index 3fcc252..da2b39e 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -155,3 +155,12 @@ test("setScript adds a nonce", () => {
   const script = document.head.childNodes[0] as HTMLScriptElement;
   expect(script.nonce).toBe(nonce);
 });
+
+test("loader should resolve immediately when google.maps defined", async () => {
+  const loader = new Loader({ apiKey: "foo" });
+  window.google = { maps: { version: "3.*.*" } as any };
+  console.warn = jest.fn();
+  await expect(loader.loadPromise()).resolves.toBeUndefined();
+  delete window.google;
+  expect(console.warn).toHaveBeenCalledTimes(1);
+});