Test - PaymentService - Refactored payMoney test

Refactored the payMoney test to work with 3.8. The output is no longer returned, but updated by reference.
This commit is contained in:
Refringe 2024-04-11 20:56:55 -04:00
parent fe7ef06c6c
commit 7d3eaf735a
No known key found for this signature in database
GPG Key ID: 7715B85B4A6306ED

View File

@ -71,48 +71,50 @@ describe("PaymentService", () =>
const itemEventRouterResponse = {
warnings: [],
profileChanges: { sessionID: { _id: sessionID, items: { new: [], change: [], del: [] } } },
profileChanges: { [sessionID]: { _id: sessionID, items: { new: [], change: [], del: [] } } },
} as unknown as IItemEventRouterResponse;
// Mock the logger debug method to return void.
vi.spyOn((paymentService as any).logger, "debug").mockImplementation(() =>
{});
vi.spyOn((paymentService as any).logger, "debug").mockResolvedValue(undefined);
// Mock the trader helper to return a trader with the currency of Roubles.
const traderHelperGetTraderSpy = vi.spyOn((paymentService as any).traderHelper, "getTrader")
.mockReturnValue({ tid: traderId, currency: "RUB" } as unknown as ITraderBase);
const getTraderSpy = vi.spyOn((paymentService as any).traderHelper, "getTrader").mockReturnValue(
{ tid: traderId, currency: "RUB" } as unknown as ITraderBase,
);
// Mock the addPaymentToOutput method to subtract the item cost from the money stack.
const addPaymentToOutputSpy = vi.spyOn(paymentService as any, "addPaymentToOutput").mockImplementation(() =>
const addPaymentToOutputSpy = vi.spyOn(paymentService as any, "addPaymentToOutput").mockImplementation(
(
pmcData: IPmcData,
currencyTpl: string,
amountToPay: number,
sessionIdentifier: string,
output: IItemEventRouterResponse,
) =>
{
moneyItem.upd.StackObjectsCount -= costAmount;
return { warnings: [], profileChanges: { [sessionID]: { items: { change: [moneyItem] } } } };
});
output.profileChanges[sessionIdentifier].items.change.push(moneyItem);
},
);
// Mock the traderHelper lvlUp method to return void.
const traderHelperLvlUpSpy = vi.spyOn((paymentService as any).traderHelper, "lvlUp").mockImplementation(
() =>
{},
);
const lvlUpSpy = vi.spyOn((paymentService as any).traderHelper, "lvlUp").mockResolvedValue(undefined);
const output = paymentService.payMoney(
pmcData,
processBuyTradeRequestData,
sessionID,
itemEventRouterResponse,
);
paymentService.payMoney(pmcData, processBuyTradeRequestData, sessionID, itemEventRouterResponse);
// Check for absence of output warnings.
expect(output.warnings).toHaveLength(0);
expect(itemEventRouterResponse.warnings).toHaveLength(0);
// Check that the currency change was correctly handled.
expect(output.profileChanges[sessionID].items.change).toHaveLength(1);
expect(output.profileChanges[sessionID].items.change[0]._id).toBe(costItemId);
expect(output.profileChanges[sessionID].items.change[0]._tpl).toBe(costItemTpl);
expect(output.profileChanges[sessionID].items.change[0].upd.StackObjectsCount).toBe(costAmount * 3);
expect(itemEventRouterResponse.profileChanges[sessionID].items.change).toHaveLength(1);
expect(itemEventRouterResponse.profileChanges[sessionID].items.change[0]._id).toBe(costItemId);
expect(itemEventRouterResponse.profileChanges[sessionID].items.change[0]._tpl).toBe(costItemTpl);
expect(itemEventRouterResponse.profileChanges[sessionID].items.change[0].upd.StackObjectsCount).toBe(
costAmount * 3,
);
// Check if mocked methods were called as expected.
expect(traderHelperGetTraderSpy).toBeCalledTimes(1);
expect(getTraderSpy).toBeCalledTimes(1);
expect(addPaymentToOutputSpy).toBeCalledWith(
expect.anything(),
costItemTpl,
@ -120,7 +122,7 @@ describe("PaymentService", () =>
sessionID,
expect.anything(),
);
expect(traderHelperLvlUpSpy).toBeCalledTimes(1);
expect(lvlUpSpy).toBeCalledTimes(1);
});
});