From 2679cdc4639f57b5b120daeb213ec4f0c0a44aca Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Sat, 3 Feb 2024 11:05:38 +0100 Subject: [PATCH] add unit test for help command --- src/interactions/slashcommands/help.ts | 2 +- .../interactions/slashcommands/help.test.ts | 38 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/interactions/slashcommands/help.ts b/src/interactions/slashcommands/help.ts index 3028056..1c2247f 100644 --- a/src/interactions/slashcommands/help.ts +++ b/src/interactions/slashcommands/help.ts @@ -122,7 +122,7 @@ class HelpCommand extends BaseSlashCommandInteraction { }`; } - private getCommandParams(command: BaseSlashCommandInteraction, locale: LocaleString): string { + public getCommandParams(command: BaseSlashCommandInteraction, locale: LocaleString): string { const commandName = command.data.name; const option = command.data.options[0]; diff --git a/src/tests/interactions/slashcommands/help.test.ts b/src/tests/interactions/slashcommands/help.test.ts index dc32f7e..f075760 100644 --- a/src/tests/interactions/slashcommands/help.test.ts +++ b/src/tests/interactions/slashcommands/help.test.ts @@ -1,6 +1,38 @@ import helpCommand from '../../../interactions/slashcommands/help'; -import { SlashCommandNumberOption } from 'discord.js'; +import Discord, { Client, SlashCommandNumberOption, ChatInputCommandInteraction } from 'discord.js'; import { BaseSlashCommandInteraction } from '../../../common/classes/interactions'; +import { ExtendedClient } from '../../../types/clientTypes'; + +describe('Discord bot command test', () => { + let client: ExtendedClient; + let mockInteraction: ChatInputCommandInteraction; + + beforeEach(() => { + client = new Client({ + intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildVoiceStates] + }); + + client.slashCommandInteractions = new Discord.Collection(); + client.slashCommandInteractions.set('help', helpCommand); + + mockInteraction = { + reply: jest.fn(), + commandName: 'help' + } as unknown as ChatInputCommandInteraction; + }); + + it('should reply when receiving a valid help command', async () => { + // Execute the help command with the mock interaction + await helpCommand.execute({ + executionId: '123', + client, + interaction: mockInteraction as ChatInputCommandInteraction + }); + + // Check if the reply method was called + expect(mockInteraction.reply).toHaveBeenCalledWith(expect.anything()); + }); +}); describe('HelpCommand', () => { describe('getCommandParams', () => { @@ -17,7 +49,7 @@ describe('HelpCommand', () => { } as unknown as BaseSlashCommandInteraction; const locale = 'en-US'; - const commandParams = helpCommand['getCommandParams'](command, locale); + const commandParams = helpCommand.getCommandParams(command, locale); expect(commandParams).toBe('**`optionname`** '); }); @@ -30,7 +62,7 @@ describe('HelpCommand', () => { } as unknown as BaseSlashCommandInteraction; const locale = 'en-US'; - const commandParams = helpCommand['getCommandParams'](command, locale); + const commandParams = helpCommand.getCommandParams(command, locale); expect(commandParams).toBe(''); });