From 6d2b323338f62295116ac1bb7463dfd800cf0acb Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 25 Mar 2024 17:31:45 +0000 Subject: [PATCH] WIP: util tests --- tests/client/util.test.ts | 84 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/tests/client/util.test.ts b/tests/client/util.test.ts index 6c38068..34ad538 100644 --- a/tests/client/util.test.ts +++ b/tests/client/util.test.ts @@ -1,5 +1,87 @@ +import { Client, 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"; + +beforeEach(() => { + jest.resetAllMocks(); + jest.resetModules(); + + process.env = {}; +}); + describe("loadSlashCommands", () => { - test.todo("EXPECT slash commands to be loaded to the discord API"); + test("EXPECT slash commands to be loaded to the discord API", () => { + process.env.BOT_TOKEN = "token"; + process.env.BOT_CLIENTID = "clientid"; + + const client = { + guilds: { + cache: { + has: jest.fn().mockReturnValue(true), + } + } + } as unknown as Client; + + CoreClient.commandItems = [ + { + Name: "global", + Command: { + CommandBuilder: { + name: "global" + } as SlashCommandBuilder, + } as Command, + }, + { + Name: "server-specific", + Command: { + CommandBuilder: { + name: "server", + } as SlashCommandBuilder, + } as Command, + ServerId: "123", + } + ]; + + const util = new Util(); + util.loadSlashCommands(client); + + expect(REST).toHaveBeenCalledWith({ version: '10' }); + expect(REST.prototype.setToken).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(client.guilds.cache.has).toHaveBeenCalledTimes(1); + expect(client.guilds.cache.has).toHaveBeenCalledWith("123"); + + expect(Routes.applicationCommands).toHaveBeenCalledTimes(1); + expect(Routes.applicationCommands).toHaveBeenCalledWith("clientid"); + + expect(Routes.applicationGuildCommands).toHaveBeenCalledTimes(1); + expect(Routes.applicationGuildCommands).toHaveBeenCalledWith("clientid", "123"); + }); test.todo("GIVEN bot is not in a guild for a server command, EXPECT this to be ignored"); });