Compare commits

..

No commits in common. "1f5a19f10169c4610374a54009a4c395d8bdd9d4" and "f745fdfbca80ec1d00d3bc0ccb6eb71605601a60" have entirely different histories.

2 changed files with 5 additions and 178 deletions

View file

@ -2,8 +2,6 @@ import {ButtonInteraction} from "discord.js";
import AppLogger from "../../client/appLogger";
import EffectHelper from "../../helpers/EffectHelper";
import EmbedColours from "../../constants/EmbedColours";
import User from "../../database/entities/app/User";
import {EffectDetails} from "../../constants/EffectDetails";
export default class Buy {
public static async Execute(interaction: ButtonInteraction) {
@ -29,13 +27,6 @@ export default class Buy {
AppLogger.LogError("Buy Confirm", "Not enough parameters");
return;
}
const effectDetail = EffectDetails.get(id);
if (!effectDetail) {
AppLogger.LogError("Buy Confirm", "Effect detail not found!");
return;
}
const quantityNumber = Number(quantity);
@ -44,25 +35,6 @@ export default class Buy {
return;
}
const totalCost = effectDetail.cost * quantityNumber;
const user = await User.FetchOneById(User, interaction.user.id);
if (!user) {
AppLogger.LogError("Buy Confirm", "Unable to find user");
return;
}
if (user.Currency < totalCost) {
interaction.reply(`You don't have enough currency to buy this! You have \`${user.Currency} Currency\` and need \`${totalCost} Currency\`!`);
return;
}
user.RemoveCurrency(totalCost);
await user.Save(User, user);
await EffectHelper.AddEffectToUserInventory(interaction.user.id, id, quantityNumber);
const generatedEmbed = await EffectHelper.GenerateEffectBuyEmbed(interaction.user.id, id, quantityNumber, true);
if (typeof generatedEmbed == "string") {

View file

@ -4,7 +4,6 @@ import GenerateButtonInteractionMock from "../../__functions__/discord.js/Genera
import { ButtonInteraction as ButtonInteractionType } from "../../__types__/discord.js";
import AppLogger from "../../../src/client/appLogger";
import EffectHelper from "../../../src/helpers/EffectHelper";
import EmbedColours from "../../../src/constants/EmbedColours";
jest.mock("../../../src/client/appLogger");
jest.mock("../../../src/helpers/EffectHelper");
@ -14,11 +13,6 @@ let interaction: ButtonInteractionType;
beforeEach(() => {
jest.resetAllMocks();
jest.spyOn(Buy as any, "Confirm")
.mockRestore();
jest.spyOn(Buy as any, "Cancel")
.mockRestore();
interaction = GenerateButtonInteractionMock();
interaction.customId = "effects buy";
@ -91,8 +85,6 @@ describe("Confirm", () => {
const embed = {
id: "embed",
setColor: jest.fn(),
setFooter: jest.fn(),
};
const row = {
id: "row",
@ -116,154 +108,17 @@ describe("Confirm", () => {
expect(EffectHelper.GenerateEffectBuyEmbed).toHaveBeenCalledTimes(1);
expect(EffectHelper.GenerateEffectBuyEmbed).toHaveBeenCalledWith("userId", "id", 1, true);
expect(embed.setColor).toHaveBeenCalledTimes(1);
expect(embed.setColor).toHaveBeenCalledWith(EmbedColours.Success);
expect(embed.setFooter).toHaveBeenCalledTimes(1);
expect(embed.setFooter).toHaveBeenCalledWith({ text: "Purchased" });
expect(interaction.reply).not.toHaveBeenCalled();
expect(AppLogger.LogError).not.toHaveBeenCalled();
});
test("GIVEN id is not supplied, EXPECT error", async () => {
// Assert
const embed = {
id: "embed",
setColor: jest.fn(),
setFooter: jest.fn(),
};
const row = {
id: "row",
};
(EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue({
embed,
row,
});
test.todo("GIVEN id is not supplied, EXPECT error");
// Act
await Buy.Execute(interaction as unknown as ButtonInteraction);
test.todo("GIVEN quantity is not supplied, EXPECT error");
// Assert
expect(AppLogger.LogError).toHaveBeenCalledTimes(1);
expect(AppLogger.LogError).toHaveBeenCalledWith("Buy Confirm", "Not enough parameters");
test.todo("GIVEN quantity is not a number, EXPECT error");
expect(EffectHelper.GenerateEffectBuyEmbed).not.toHaveBeenCalled();
expect(interaction.reply).not.toHaveBeenCalled();
expect(interaction.update).not.toHaveBeenCalled();
});
test.todo("GIVEN quantity is 0, EXPECT error");
test("GIVEN quantity is not supplied, EXPECT error", async () => {
// Assert
interaction.customId += " id";
const embed = {
id: "embed",
setColor: jest.fn(),
setFooter: jest.fn(),
};
const row = {
id: "row",
};
(EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue({
embed,
row,
});
// Act
await Buy.Execute(interaction as unknown as ButtonInteraction);
// Assert
expect(AppLogger.LogError).toHaveBeenCalledTimes(1);
expect(AppLogger.LogError).toHaveBeenCalledWith("Buy Confirm", "Not enough parameters");
expect(EffectHelper.GenerateEffectBuyEmbed).not.toHaveBeenCalled();
expect(interaction.reply).not.toHaveBeenCalled();
expect(interaction.update).not.toHaveBeenCalled();
});
test("GIVEN quantity is not a number, EXPECT error", async () => {
// Assert
interaction.customId += " id invalid";
const embed = {
id: "embed",
setColor: jest.fn(),
setFooter: jest.fn(),
};
const row = {
id: "row",
};
(EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue({
embed,
row,
});
// Act
await Buy.Execute(interaction as unknown as ButtonInteraction);
// Assert
expect(AppLogger.LogError).toHaveBeenCalledTimes(1);
expect(AppLogger.LogError).toHaveBeenCalledWith("Buy Confirm", "Invalid number");
expect(EffectHelper.GenerateEffectBuyEmbed).not.toHaveBeenCalled();
expect(interaction.reply).not.toHaveBeenCalled();
expect(interaction.update).not.toHaveBeenCalled();
});
test("GIVEN quantity is 0, EXPECT error", async () => {
// Assert
interaction.customId += " id 0";
const embed = {
id: "embed",
setColor: jest.fn(),
setFooter: jest.fn(),
};
const row = {
id: "row",
};
(EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue({
embed,
row,
});
// Act
await Buy.Execute(interaction as unknown as ButtonInteraction);
// Assert
expect(AppLogger.LogError).toHaveBeenCalledTimes(1);
expect(AppLogger.LogError).toHaveBeenCalledWith("Buy Confirm", "Invalid number");
expect(EffectHelper.GenerateEffectBuyEmbed).not.toHaveBeenCalled();
expect(interaction.reply).not.toHaveBeenCalled();
expect(interaction.update).not.toHaveBeenCalled();
});
test.todo("GIVEN user is not found, EXPECT error");
test.todo("GIVEN user does not have enough currency, EXPECT error");
test("GIVEN GenerateEffectBuyEmbed returns with a string, EXPECT error replied", async () => {
// Assert
interaction.customId += " id 1";
(EffectHelper.GenerateEffectBuyEmbed as jest.Mock).mockResolvedValue("Test error");
// Act
await Buy.Execute(interaction as unknown as ButtonInteraction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Test error");
expect(EffectHelper.GenerateEffectBuyEmbed).toHaveBeenCalledTimes(1);
expect(interaction.update).not.toHaveBeenCalled();
expect(AppLogger.LogError).not.toHaveBeenCalled();
});
test.todo("GIVEN GenerateEffectBuyEmbed returns with a string, EXPECT error replied");
});