From 6af92ad1c7aaad50395bd1f9cef346be301ebdda Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 27 May 2023 14:52:01 +0100 Subject: [PATCH] Add tests --- tests/commands/timeout.test.ts | 259 ++++++++++++++++++++++++++++++++- 1 file changed, 255 insertions(+), 4 deletions(-) diff --git a/tests/commands/timeout.test.ts b/tests/commands/timeout.test.ts index c31394a..f203dd6 100644 --- a/tests/commands/timeout.test.ts +++ b/tests/commands/timeout.test.ts @@ -194,7 +194,19 @@ describe('execute', () => { expect(interaction.reply).not.toBeCalled(); }); - test.todo('GIVEN interaction.guildId IS NULL, EXPECT nothing to happen'); + test('GIVEN interaction.guildId IS NULL, EXPECT nothing to happen', async () => { + const command = new Timeout(); + + const interaction = { + guild: mock(), + guildId: null, + reply: jest.fn(), + } as unknown as CommandInteraction; + + await command.execute(interaction); + + expect(interaction.reply).not.toBeCalled(); + }); // Validation test('GIVEN targetUser IS NULL, EXPECT validation error', async () => { @@ -469,10 +481,249 @@ describe('execute', () => { }); // Log embed - test.todo('GIVEN channelName IS NULL, EXPECT execution to return'); + test('GIVEN channelName IS NULL, EXPECT execution to return', async () => { + const command = new Timeout(); - test.todo('GIVEN channel IS NULL, EXPECT embed to not be sent'); + let savedAudit: DeepPartial | undefined; + + const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget, entity: DeepPartial): Promise => { + savedAudit = entity; + + return Promise.resolve(); + }); + + const settingsGet = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue(undefined); + + const timeoutFunc = jest.fn(); + + const sentEmbeds: EmbedBuilder[] = []; + + const logChannelSendFunc = jest.fn(); + + const interaction = { + reply: jest.fn(), + guild: { + channels: { + cache: { + find: jest.fn().mockReturnValue({ + send: logChannelSendFunc, + } as unknown as TextChannel), + } + } + }, + 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: 'Test reason', + } as CommandInteractionOption; + default: + return null; + } + }), + } + } as unknown as CommandInteraction; + + + await command.execute(interaction); + + expect(timeoutFunc).toBeCalled(); + expect(sentEmbeds.length).toBe(0); + expect(logChannelSendFunc).not.toBeCalled(); + }); + + test('GIVEN channel IS NULL, EXPECT embed to not be sent', async () => { + const command = new Timeout(); + + let savedAudit: DeepPartial | undefined; + + const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget, entity: DeepPartial): Promise => { + savedAudit = entity; + + return Promise.resolve(); + }); + + const settingsGet = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs'); + + const timeoutFunc = jest.fn(); + + const sentEmbeds: EmbedBuilder[] = []; + + const interaction = { + reply: jest.fn(), + guild: { + channels: { + cache: { + find: jest.fn().mockReturnValue(undefined), + } + } + }, + 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: 'Test reason', + } as CommandInteractionOption; + default: + return null; + } + }), + } + } as unknown as CommandInteraction; + + + await command.execute(interaction); + + expect(timeoutFunc).toBeCalled(); + expect(sentEmbeds.length).toBeGreaterThan(0); + }); // DM user - test.todo('GIVEN user can NOT be messaged, EXPECT resultEmbed to contain "DM Sent = false"'); + test('GIVEN user can NOT be messaged, EXPECT resultEmbed to contain "DM Sent = false"', async () => { + let embeds: APIEmbed[] | undefined; + + const command = new Timeout(); + + const interactionReply = jest.fn((options: InteractionReplyOptions) => { + embeds = options.embeds as APIEmbed[]; + }); + + let savedAudit: DeepPartial | undefined; + + const getSetting = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs'); + const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget, entity: DeepPartial): Promise => { + savedAudit = entity; + + return Promise.resolve(); + }); + + const timeoutFunc = jest.fn(); + + let dmChannelSentEmbeds: (APIEmbed | JSONEncodable)[] | undefined; + let logsChannelSentEmbeds: (APIEmbed | JSONEncodable)[] | undefined; + + const dmChannel = { + send: jest.fn().mockImplementation((options: MessageCreateOptions) => { + dmChannelSentEmbeds = options.embeds; + }), + } as unknown as DMChannel; + + const userInput = { + user: { + id: 'userId', + tag: 'userTag', + createDM: jest.fn().mockRejectedValue(undefined), + } as unknown as User, + member: { + manageable: true, + timeout: timeoutFunc, + } as unknown as GuildMember, + } as CommandInteractionOption; + + const lengthInput = { + value: '1s', + } as CommandInteractionOption; + + const reasonInput = { + value: 'Test reason', + } as CommandInteractionOption; + + const logsChannel = { + name: 'mod-logs', + send: jest.fn().mockImplementation((options: MessageCreateOptions) => { + logsChannelSentEmbeds = options.embeds; + }), + } as unknown as TextChannel; + + const interaction = { + guild: { + channels: { + cache: { + find: jest.fn() + .mockReturnValue(logsChannel), + } + }, + name: "Test Guild", + } as unknown as Guild, + guildId: 'guildId', + reply: interactionReply, + options: { + get: jest.fn() + .mockReturnValueOnce(userInput) + .mockReturnValueOnce(lengthInput) + .mockReturnValue(reasonInput), + }, + user: { + id: 'moderatorId' + } + } as unknown as CommandInteraction; + + await command.execute(interaction); + + // EXPECT embeds to be sent + expect(embeds).toBeDefined(); + expect(embeds!.length).toBe(1); + + const resultEmbed = embeds![0] as EmbedBuilder; + + // EXPECT DM field to be configured + const resultEmbedDMField = resultEmbed.data.fields![0]; + + expect(resultEmbedDMField.name).toBe("DM Sent"); + expect(resultEmbedDMField.value).toBe("false"); + }); }); \ No newline at end of file