From 6e6ad3331ab44f9ae11c644a25e2097710a790da Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 18 Jan 2025 16:49:49 +0000 Subject: [PATCH] WIP: Start of Claim tests --- tests/__types__/discord.js.ts | 17 +++++ tests/buttonEvents/Claim.test.ts | 124 +++++++++++++++++++++++++++++-- 2 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 tests/__types__/discord.js.ts diff --git a/tests/__types__/discord.js.ts b/tests/__types__/discord.js.ts new file mode 100644 index 0000000..22c3291 --- /dev/null +++ b/tests/__types__/discord.js.ts @@ -0,0 +1,17 @@ +export type ButtonInteraction = { + guild: {} | null, + guildId: string | null, + channel: { + isSendable: jest.Func, + send: jest.Func, + } | null, + deferUpdate: jest.Func, + editReply: jest.Func, + message: { + createdAt: Date, + } | null, + user: { + id: string, + } | null, + customId: string, +} \ No newline at end of file diff --git a/tests/buttonEvents/Claim.test.ts b/tests/buttonEvents/Claim.test.ts index beab3c8..226cee0 100644 --- a/tests/buttonEvents/Claim.test.ts +++ b/tests/buttonEvents/Claim.test.ts @@ -1,14 +1,126 @@ -test.todo("GIVEN interaction.guild is null, EXPECT nothing to happen"); +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"; -test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen"); +jest.mock("../../src/client/appLogger"); -test.todo("GIVEN interaction.channel is null, EXPECT nothing to happen"); +let interaction: ButtonInteractionType; -test.todo("GIVEN channel is not sendable, EXPECT nothing to happen"); +beforeEach(() => { + jest.useFakeTimers(); + jest.setSystemTime(1000 * 60 * 30); -test.todo("GIVEN interaction.message was created more than 5 minutes ago, EXPECT error"); + 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", + }; +}); -test.todo("GIVEN user.RemoveCurrency fails, EXPECT error"); +afterAll(() => { + jest.useRealTimers(); +}); + +test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => { + // Arrange + interaction.guild = null; + + // Act + const claim = new Claim(); + await claim.execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(interaction.deferUpdate).not.toHaveBeenCalled(); + expect(interaction.editReply).not.toHaveBeenCalled(); + expect((interaction.channel as TextChannel).send).not.toHaveBeenCalled(); +}); + +test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => { + // Arrange + interaction.guildId = null; + + // Act + const claim = new Claim(); + await claim.execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(interaction.deferUpdate).not.toHaveBeenCalled(); + expect(interaction.editReply).not.toHaveBeenCalled(); + expect((interaction.channel as TextChannel).send).not.toHaveBeenCalled(); +}); + +test("GIVEN interaction.channel is null, EXPECT nothing to happen", async () => { + // Arrange + interaction.channel = null; + + // Act + const claim = new Claim(); + await claim.execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(interaction.deferUpdate).not.toHaveBeenCalled(); + expect(interaction.editReply).not.toHaveBeenCalled(); +}); + +test("GIVEN channel is not sendable, EXPECT nothing to happen", async () => { + // Arrange + interaction.channel!.isSendable = jest.fn().mockReturnValue(false); + + // Act + const claim = new Claim(); + await claim.execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(interaction.deferUpdate).not.toHaveBeenCalled(); + expect(interaction.editReply).not.toHaveBeenCalled(); + expect((interaction.channel as TextChannel).send).not.toHaveBeenCalled(); +}); + +test("GIVEN interaction.message was created more than 5 minutes ago, EXPECT error", async () => { + // Arrange + interaction.message!.createdAt = new Date(0); + + // Act + const claim = new Claim(); + await claim.execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(interaction.channel!.send).toHaveBeenCalledTimes(1); + expect(interaction.channel!.send).toHaveBeenCalledWith("[object Object], Cards can only be claimed within 5 minutes of it being dropped!"); + + expect(interaction.editReply).not.toHaveBeenCalled(); +}); + +test("GIVEN user.RemoveCurrency fails, EXPECT error", async () => { + // Arrange + User.FetchOneById = jest.fn().mockResolvedValue({ + RemoveCurrency: jest.fn().mockReturnValue(false), + Currency: 5, + }); + + // Act + const claim = new Claim(); + await claim.execute(interaction as unknown as ButtonInteraction); + + // Assert + expect(interaction.channel!.send).toHaveBeenCalledTimes(1); + 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");