From 34964af78ec628287ba63bd1d661830c0eedd557 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 22 Jan 2025 18:05:49 +0000 Subject: [PATCH] Update tests --- src/buttonEvents/Effects.ts | 3 + .../GenerateButtonInteractionMock.ts | 21 ++++++ tests/buttonEvents/Claim.test.ts | 34 ++-------- tests/buttonEvents/Effects.test.ts | 67 ++++++++++++++++++- tests/commands/effects.test.ts | 27 -------- tests/helpers/EffectHelper.test.ts | 41 ------------ 6 files changed, 92 insertions(+), 101 deletions(-) create mode 100644 tests/__functions__/discord.js/GenerateButtonInteractionMock.ts delete mode 100644 tests/commands/effects.test.ts delete mode 100644 tests/helpers/EffectHelper.test.ts diff --git a/src/buttonEvents/Effects.ts b/src/buttonEvents/Effects.ts index f185009..0f9686b 100644 --- a/src/buttonEvents/Effects.ts +++ b/src/buttonEvents/Effects.ts @@ -2,6 +2,7 @@ import { ButtonInteraction } from "discord.js"; import { ButtonEvent } from "../type/buttonEvent"; import List from "./Effects/List"; import Use from "./Effects/Use"; +import AppLogger from "../client/appLogger"; export default class Effects extends ButtonEvent { public override async execute(interaction: ButtonInteraction) { @@ -14,6 +15,8 @@ export default class Effects extends ButtonEvent { case "use": await Use.Execute(interaction); break; + default: + AppLogger.LogError("Buttons/Effects", `Unknown action, ${action}`); } } } diff --git a/tests/__functions__/discord.js/GenerateButtonInteractionMock.ts b/tests/__functions__/discord.js/GenerateButtonInteractionMock.ts new file mode 100644 index 0000000..a1024ee --- /dev/null +++ b/tests/__functions__/discord.js/GenerateButtonInteractionMock.ts @@ -0,0 +1,21 @@ +import { ButtonInteraction } from "../../__types__/discord.js"; + +export default function GenerateButtonInteractionMock(): ButtonInteraction { + return { + guild: {}, + guildId: "guildId", + channel: { + isSendable: jest.fn().mockReturnValue(true), + send: jest.fn(), + }, + deferUpdate: jest.fn(), + editReply: jest.fn(), + message: { + createdAt: new Date(1000 * 60 * 27), + }, + user: { + id: "userId", + }, + customId: "customId", + }; +} \ No newline at end of file diff --git a/tests/buttonEvents/Claim.test.ts b/tests/buttonEvents/Claim.test.ts index 226cee0..d9e7e6f 100644 --- a/tests/buttonEvents/Claim.test.ts +++ b/tests/buttonEvents/Claim.test.ts @@ -2,6 +2,7 @@ import { ButtonInteraction, TextChannel } from "discord.js"; import Claim from "../../src/buttonEvents/Claim"; import { ButtonInteraction as ButtonInteractionType } from "../__types__/discord.js"; import User from "../../src/database/entities/app/User"; +import GenerateButtonInteractionMock from "../__functions__/discord.js/GenerateButtonInteractionMock"; jest.mock("../../src/client/appLogger"); @@ -11,23 +12,8 @@ beforeEach(() => { jest.useFakeTimers(); jest.setSystemTime(1000 * 60 * 30); - interaction = { - guild: {}, - guildId: "guildId", - channel: { - isSendable: jest.fn().mockReturnValue(true), - send: jest.fn(), - }, - deferUpdate: jest.fn(), - editReply: jest.fn(), - message: { - createdAt: new Date(1000 * 60 * 27), - }, - user: { - id: "userId", - }, - customId: "claim cardNumber claimId droppedBy userId", - }; + interaction = GenerateButtonInteractionMock(); + interaction.customId = "claim cardNumber claimId droppedBy userId"; }); afterAll(() => { @@ -120,16 +106,4 @@ test("GIVEN user.RemoveCurrency fails, EXPECT error", async () => { expect(interaction.channel!.send).toHaveBeenCalledWith("[object Object], Not enough currency! You need 10 currency, you have 5!"); expect(interaction.editReply).not.toHaveBeenCalled(); -}); - -test.todo("GIVEN the card has already been claimed, EXPECT error"); - -test.todo("GIVEN the current drop is the latest AND the current user is NOT the one who dropped it, EXPECT error"); - -test.todo("GIVEN the user already has the card in their inventory, EXPECT the entity quantity to be +1"); - -test.todo("GIVEN user does NOT have the card in their inventory, EXPECT a new entity to be created"); - -test.todo("GIVEN card can not be found in the bot, EXPECT error logged"); - -test.todo("EXPECT message to be edited"); \ No newline at end of file +}); \ No newline at end of file diff --git a/tests/buttonEvents/Effects.test.ts b/tests/buttonEvents/Effects.test.ts index 18ccc31..f1f86be 100644 --- a/tests/buttonEvents/Effects.test.ts +++ b/tests/buttonEvents/Effects.test.ts @@ -1,5 +1,66 @@ -test.todo("GIVEN action is list, EXPECT list function to be called"); +import { ButtonInteraction } from "discord.js"; +import Effects from "../../src/buttonEvents/Effects"; +import GenerateButtonInteractionMock from "../__functions__/discord.js/GenerateButtonInteractionMock"; +import { ButtonInteraction as ButtonInteractionType } from "../__types__/discord.js"; +import List from "../../src/buttonEvents/Effects/List"; +import Use from "../../src/buttonEvents/Effects/Use"; +import AppLogger from "../../src/client/appLogger"; -test.todo("GIVEN action is use, EXPECT use function to be called"); +jest.mock("../../src/client/appLogger"); +jest.mock("../../src/buttonEvents/Effects/List"); +jest.mock("../../src/buttonEvents/Effects/Use"); -test.todo("GIVEN action is unknown, EXPECT nothing to be called"); \ No newline at end of file +let interaction: ButtonInteractionType; + +beforeEach(() => { + jest.resetAllMocks(); + + interaction = GenerateButtonInteractionMock(); + interaction.customId = "effects"; +}); + +test("GIVEN action is list, EXPECT list function to be called", async () => { + // Arrange + interaction.customId = "effects list"; + + // Act + const effects = new Effects(); + await effects.execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(List).toHaveBeenCalledTimes(1); + expect(List).toHaveBeenCalledWith(interaction); + + expect(Use.Execute).not.toHaveBeenCalled(); +}); + +test("GIVEN action is use, EXPECT use function to be called", async () => { + // Arrange + interaction.customId = "effects use"; + + // Act + const effects = new Effects(); + await effects.execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(Use.Execute).toHaveBeenCalledTimes(1); + expect(Use.Execute).toHaveBeenCalledWith(interaction); + + expect(List).not.toHaveBeenCalled(); +}); + +test("GIVEN action is invalid, EXPECT nothing to be called", async () => { + // Arrange + interaction.customId = "effects invalid"; + + // Act + const effects = new Effects(); + await effects.execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(List).not.toHaveBeenCalled(); + expect(Use.Execute).not.toHaveBeenCalled(); + + expect(AppLogger.LogError).toHaveBeenCalledTimes(1); + expect(AppLogger.LogError).toHaveBeenCalledWith("Buttons/Effects", "Unknown action, invalid"); +}); \ No newline at end of file diff --git a/tests/commands/effects.test.ts b/tests/commands/effects.test.ts deleted file mode 100644 index 20c8f35..0000000 --- a/tests/commands/effects.test.ts +++ /dev/null @@ -1,27 +0,0 @@ -describe("constructor", () => { - test.todo("EXPECT CommandBuilder to be defined"); -}); - -describe("execute", () => { - test.todo("GIVEN interaction is NOT a ChatInputCommand, EXPECT nothing to happen"); - - test.todo("GIVEN subcommand is list, EXPECT list function to be called"); - - test.todo("GIVEN subcommand is use, EXPECT use function to be called"); -}); - -describe("List", () => { - test.todo("GIVEN pageOption is null, EXPECT page to default to 0"); - - test.todo("GIVEN pageOption.value is undefined, EXPECT page to default to 0"); - - test.todo("EXPECT interaction to be replied"); -}); - -describe("Use", () => { - test.todo("GIVEN effectDetail is not found, EXPECT error"); - - test.todo("GIVEN user can not use effect, EXPECT error"); - - test.todo("EXPECT interaction to be replied"); -}); \ No newline at end of file diff --git a/tests/helpers/EffectHelper.test.ts b/tests/helpers/EffectHelper.test.ts deleted file mode 100644 index e41c2bc..0000000 --- a/tests/helpers/EffectHelper.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -describe("AddEffectToUserInventory", () => { - test.todo("GIVEN effect is found in database, EXPECT effect unused to be incremented"); - - test.todo("GIVEN effect is NOT found in database, EXPECT new effect to be created"); -}); - -describe("UseEffect", () => { - test.todo("GIVEN user can not use effect, EXPECT false returned"); - - test.todo("GIVEN user has effect, EXPECT entity to be updated AND true returned"); -}); - -describe("CanUseEffect", () => { - test.todo("GIVEN effect is not in database, EXPECT false returned"); - - test.todo("GIVEN user does not have any of the effect unused, EXPECT false returned"); - - test.todo("GIVEN effectDetail can not be found, EXPECT false returned"); - - test.todo("GIVEN effect has NOT passed the cooldown, EXPECT false returned"); - - test.todo("GIVEN effect has passed the cooldown, EXPECT true returned"); - - test.todo("GIVEN effect does not have a WhenExpires date supplied, EXPECT true returned"); -}); - -describe("HasEffect", () => { - test.todo("GIVEN effect is NOT found in database, EXPECT false returned"); - - test.todo("GIVEN effect does NOT have an expiry date, EXPECT false returned"); - - test.todo("GIVEN effect.WhenExpires is in the future, EXPECT false returned"); - - test.todo("GIVEN effect.WhenExpires is in the past, EXPECT true returned"); -}); - -describe("GenerateEffectEmbed", () => { - test.todo("GIVEN user has no effects, EXPECT embed to be generated"); - - test.todo("GIVEN user has some effects, EXPECT embed to be generated"); -}); \ No newline at end of file