Repository: googlemaps/js-api-loader
Pull request #250 (2021-04-28 17:05:01)

fix: move warning and return if already defined

codecov[bot]: # [Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/250?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps) Report
> Merging [#250](https://codecov.io/gh/googlemaps/js-api-loader/pull/250?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps) (f949982) into [master](https://codecov.io/gh/googlemaps/js-api-loader/commit/c66435cc1f788efa610cdee01ece5c653c36dd35?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps) (c66435c) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/googlemaps/js-api-loader/pull/250/graphs/tree.svg?width=650&height=150&src=pr&token=aAmgcWFXMs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps)](https://codecov.io/gh/googlemaps/js-api-loader/pull/250?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps)

```diff
@@            Coverage Diff            @@
##            master      #250   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          122       123    +1     
  Branches        25        25           
=========================================
+ Hits           122       123    +1     
```


| [Impacted Files](https://codecov.io/gh/googlemaps/js-api-loader/pull/250?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps) | Coverage Δ | |
|---|---|---|
| [src/index.ts](https://codecov.io/gh/googlemaps/js-api-loader/pull/250/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps#diff-c3JjL2luZGV4LnRz) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/250?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/250?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps). Last update [c66435c...f949982](https://codecov.io/gh/googlemaps/js-api-loader/pull/250?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps).


Patch:
diff --git a/src/index.ts b/src/index.ts
index acf1ce0..e9d229f 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -482,18 +482,21 @@ export class Loader {
   }
 
   private execute(): void {
-    if (window.google && window.google.maps && window.google.maps.version) {
-      console.warn(
-        "Google Maps already loaded outside @googlemaps/js-api-loader." +
-          "This may result in undesirable behavior as options and script parameters may not match."
-      );
-      this.callback();
-    }
-
     this.resetIfRetryingFailed();
+
     if (this.done) {
       this.callback();
     } else {
+      // short circuit and warn if google.maps is already loaded
+      if (window.google && window.google.maps && window.google.maps.version) {
+        console.warn(
+          "Google Maps already loaded outside @googlemaps/js-api-loader." +
+            "This may result in undesirable behavior as options and script parameters may not match."
+        );
+        this.callback();
+        return;
+      }
+
       if (this.loading) {
         // do nothing but wait
       } else {


Test patch:
diff --git a/src/index.test.ts b/src/index.test.ts
index 40bdaa8..0b41602 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -329,6 +329,16 @@ test("loader should resolve immediately when google.maps defined", async () => {
   expect(console.warn).toHaveBeenCalledTimes(1);
 });
 
+test("loader should not warn if done and google.maps is defined", async () => {
+  const loader = new Loader({ apiKey: "foo" });
+  loader["done"] = true;
+  window.google = { maps: { version: "3.*.*" } as any };
+  console.warn = jest.fn();
+  await expect(loader.loadPromise()).resolves.toBeUndefined();
+  delete window.google;
+  expect(console.warn).toHaveBeenCalledTimes(0);
+});
+
 test("deleteScript removes script tag from head", () => {
   const loader = new Loader({ apiKey: "foo" });
   loader["setScript"]();