Create use effect command #419

Open
Vylpes wants to merge 26 commits from feature/380-use-effect into develop
4 changed files with 354 additions and 130 deletions
Showing only changes of commit 3d665aba67 - Show all commits

View file

@ -12,7 +12,7 @@ export default class Effects extends ButtonEvent {
await List(interaction); await List(interaction);
break; break;
case "use": case "use":
await Use(interaction); await Use.Execute(interaction);
break; break;
} }
} }

View file

@ -5,20 +5,21 @@ import EmbedColours from "../../constants/EmbedColours";
import TimeLengthInput from "../../helpers/TimeLengthInput"; import TimeLengthInput from "../../helpers/TimeLengthInput";
import AppLogger from "../../client/appLogger"; import AppLogger from "../../client/appLogger";
export default async function Use(interaction: ButtonInteraction) { export default class Use {
public static async Execute(interaction: ButtonInteraction) {
const subaction = interaction.customId.split(" ")[2]; const subaction = interaction.customId.split(" ")[2];
switch (subaction) { switch (subaction) {
case "confirm": case "confirm":
await UseConfirm(interaction); await this.UseConfirm(interaction);
break; break;
case "cancel": case "cancel":
await UseCancel(interaction); await this.UseCancel(interaction);
break; break;
} }
} }
export async function UseConfirm(interaction: ButtonInteraction) { private static async UseConfirm(interaction: ButtonInteraction) {
const id = interaction.customId.split(" ")[3]; const id = interaction.customId.split(" ")[3];
const effectDetail = EffectDetails.get(id); const effectDetail = EffectDetails.get(id);
@ -78,7 +79,7 @@ export async function UseConfirm(interaction: ButtonInteraction) {
}); });
} }
export async function UseCancel(interaction: ButtonInteraction) { private static async UseCancel(interaction: ButtonInteraction) {
const id = interaction.customId.split(" ")[3]; const id = interaction.customId.split(" ")[3];
const effectDetail = EffectDetails.get(id); const effectDetail = EffectDetails.get(id);
@ -128,3 +129,4 @@ export async function UseCancel(interaction: ButtonInteraction) {
components: [ row ], components: [ row ],
}); });
} }
}

View file

