Skip to content

Commit

Permalink
Add checks for undefined and null to isRegisteredForObject() (#137)
Browse files Browse the repository at this point in the history
* Trigger.ts:isRegisteredForObject() doesn't handle undefined and null
Fixes #136

* Add changelog comment
  • Loading branch information
neilenns authored Jun 1, 2020
1 parent 2dc386d commit 7a4ed35
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/Trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/handlers/Trigger.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 7a4ed35

Please sign in to comment.