diff --git a/src/commands/ignore.ts b/src/commands/ignore.ts index f1ab7ac..335258f 100644 --- a/src/commands/ignore.ts +++ b/src/commands/ignore.ts @@ -13,23 +13,16 @@ export default class Ignore extends Command { } public override async execute(interaction: CommandInteraction) { - if (!interaction.guildId) return; - - const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.guildId); + const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.channelId); if (isChannelIgnored) { - const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.guildId); + const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.channelId); - if (!entity) { - await interaction.reply('Unable to find channel.'); - return; - } - - await IgnoredChannel.Remove(IgnoredChannel, entity); + await IgnoredChannel.Remove(IgnoredChannel, entity!); await interaction.reply('This channel will start being logged again.'); } else { - const entity = new IgnoredChannel(interaction.guildId); + const entity = new IgnoredChannel(interaction.channelId); await entity.Save(IgnoredChannel, entity); diff --git a/tests/commands/ignore.test.ts b/tests/commands/ignore.test.ts index d60ba60..fad4cd6 100644 --- a/tests/commands/ignore.test.ts +++ b/tests/commands/ignore.test.ts @@ -1,11 +1,79 @@ +import { CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js"; +import Command from "../../src/commands/ignore"; +import IgnoredChannel from "../../src/database/entities/IgnoredChannel"; + describe('constructor', () => { - test.todo("EXPECT properties to be set"); + test("EXPECT properties to be set", () => { + const command = new Command(); + + expect(command.CommandBuilder).toBeDefined(); + + const commandBuilder = command.CommandBuilder as SlashCommandBuilder; + + expect(commandBuilder.name).toBe("ignore"); + expect(commandBuilder.description).toBe("Ignore events in this channel"); + expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.Administrator.toString()); + }); }); describe("execute", () => { - test.todo("GIVEN channel is currently ignored, EXPECT channel to be removed from list"); + test("GIVEN channel is currently ignored, EXPECT channel to be removed from list", async () => { + // Arrange + const interaction = { + guildId: "guildId", + channelId: "channelId", + reply: jest.fn(), + } as unknown as CommandInteraction; - test.todo("GIVEN channel is not currently ignored, EXPECT channel to be added to list"); + IgnoredChannel.IsChannelIgnored = jest.fn().mockResolvedValue(true); + IgnoredChannel.FetchOneById = jest.fn().mockResolvedValue({}); + IgnoredChannel.Remove = jest.fn(); - test.todo("GIVEN channel is currently ignored but not found in database, EXPECT error"); + // Act + const command = new Command(); + await command.execute(interaction); + + // Assert + expect(IgnoredChannel.IsChannelIgnored).toHaveBeenCalledTimes(1); + expect(IgnoredChannel.IsChannelIgnored).toHaveBeenCalledWith("channelId"); + + expect(IgnoredChannel.FetchOneById).toHaveBeenCalledTimes(1); + expect(IgnoredChannel.FetchOneById).toHaveBeenCalledWith(IgnoredChannel, "channelId"); + + expect(IgnoredChannel.Remove).toHaveBeenCalledTimes(1); + expect(IgnoredChannel.Remove).toHaveBeenCalledWith(IgnoredChannel, {}); + + expect(interaction.reply).toHaveBeenCalledTimes(1); + expect(interaction.reply).toHaveBeenCalledWith("This channel will start being logged again."); + }); + + test("GIVEN channel is not currently ignored, EXPECT channel to be added to list", async () => { + let savedChannel: IgnoredChannel | undefined; + + // Arrange + const interaction = { + guildId: "guildId", + channelId: "channelId", + reply: jest.fn(), + } as unknown as CommandInteraction; + + IgnoredChannel.IsChannelIgnored = jest.fn().mockResolvedValue(false); + IgnoredChannel.prototype.Save = jest.fn().mockImplementation((_, channel: IgnoredChannel) => { + savedChannel = channel; + }); + + // Act + const command = new Command(); + await command.execute(interaction); + + // Assert + expect(IgnoredChannel.prototype.Save).toHaveBeenCalledTimes(1); + expect(IgnoredChannel.prototype.Save).toHaveBeenCalledWith(IgnoredChannel, expect.any(IgnoredChannel)); + + expect(interaction.reply).toHaveBeenCalledTimes(1); + expect(interaction.reply).toHaveBeenCalledWith("This channel will now be ignored from logging."); + + expect(savedChannel).toBeDefined(); + expect(savedChannel!.Id).toBe("channelId"); + }); }); \ No newline at end of file