diff --git a/tests/client/util.test.ts b/tests/client/util.test.ts index 34ad538..0ac9b71 100644 --- a/tests/client/util.test.ts +++ b/tests/client/util.test.ts @@ -1,27 +1,9 @@ -import { Client, SlashCommandBuilder } from "discord.js"; +import discordjs, { Client, REST, Routes, SlashCommandBuilder } from "discord.js"; import { Util } from "../../src/client/util"; import { CoreClient } from "../../src/client/client"; import { Command } from "../../src/type/command"; - -jest.mock("discord.js", () => { - return { - Client: jest.fn(), - REST: jest.fn().mockImplementation(() => { - return { - v10: { - put: jest.fn(), - }, - setToken: jest.fn(), - } - }), - Routes: { - applicationCommands: jest.fn().mockReturnValue("command url"), - applicationGuildCommands: jest.fn().mockReturnValue("guild command url"), - }, - } -}); - -import { REST, Routes } from "discord.js"; +import IEventItem from "../../src/contracts/IEventItem"; +import { EventType } from "../../src/constants/EventType"; beforeEach(() => { jest.resetAllMocks(); @@ -43,6 +25,15 @@ describe("loadSlashCommands", () => { } } as unknown as Client; + const setTokenPutMock = jest.fn(); + const setTokenMock = jest.fn().mockReturnValue({ + put: setTokenPutMock, + }); + + REST.prototype.setToken = setTokenMock; + Routes.applicationCommands = jest.fn().mockReturnValue("command url"); + Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url"); + CoreClient.commandItems = [ { Name: "global", @@ -66,12 +57,11 @@ describe("loadSlashCommands", () => { const util = new Util(); util.loadSlashCommands(client); - expect(REST).toHaveBeenCalledWith({ version: '10' }); - expect(REST.prototype.setToken).toHaveBeenCalledWith("token"); + expect(setTokenMock).toHaveBeenCalledWith("token"); - expect(REST.prototype.put).toHaveBeenCalledTimes(2); - expect(REST.prototype.put).toHaveBeenCalledWith("command url", { body: [ CoreClient.commandItems[0].Command.CommandBuilder ] }); - expect(REST.prototype.put).toHaveBeenCalledWith("guild command url", { body: [ CoreClient.commandItems[1].Command.CommandBuilder ]}); + expect(setTokenPutMock).toHaveBeenCalledTimes(2); + expect(setTokenPutMock).toHaveBeenCalledWith("command url", { body: [ CoreClient.commandItems[0].Command.CommandBuilder ] }); + expect(setTokenPutMock).toHaveBeenCalledWith("guild command url", { body: [ CoreClient.commandItems[1].Command.CommandBuilder ]}); expect(client.guilds.cache.has).toHaveBeenCalledTimes(1); expect(client.guilds.cache.has).toHaveBeenCalledWith("123"); @@ -83,31 +73,261 @@ describe("loadSlashCommands", () => { expect(Routes.applicationGuildCommands).toHaveBeenCalledWith("clientid", "123"); }); - test.todo("GIVEN bot is not in a guild for a server command, EXPECT this to be ignored"); + test("GIVEN bot is not in a guild for a server command, EXPECT this to be ignored", () => { + process.env.BOT_TOKEN = "token"; + process.env.BOT_CLIENTID = "clientid"; + + const client = { + guilds: { + cache: { + has: jest.fn().mockReturnValue(false), + } + } + } as unknown as Client; + + const setTokenPutMock = jest.fn(); + const setTokenMock = jest.fn().mockReturnValue({ + put: setTokenPutMock, + }); + + REST.prototype.setToken = setTokenMock; + Routes.applicationCommands = jest.fn().mockReturnValue("command url"); + Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url"); + + CoreClient.commandItems = [ + { + Name: "server-specific", + Command: { + CommandBuilder: { + name: "server", + } as SlashCommandBuilder, + } as Command, + ServerId: "123", + } + ]; + + const util = new Util(); + util.loadSlashCommands(client); + + expect(Routes.applicationGuildCommands).not.toHaveBeenCalled(); + }); }); describe("loadEvents", () => { - test.todo("GIVEN event type is channelCreate, EXPECT event function to be executed"); + test("GIVEN event type is channelCreate, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.ChannelCreate, + ExecutionFunction: jest.fn(), + } + ]; - test.todo("GIVEN event type is channelDelete, EXPECT event function to be executed"); + const util = new Util(); + util.loadEvents(client, events); - test.todo("GIVEN event type is channelUpdate, EXPECT event function to be executed"); + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("channelCreate", expect.any(Function)); + }); - test.todo("GIVEN event type is guildBanAdd, EXPECT event function to be executed"); + test("GIVEN event type is channelDelete, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.ChannelDelete, + ExecutionFunction: jest.fn(), + } + ]; - test.todo("GIVEN event type is guildBanRemove, EXPECT event function to be executed"); + const util = new Util(); + util.loadEvents(client, events); - test.todo("GIVEN event type is guildCreate, EXPECT event function to be executed"); + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("channelDelete", expect.any(Function)); + }); - test.todo("GIVEN event type is guildMemberAdd, EXPECT event function to be executed"); + test("GIVEN event type is channelUpdate, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.ChannelUpdate, + ExecutionFunction: jest.fn(), + } + ]; - test.todo("GIVEN event type is guildMemberRemove, EXPECT event function to be executed"); + const util = new Util(); + util.loadEvents(client, events); - test.todo("GIVEN event type is messageCreate, EXPECT event function to be executed"); + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("channelUpdate", expect.any(Function)); + }); - test.todo("GIVEN event type is messageDelete, EXPECT event function to be executed"); + test("GIVEN event type is guildBanAdd, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.GuildBanAdd, + ExecutionFunction: jest.fn(), + } + ]; - test.todo("GIVEN event type is messageUpdate, EXPECT event function to be executed"); + const util = new Util(); + util.loadEvents(client, events); - test.todo("GIVEN event type is not implemented, EXPECT error"); + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("guildBanAdd", expect.any(Function)); + }); + + test("GIVEN event type is guildBanRemove, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.GuildBanRemove, + ExecutionFunction: jest.fn(), + } + ]; + + const util = new Util(); + util.loadEvents(client, events); + + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("guildBanRemove", expect.any(Function)); + }); + + test("GIVEN event type is guildCreate, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.GuildCreate, + ExecutionFunction: jest.fn(), + } + ]; + + const util = new Util(); + util.loadEvents(client, events); + + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("guildCreate", expect.any(Function)); + }); + + test("GIVEN event type is guildMemberAdd, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.GuildMemberAdd, + ExecutionFunction: jest.fn(), + } + ]; + + const util = new Util(); + util.loadEvents(client, events); + + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("guildMemberAdd", expect.any(Function)); + }); + + test("GIVEN event type is guildMemberRemove, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.GuildMemberRemove, + ExecutionFunction: jest.fn(), + } + ]; + + const util = new Util(); + util.loadEvents(client, events); + + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("guildMemberRemove", expect.any(Function)); + }); + + test("GIVEN event type is messageCreate, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.MessageCreate, + ExecutionFunction: jest.fn(), + } + ]; + + const util = new Util(); + util.loadEvents(client, events); + + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("messageCreate", expect.any(Function)); + }); + + test("GIVEN event type is messageDelete, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.MessageDelete, + ExecutionFunction: jest.fn(), + } + ]; + + const util = new Util(); + util.loadEvents(client, events); + + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("messageDelete", expect.any(Function)); + }); + + test("GIVEN event type is messageUpdate, EXPECT event function to be executed", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.MessageUpdate, + ExecutionFunction: jest.fn(), + } + ]; + + const util = new Util(); + util.loadEvents(client, events); + + expect(client.on).toHaveBeenCalledTimes(1); + expect(client.on).toHaveBeenCalledWith("messageUpdate", expect.any(Function)); + }); + + test("GIVEN event type is not implemented, EXPECT error", () => { + const client = { + on: jest.fn(), + } as unknown as Client; + const events: IEventItem[] = [ + { + EventType: EventType.Ready, + ExecutionFunction: jest.fn(), + } + ]; + + console.error = jest.fn(); + + const util = new Util(); + util.loadEvents(client, events); + + expect(console.error).toHaveBeenCalledWith("Event not implemented."); + }); }); \ No newline at end of file