@ -1,21 +1,148 @@
describe("Use", () => { import { ButtonInteraction, EmbedBuilder, InteractionResponse } from "discord.js";
test.todo("GIVEN subaction is confirm, EXPECT UseConfirm to be called"); import Use from "../../../src/buttonEvents/Effects/Use";
import { mock } from "jest-mock-extended";
import AppLogger from "../../../src/client/appLogger";
import EffectHelper from "../../../src/helpers/EffectHelper";
test.todo("GIVEN subaction is cancel, EXPECT UseCancel to be called"); jest.mock("../../../src/client/appLogger");
jest.mock("../../../src/helpers/EffectHelper");
test.todo("GIVEN subaction is unknown, EXPECT nothing to be called"); beforeEach(() => {
jest.resetAllMocks();
jest.useFakeTimers();
jest.setSystemTime(0);
});
afterAll(() => {
jest.useRealTimers();
});
describe("Execute", () => {
test("GIVEN subaction is unknown, EXPECT nothing to be called", async () => {
// Arrange
const interaction = mock<ButtonInteraction>();
interaction.customId = "effects use invalud";
// Act
await Use.Execute(interaction);
// Assert
expect(interaction.reply).not.toHaveBeenCalled();
expect(interaction.update).not.toHaveBeenCalled();
});
}); });
describe("UseConfirm", () => { describe("UseConfirm", () => {
test.todo("GIVEN effectDetail is not found, EXPECT error"); let interaction = mock<ButtonInteraction>();
test.todo("GIVEN EffectHelper.UseEffect failed, EXPECT error"); beforeEach(() => {
interaction = mock<ButtonInteraction>();
interaction.customId = "effects use confirm";
});
test.todo("GIVEN EffectHelper.UseEffect succeeded, EXPECT interaction updated"); test("GIVEN effectDetail is not found, EXPECT error", async () => {
// Arrange
interaction.customId += " invalid";
// Act
await Use.Execute(interaction);
// Assert
expect(AppLogger.LogError).toHaveBeenCalledTimes(1);
expect(AppLogger.LogError).toHaveBeenCalledWith("Button/Effects/Use", "Effect not found, invalid");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Effect not found in system!");
});
test("GIVEN EffectHelper.UseEffect failed, EXPECT error", async () => {
// Arrange
interaction.customId += " unclaimed";
interaction.user.id = "userId";
(EffectHelper.UseEffect as jest.Mock).mockResolvedValue(false);
const whenExpires = new Date(Date.now() + 10 * 60 * 1000);
// Act
await Use.Execute(interaction);
// Assert
expect(EffectHelper.UseEffect).toHaveBeenCalledTimes(1);
expect(EffectHelper.UseEffect).toHaveBeenCalledWith("userId", "unclaimed", whenExpires);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown");
});
test("GIVEN EffectHelper.UseEffect succeeded, EXPECT interaction updated", async () => {
let updatedWith;
// Arrange
interaction.customId += " unclaimed";
interaction.user.id = "userId";
interaction.update.mockImplementation(async (opts: any) => {
updatedWith = opts;
return mock<InteractionResponse<boolean>>();
});
(EffectHelper.UseEffect as jest.Mock).mockResolvedValue(true);
const whenExpires = new Date(Date.now() + 10 * 60 * 1000);
// Act
await Use.Execute(interaction);
// Assert
expect(EffectHelper.UseEffect).toHaveBeenCalledTimes(1);
expect(EffectHelper.UseEffect).toHaveBeenCalledWith("userId", "unclaimed", whenExpires);
expect(interaction.update).toHaveBeenCalledTimes(1);
expect(updatedWith).toMatchSnapshot();
});
}); });
describe("UseCancel", () => { describe("UseCancel", () => {
test.todo("GIVEN effectDetail is not found, EXPECT error"); let interaction = mock<ButtonInteraction>();
test.todo("GIVEN effectDetail is found, EXPECT interaction updated"); beforeEach(() => {
interaction = mock<ButtonInteraction>();
interaction.customId = "effects use cancel";
});
test("GIVEN effectDetail is not found, EXPECT error", async () => {
// Arrange
interaction.customId += " invalid";
// Act
await Use.Execute(interaction);
// Assert
expect(AppLogger.LogError).toHaveBeenCalledTimes(1);
expect(AppLogger.LogError).toHaveBeenCalledWith("Button/Effects/Cancel", "Effect not found, invalid");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Effect not found in system!");
});
test("GIVEN effectDetail is found, EXPECT interaction updated", async () => {
let updatedWith;
// Arrange
interaction.customId += " unclaimed";
interaction.user.id = "userId";
interaction.update.mockImplementation(async (opts: any) => {
updatedWith = opts;
return mock<InteractionResponse<boolean>>();
});
// Act
await Use.Execute(interaction);
// Assert
expect(interaction.update).toHaveBeenCalledTimes(1);
expect(updatedWith).toMatchSnapshot();
});
}); });

View file

@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`UseCancel GIVEN effectDetail is found, EXPECT interaction updated 1`] = `
{
"components": [
{
"components": [
{
"custom_id": "effects use confirm unclaimed",
"disabled": true,
"emoji": undefined,
"label": "Confirm",
"style": 1,
"type": 2,
},
{
"custom_id": "effects use cancel unclaimed",
"disabled": true,
"emoji": undefined,
"label": "Cancel",
"style": 4,
"type": 2,
},
],
"type": 1,
},
],
"embeds": [
{
"color": 13882323,
"description": "The effect from your inventory has not been used",
"fields": [
{
"inline": true,
"name": "Effect",
"value": "Unclaimed Chance Up",
},
{
"inline": true,
"name": "Expires",
"value": "10m",
},
],
"title": "Effect Use Cancelled",
},
],
}
`;
exports[`UseConfirm GIVEN EffectHelper.UseEffect succeeded, EXPECT interaction updated 1`] = `
{
"components": [
{
"components": [
{
"custom_id": "effects use confirm unclaimed",
"disabled": true,
"emoji": undefined,
"label": "Confirm",
"style": 1,
"type": 2,
},
{
"custom_id": "effects use cancel unclaimed",
"disabled": true,
"emoji": undefined,
"label": "Cancel",
"style": 4,
"type": 2,
},
],
"type": 1,
},
],
"embeds": [
{
"color": 2263842,
"description": "You now have an active effect!",
"fields": [
{
"inline": true,
"name": "Effect",
"value": "Unclaimed Chance Up",
},
{
"inline": true,
"name": "Expires",
"value": "<t:600:f>",
},
],
"title": "Effect Used",
},
],
}
`;