Create ignore command tests
All checks were successful
Test / build (push) Successful in 17s

This commit is contained in:
Ethan Lane 2024-05-11 10:58:50 +01:00
parent ac51eb7f40
commit 2841ed5463
2 changed files with 76 additions and 15 deletions

View file

@ -13,23 +13,16 @@ export default class Ignore extends Command {
} }
public override async execute(interaction: CommandInteraction) { public override async execute(interaction: CommandInteraction) {
if (!interaction.guildId) return; const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.channelId);
const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.guildId);
if (isChannelIgnored) { if (isChannelIgnored) {
const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.guildId); const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.channelId);
if (!entity) { await IgnoredChannel.Remove(IgnoredChannel, entity!);
await interaction.reply('Unable to find channel.');
return;
}
await IgnoredChannel.Remove(IgnoredChannel, entity);
await interaction.reply('This channel will start being logged again.'); await interaction.reply('This channel will start being logged again.');
} else { } else {
const entity = new IgnoredChannel(interaction.guildId); const entity = new IgnoredChannel(interaction.channelId);
await entity.Save(IgnoredChannel, entity); await entity.Save(IgnoredChannel, entity);

View file

@ -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', () => { 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", () => { 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");
});
}); });