card-drop/tests/buttonEvents/Effects/Use.test.ts
Ethan Lane a3248e978a
All checks were successful
Deploy To Stage / build (push) Successful in 48s
Deploy To Stage / deploy (push) Successful in 17s
Create use effect command (#419)
# 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>
2025-01-25 17:29:01 +00:00

148 lines
No EOL
4.8 KiB
TypeScript

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();
});
});