From 7a4ed35332b2c1fe8408c9f6211a7cbdd7793e8c Mon Sep 17 00:00:00 2001 From: danecreekphotography Date: Mon, 1 Jun 2020 05:52:34 -0700 Subject: [PATCH] Add checks for undefined and null to isRegisteredForObject() (#137) * Trigger.ts:isRegisteredForObject() doesn't handle undefined and null Fixes #136 * Add changelog comment --- CHANGELOG.md | 1 + src/Trigger.ts | 6 +++--- tests/handlers/Trigger.test.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 tests/handlers/Trigger.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 4134c14..20b730e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Address a warning during config file validation ([issue 123](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/123)). - Document the available Docker image tags ([issue 128](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/128)). +- Addresses code cleanup [issue 136](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/136). ## Version 1.5.0 diff --git a/src/Trigger.ts b/src/Trigger.ts index c513a17..3a69da7 100644 --- a/src/Trigger.ts +++ b/src/Trigger.ts @@ -210,17 +210,17 @@ export default class Trigger { * @returns True if the trigger is activated by the label */ public isRegisteredForObject(fileName: string, label: string): boolean { - const isRegistered = this.watchObjects.includes(label); + const isRegistered = this.watchObjects?.includes(label); if (!isRegistered) { log.info( `Trigger ${this.name}`, - `${fileName}: Detected object ${label} is not in the watch objects list [${this.watchObjects.join(", ")}]`, + `${fileName}: Detected object ${label} is not in the watch objects list [${this.watchObjects?.join(", ")}]`, ); } else { log.info(`Trigger ${this.name}`, `${fileName}: Matched triggering object ${label}`); } - return isRegistered; + return isRegistered ?? false; } /** diff --git a/tests/handlers/Trigger.test.ts b/tests/handlers/Trigger.test.ts new file mode 100644 index 0000000..c477634 --- /dev/null +++ b/tests/handlers/Trigger.test.ts @@ -0,0 +1,32 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Neil Enns. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +import Trigger from "../../src/Trigger"; + +test("Verify isRegisteredForObject()", () => { + // Empty constructor should default to enabled true + const trigger = new Trigger(); + trigger.name = "Trigger.test.ts"; + + trigger.watchObjects = ["dog"]; + expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(true); + + trigger.watchObjects = []; + expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(false); + + trigger.watchObjects = undefined; + expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(false); + + trigger.watchObjects = null; + expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(false); + + trigger.watchObjects = ["DoG"]; + expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(false); + + trigger.watchObjects = ["dog"]; + expect(trigger.isRegisteredForObject("unit test", "doG")).toBe(false); + + trigger.watchObjects = ["cat", "elephant"]; + expect(trigger.isRegisteredForObject("unit test", "dog")).toBe(false); +});