Create use effect command (#419)
All checks were successful
Deploy To Stage / build (push) Successful in 48s
Deploy To Stage / deploy (push) Successful in 17s

# Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

#380

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

# How Has This Been Tested?

Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration.

# Checklist

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that provde my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream modules

Reviewed-on: #419
Reviewed-by: VylpesTester <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
Ethan Lane 2025-01-25 17:29:01 +00:00 committed by Vylpes
parent 3e81f8ce1d
commit a3248e978a
42 changed files with 1241 additions and 1133 deletions

View file

@ -0,0 +1,50 @@
import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, EmbedBuilder } from "discord.js";
import List from "../../../src/buttonEvents/Effects/List";
import EffectHelper from "../../../src/helpers/EffectHelper";
import { mock } from "jest-mock-extended";
jest.mock("../../../src/helpers/EffectHelper");
let interaction: ReturnType<typeof mock<ButtonInteraction>>;
beforeEach(() => {
jest.resetAllMocks();
(EffectHelper.GenerateEffectEmbed as jest.Mock).mockResolvedValue({
embed: mock<EmbedBuilder>(),
row: mock<ActionRowBuilder<ButtonBuilder>>(),
});
interaction = mock<ButtonInteraction>();
interaction.user.id = "userId";
interaction.customId = "effects list 1";
});
test("GIVEN pageOption is NOT a number, EXPECT error", async () => {
// Arrange
interaction.customId = "effects list invalid";
// Act
await List(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Page option is not a valid number")
expect(EffectHelper.GenerateEffectEmbed).not.toHaveBeenCalled();
expect(interaction.update).not.toHaveBeenCalled();
});
test("GIVEN pageOption is a number, EXPECT interaction updated", async () => {
// Arrange
interaction.customId = "effects list 1";
// Act
await List(interaction);
// Assert
expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledTimes(1);
expect(EffectHelper.GenerateEffectEmbed).toHaveBeenCalledWith("userId", 1);
expect(interaction.update).toHaveBeenCalledTimes(1);
});

View file

@ -0,0 +1,148 @@
import { ButtonInteraction, InteractionResponse, InteractionUpdateOptions, MessagePayload } from "discord.js";
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";
jest.mock("../../../src/client/appLogger");
jest.mock("../../../src/helpers/EffectHelper");
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", () => {
let interaction = mock<ButtonInteraction>();
beforeEach(() => {
interaction = mock<ButtonInteraction>();
interaction.customId = "effects use confirm";
});
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: string | MessagePayload | InteractionUpdateOptions) => {
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", () => {
let interaction = mock<ButtonInteraction>();
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: string | MessagePayload | InteractionUpdateOptions) => {
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",
},
],
}
`;