Small ItemHelper.getRandomCompatibleCaliberTemplateId()
Improvements
- Updated to safely access a deeply nested property. - Updated to safely access the item `_id` and `_name` properties in the warning log. - Updated to use the `RandomUtil.getArrayValue()` method to select a random ammo item template ID. - Finished up some tests.
This commit is contained in:
parent
ea7d6117ea
commit
0ad85b45e1
@ -912,17 +912,17 @@ class ItemHelper
|
|||||||
* @param item Db item template to look up Cartridge filter values from
|
* @param item Db item template to look up Cartridge filter values from
|
||||||
* @returns Caliber of cartridge
|
* @returns Caliber of cartridge
|
||||||
*/
|
*/
|
||||||
public getRandomCompatibleCaliberTemplateId(item: ITemplateItem): string
|
public getRandomCompatibleCaliberTemplateId(item: ITemplateItem): string | null
|
||||||
{
|
{
|
||||||
const cartridges = item._props.Cartridges[0]._props.filters[0].Filter;
|
const cartridges = item?._props?.Cartridges[0]?._props?.filters[0]?.Filter;
|
||||||
|
|
||||||
if (!cartridges)
|
if (!cartridges)
|
||||||
{
|
{
|
||||||
this.logger.warning(`no cartridges found for item: ${item._id} ${item._name}`);
|
this.logger.warning(`Failed to find cartridge for item: ${item?._id} ${item?._name}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cartridges[Math.floor(Math.random() * item._props.Cartridges[0]._props.filters[0].Filter.length)];
|
return this.randomUtil.getArrayValue(cartridges);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1076,31 +1076,39 @@ describe("ItemHelper", () =>
|
|||||||
|
|
||||||
describe("getRandomCompatibleCaliberTemplateId", () =>
|
describe("getRandomCompatibleCaliberTemplateId", () =>
|
||||||
{
|
{
|
||||||
it("Should return an item from the passed in items Filter array", () =>
|
it("should return an item from the passed template's cartridge filter array", () =>
|
||||||
{
|
{
|
||||||
const fakeTemplateItem = {
|
const validAmmoItems = [
|
||||||
_props: {
|
"5735ff5c245977640e39ba7e",
|
||||||
Cartridges: [
|
"573601b42459776410737435",
|
||||||
{
|
"573602322459776445391df1",
|
||||||
_props: {
|
"5736026a245977644601dc61",
|
||||||
filters: [
|
"573603562459776430731618",
|
||||||
{
|
"573603c924597764442bd9cb",
|
||||||
Filter: [
|
"5735fdcd2459776445391d61"
|
||||||
"desiredItemTpl"
|
];
|
||||||
]
|
const mockTemplateItem = {
|
||||||
}
|
"_id": "571a29dc2459771fb2755a6a",
|
||||||
]
|
"_name": "mag_tt_toz_std_762x25tt_8",
|
||||||
}
|
"_props": {
|
||||||
|
"Cartridges": [{
|
||||||
|
"_props": {
|
||||||
|
"filters": [{
|
||||||
|
"Filter": validAmmoItems
|
||||||
|
}]
|
||||||
}
|
}
|
||||||
]
|
}]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const result = itemHelper.getRandomCompatibleCaliberTemplateId(fakeTemplateItem as ITemplateItem);
|
|
||||||
|
|
||||||
expect(result).toBe("desiredItemTpl");
|
vi.spyOn((itemHelper as any).randomUtil, "getArrayValue").mockReturnValue(validAmmoItems[0]);
|
||||||
|
|
||||||
|
const result = itemHelper.getRandomCompatibleCaliberTemplateId(mockTemplateItem as ITemplateItem);
|
||||||
|
|
||||||
|
expect(validAmmoItems).toContain(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should return null when item passed in has empty cartridges data", () =>
|
it("should return null when passed template has empty cartridge property", () =>
|
||||||
{
|
{
|
||||||
const fakeTemplateItem = {
|
const fakeTemplateItem = {
|
||||||
_props: {
|
_props: {
|
||||||
@ -1114,11 +1122,20 @@ describe("ItemHelper", () =>
|
|||||||
expect(result).toBe(null);
|
expect(result).toBe(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should return null when undefined passed in", () =>
|
it("should return null when undefined passed in", () =>
|
||||||
{
|
{
|
||||||
const result = itemHelper.getRandomCompatibleCaliberTemplateId(undefined as ITemplateItem);
|
const result = itemHelper.getRandomCompatibleCaliberTemplateId(undefined as ITemplateItem);
|
||||||
|
|
||||||
expect(result).toBe(null);
|
expect(result).toBe(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should log a warning when the template cartridge can not be found", () =>
|
||||||
|
{
|
||||||
|
const mockLoggerWarning = vi.spyOn((itemHelper as any).logger, "warning");
|
||||||
|
|
||||||
|
itemHelper.getRandomCompatibleCaliberTemplateId(undefined as ITemplateItem);
|
||||||
|
|
||||||
|
expect(mockLoggerWarning).toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user