Skip to content

Commit af85dc8

Browse files
authored
fix: move warning and return if already defined (#250)
1 parent c66435c commit af85dc8

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ test("loader should resolve immediately when google.maps defined", async () => {
329329
expect(console.warn).toHaveBeenCalledTimes(1);
330330
});
331331

332+
test("loader should not warn if done and google.maps is defined", async () => {
333+
const loader = new Loader({ apiKey: "foo" });
334+
loader["done"] = true;
335+
window.google = { maps: { version: "3.*.*" } as any };
336+
console.warn = jest.fn();
337+
await expect(loader.loadPromise()).resolves.toBeUndefined();
338+
delete window.google;
339+
expect(console.warn).toHaveBeenCalledTimes(0);
340+
});
341+
332342
test("deleteScript removes script tag from head", () => {
333343
const loader = new Loader({ apiKey: "foo" });
334344
loader["setScript"]();

src/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,18 +482,21 @@ export class Loader {
482482
}
483483

484484
private execute(): void {
485-
if (window.google && window.google.maps && window.google.maps.version) {
486-
console.warn(
487-
"Google Maps already loaded outside @googlemaps/js-api-loader." +
488-
"This may result in undesirable behavior as options and script parameters may not match."
489-
);
490-
this.callback();
491-
}
492-
493485
this.resetIfRetryingFailed();
486+
494487
if (this.done) {
495488
this.callback();
496489
} else {
490+
// short circuit and warn if google.maps is already loaded
491+
if (window.google && window.google.maps && window.google.maps.version) {
492+
console.warn(
493+
"Google Maps already loaded outside @googlemaps/js-api-loader." +
494+
"This may result in undesirable behavior as options and script parameters may not match."
495+
);
496+
this.callback();
497+
return;
498+
}
499+
497500
if (this.loading) {
498501
// do nothing but wait
499502
} else {

0 commit comments

Comments
 (0)