diff --git a/project/src/services/ItemBaseClassService.ts b/project/src/services/ItemBaseClassService.ts index fc72a69f..94259cea 100644 --- a/project/src/services/ItemBaseClassService.ts +++ b/project/src/services/ItemBaseClassService.ts @@ -91,8 +91,9 @@ export class ItemBaseClassService return false; } - // Edge case, the cache is only generated for items with `_type === "Item"`, so return false for any other type - if (this.items[itemTpl]?._type !== "Item") + // The cache is only generated for item templates with `_type === "Item"`, so return false for any other type, + // including item templates that simply don't exist. + if (!this.cachedItemIsOfItemType(itemTpl)) { return false; } @@ -114,6 +115,16 @@ export class ItemBaseClassService return this.itemBaseClassesCache[itemTpl].some((x) => baseClasses.includes(x)); } + /** + * Check if cached item template is of type Item + * @param itemTemplateId item to check + * @returns true if item is of type Item + */ + private cachedItemIsOfItemType(itemTemplateId: string): boolean + { + return this.items[itemTemplateId]?._type === "Item"; + } + /** * Get base classes item inherits from * @param itemTpl item to get base classes for diff --git a/project/tests/services/ItemBaseClassService.test.ts b/project/tests/services/ItemBaseClassService.test.ts index eee0d619..1fedba8d 100644 --- a/project/tests/services/ItemBaseClassService.test.ts +++ b/project/tests/services/ItemBaseClassService.test.ts @@ -42,23 +42,23 @@ describe("ItemBaseClassService", () => it("should return false when the base item type is passed in", () => { // Remove item from base cache - const result = itemBaseClassService.itemHasBaseClass("54009119af1c881c07000029", []); // "Base item" + const result = itemBaseClassService.itemHasBaseClass("54009119af1c881c07000029", []); expect(result).toBe(false); }); - it("should return true when an item is passed in", () => + it("should return true when a med item is passed in with the meds base class", () => { const salewaTpl = "544fb45d4bdc2dee738b4568"; // Remove item from base cache delete itemBaseClassService.itemBaseClassesCache[salewaTpl]; - const result = itemBaseClassService.itemHasBaseClass(salewaTpl, ["543be5664bdc2dd4348b4569"]); // "Meds" type + const result = itemBaseClassService.itemHasBaseClass(salewaTpl, ["543be5664bdc2dd4348b4569"]); expect(result).toBe(true); }); - it("should return true when an item and 2 matching base classes are passed in", () => + it("should return true when an item and two matching base classes are passed in", () => { const salewaTpl = "544fb45d4bdc2dee738b4568"; @@ -67,7 +67,7 @@ describe("ItemBaseClassService", () => const result = itemBaseClassService.itemHasBaseClass(salewaTpl, [ "543be5664bdc2dd4348b4569", "54009119af1c881c07000029", - ]); // "Meds" and "Item" type + ]); // "Meds" and "Item" base classes expect(result).toBe(true); }); @@ -84,15 +84,28 @@ describe("ItemBaseClassService", () => delete itemBaseClassService.itemBaseClassesCache[salewaTpl]; // Perform check - const result = itemBaseClassService.itemHasBaseClass(salewaTpl, ["543be5664bdc2dd4348b4569"]); // "Meds" type + const result = itemBaseClassService.itemHasBaseClass(salewaTpl, ["543be5664bdc2dd4348b4569"]); expect(result).toBe(true); expect(hydrateItemBaseClassCacheSpy).toHaveBeenCalled(); }); - it("should throw an exception when an invalid item is passed in", () => + it("should return false for any item template ID that does not exist", () => { - expect(() => itemBaseClassService.itemHasBaseClass("fakeTpl", ["543be5664bdc2dd4348b4569"])).toThrowError(); + const result = itemBaseClassService.itemHasBaseClass("not-a-valid-template-id", [ + "543be5664bdc2dd4348b4569", + ]); + + expect(result).toBe(false); + }); + + it("should return false for any item template ID without the Item type ", () => + { + const result = itemBaseClassService.itemHasBaseClass("54009119af1c881c07000029", [ + "543be5664bdc2dd4348b4569", + ]); + + expect(result).toBe(false); }); });