From 177f7a3072cb9b327a0a884e588d102481cb938a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 27 May 2023 14:40:09 +0100 Subject: [PATCH] Add reason is null test --- tests/commands/timeout.test.ts | 78 ++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/tests/commands/timeout.test.ts b/tests/commands/timeout.test.ts index 67c1d21..c31394a 100644 --- a/tests/commands/timeout.test.ts +++ b/tests/commands/timeout.test.ts @@ -1,4 +1,4 @@ -import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, Embed, EmbedBuilder, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, MessageCreateOptions, SlashCommandBuilder, TextChannel, User } from "discord.js"; +import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, Embed, EmbedBuilder, EmbedField, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, Message, MessageCreateOptions, MessagePayload, SlashCommandBuilder, TextChannel, User } from "discord.js"; import { mock } from "jest-mock-extended"; import Timeout from "../../src/commands/timeout"; import SettingsHelper from "../../src/helpers/SettingsHelper"; @@ -392,9 +392,81 @@ describe('execute', () => { }); // Reason variable - test.todo('GIVEN reason IS NOT NULL, EXPECT to be ran with reason set'); + test('GIVEN reason IS NULL, EXPECT to be ran with empty string', async () => { + const command = new Timeout(); - test.todo('GIVEN reason IS NULL, EXPECT to be ran with empty string'); + let savedAudit: DeepPartial | undefined; + + const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget, entity: DeepPartial): Promise => { + savedAudit = entity; + + return Promise.resolve(); + }); + + const timeoutFunc = jest.fn(); + + const sentEmbeds: EmbedBuilder[] = []; + + const interaction = { + reply: jest.fn(), + guild: { + channels: { + cache: { + find: jest.fn().mockReturnValue(mock()), + } + } + }, + guildId: 'guildId', + user: { + id: 'moderatorId', + }, + options: { + get: jest.fn((value: string): CommandInteractionOption | null => { + switch (value) { + case 'target': + return { + user: { + id: 'userId', + tag: 'userTag', + createDM: jest.fn().mockReturnValue({ + send: jest.fn(async (options: MessageCreateOptions): Promise> => { + sentEmbeds.push(options.embeds![0] as EmbedBuilder); + + return mock>(); + }) + }) as unknown as DMChannel, + } as unknown as User, + member: { + manageable: true, + timeout: timeoutFunc, + } as unknown as GuildMember + } as CommandInteractionOption; + case 'length': + return { + value: '1m' + } as CommandInteractionOption; + case 'reason': + return { + value: undefined, + } as CommandInteractionOption; + default: + return null; + } + }), + } + } as unknown as CommandInteraction; + + + await command.execute(interaction); + + expect(timeoutFunc).toBeCalledWith(1000 * 60 * 1, ""); + expect(savedAudit?.Reason).toBe("*none*"); + + const dmEmbed = (sentEmbeds[0] as any).data; + const dmEmbedReasonField = dmEmbed.fields![0] as EmbedField; + + expect(dmEmbedReasonField.value).toBe("*none*"); + }); // Log embed test.todo('GIVEN channelName IS NULL, EXPECT execution to return');