diff --git a/src/client/events.ts b/src/client/events.ts index 97d8cc7..b2f01e3 100644 --- a/src/client/events.ts +++ b/src/client/events.ts @@ -8,10 +8,10 @@ export class Events { if (interaction.isChatInputCommand()) { ChatInputCommand.onChatInput(interaction); - } - - if (interaction.isButton()) { + } else if (interaction.isButton()) { Button.onButtonClicked(interaction); + } else { + console.error("Received interaction unable to interact with, ignoring"); } } diff --git a/tests/client/events.test.ts b/tests/client/events.test.ts index ad0a2a9..b5aab06 100644 --- a/tests/client/events.test.ts +++ b/tests/client/events.test.ts @@ -1,11 +1,89 @@ +import { Interaction } from "discord.js"; +import { Events } from "../../src/client/events"; +import ChatInputCommand from "../../src/client/interactionCreate/chatInputCommand"; +import Button from "../../src/client/interactionCreate/button"; + describe('onInteractionCreate', () => { - test.todo("GIVEN the interaction is a chat input command, EXPECT chat input command handler to be executed"); + test("GIVEN the interaction is a chat input command, EXPECT chat input command handler to be executed", async () => { + const interaction = { + isChatInputCommand: jest.fn().mockReturnValue(true), + guildId: "123", + } as unknown as Interaction; - test.todo("GIVEN the interaction is a button, EXPECT button handler to be executed"); + ChatInputCommand.onChatInput = jest.fn(); - test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen"); + const events = new Events(); + await events.onInteractionCreate(interaction); + + expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1); + + expect(ChatInputCommand.onChatInput).toHaveBeenCalledTimes(1); + expect(ChatInputCommand.onChatInput).toHaveBeenCalledWith(interaction); + }); + + test("GIVEN the interaction is a button, EXPECT button handler to be executed", async () => { + const interaction = { + isChatInputCommand: jest.fn().mockReturnValue(false), + isButton: jest.fn().mockReturnValue(true), + guildId: "123", + } as unknown as Interaction; + + Button.onButtonClicked = jest.fn(); + + const events = new Events(); + await events.onInteractionCreate(interaction); + + expect(interaction.isButton).toHaveBeenCalledTimes(1); + + expect(Button.onButtonClicked).toHaveBeenCalledTimes(1); + expect(Button.onButtonClicked).toHaveBeenCalledWith(interaction); + }); + + test("GIVEN the interaction is not a chat input command or button, EXPECT error", async () => { + const interaction = { + isChatInputCommand: jest.fn().mockReturnValue(false), + isButton: jest.fn().mockReturnValue(false), + guildId: "123", + } as unknown as Interaction; + + console.error = jest.fn(); + + const events = new Events(); + await events.onInteractionCreate(interaction); + + expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1); + expect(interaction.isButton).toHaveBeenCalledTimes(1); + + expect(console.error).toHaveBeenCalledWith("Received interaction unable to interact with, ignoring"); + }); + + test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => { + const interaction = { + isChatInputCommand: jest.fn(), + isButton: jest.fn(), + guildId: null, + } as unknown as Interaction; + + ChatInputCommand.onChatInput = jest.fn(); + Button.onButtonClicked = jest.fn(); + + const events = new Events(); + await events.onInteractionCreate(interaction); + + expect(interaction.isChatInputCommand).not.toHaveBeenCalled(); + expect(interaction.isButton).not.toHaveBeenCalled(); + expect(ChatInputCommand.onChatInput).not.toHaveBeenCalled(); + expect(Button.onButtonClicked).not.toHaveBeenCalled(); + }); }); describe("onReady", () => { - test.todo("EXPECT console to be logged"); + test("EXPECT console to be logged", () => { + console.log = jest.fn(); + + const events = new Events(); + events.onReady(); + + expect(console.log).toHaveBeenCalledWith("Ready"); + }); }); \ No newline at end of file