Repository: googlemaps/js-api-loader
Pull request #488 (2022-01-04 21:30:06)

feat: add status getter
Allow access to a `LoaderStatus` enum with `Loader.status`.

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

[![Impacted file tree graph](https://codecov.io/gh/googlemaps/js-api-loader/pull/488/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/488?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps)

```diff
@@            Coverage Diff            @@
##              main      #488   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          123       136   +13     
  Branches        25        28    +3     
=========================================
+ Hits           123       136   +13     
```


| [Impacted Files](https://codecov.io/gh/googlemaps/js-api-loader/pull/488?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/488/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/488?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/488?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=googlemaps). Last update [9945c5c...47a4239](https://codecov.io/gh/googlemaps/js-api-loader/pull/488?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 e8fc2f3..3218518 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -165,6 +165,16 @@ export interface LoaderOptions {
   retries?: number;
 }
 
+/**
+ * The status of the [[Loader]].
+ */
+export enum LoaderStatus {
+  INITIALIZED,
+  LOADING,
+  SUCCESS,
+  FAILURE,
+}
+
 /**
  * [[Loader]] makes it easier to add Google Maps JavaScript API to your application
  * dynamically using
@@ -314,6 +324,19 @@ export class Loader {
     };
   }
 
+  public get status(): LoaderStatus {
+    if (this.errors.length) {
+      return LoaderStatus.FAILURE;
+    }
+    if (this.done) {
+      return LoaderStatus.SUCCESS;
+    }
+    if (this.loading) {
+      return LoaderStatus.LOADING;
+    }
+    return LoaderStatus.INITIALIZED;
+  }
+
   private get failed(): boolean {
     return this.done && !this.loading && this.errors.length >= this.retries + 1;
   }


Test patch:
diff --git a/src/index.test.ts b/src/index.test.ts
index 43fe595..06242cd 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 /* eslint @typescript-eslint/no-explicit-any: 0 */
-import { DEFAULT_ID, Loader, LoaderOptions } from ".";
+import { DEFAULT_ID, Loader, LoaderOptions, LoaderStatus } from ".";
 
 jest.useFakeTimers();
 
@@ -54,6 +54,7 @@ test.each([
 ])("createUrl is correct", (options: LoaderOptions, expected: string) => {
   const loader = new Loader(options);
   expect(loader.createUrl()).toEqual(expected);
+  expect(loader.status).toBe(LoaderStatus.INITIALIZED);
 });
 
 test("uses default id if empty string", () => {
@@ -128,6 +129,7 @@ test("script onerror should reject promise", async () => {
   expect(loader["done"]).toBeTruthy();
   expect(loader["loading"]).toBeFalsy();
   expect(loader["errors"].length).toBe(1);
+  expect(loader.status).toBe(LoaderStatus.FAILURE);
 });
 
 test("script onerror should reject promise with multiple loaders", async () => {
@@ -301,6 +303,7 @@ test("singleton should be used", () => {
 
   loader["done"] = true;
   expect(extraLoader["done"]).toBe(loader["done"]);
+  expect(loader.status).toBe(LoaderStatus.SUCCESS);
 });
 
 test("singleton should throw with different options", () => {
@@ -333,7 +336,7 @@ test("loader should resolve immediately when failed loading", async () => {
 test("loader should wait if already loading", () => {
   const loader = new Loader({ apiKey: "foo", retries: 0 });
   loader["loading"] = true;
-
+  expect(loader.status).toBe(LoaderStatus.LOADING);
   loader.load();
 });