Test - Updates ItemBaseClassService tests to inline with recent code changes

This commit is contained in:
Refringe 2024-04-10 23:15:28 -04:00
parent c123c034cf
commit 1cd4b055eb
No known key found for this signature in database
GPG Key ID: 7715B85B4A6306ED
2 changed files with 34 additions and 10 deletions

View File

@ -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

View File

@ -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);
});
});