Create remove lobby tests

This commit is contained in:
Ethan Lane 2024-03-27 11:51:26 +00:00
parent 5e30962053
commit 946422b2ac

View file

@ -1,13 +1,116 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandChannelOption } from "discord.js";
import Remove from "../../../../src/commands/501231711271780357/Lobby/remove";
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
import BaseEntity from "../../../../src/contracts/BaseEntity";
describe('constructor', () => {
test.todo("EXPECT properties to be set");
test("EXPECT properties to be set", () => {
const remove = new Remove();
expect(remove.CommandBuilder).toBeDefined();
const commandBuilder = remove.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("removelobby");
expect(commandBuilder.description).toBe("Remove a lobby channel");
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
expect(commandBuilder.options.length).toBe(1);
const channelOption = commandBuilder.options[0] as SlashCommandChannelOption;
expect(channelOption.name).toBe("channel");
expect(channelOption.description).toBe("The channel");
expect(channelOption.required).toBe(true);
});
});
describe("execute", () => {
test.todo("EXPECT channel to be removed from database");
test("EXPECT channel to be removed from database", async () => {
const channel = {
channel: {
id: "channelId",
},
};
test.todo("GIVEN channel is null, EXPECT error");
const interaction = {
options: {
get: jest.fn().mockReturnValue(channel),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
test.todo("GIVEN channel.channel is undefined, EXPECT error");
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue({});
BaseEntity.Remove = jest.fn();
test.todo("GIVEN channel is not set up as a lobby, EXPECT error");
const remove = new Remove();
await remove.execute(interaction);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
expect(BaseEntity.Remove).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledTimes(1);
expect(interaction.options.get).toHaveBeenCalledWith("channel");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Removed <#channelId> from the list of lobby channels");
});
test("GIVEN channel is null, EXPECT error", async () => {
const interaction = {
options: {
get: jest.fn().mockReturnValue(null),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const remove = new Remove();
await remove.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Channel is required.");
});
test("GIVEN channel.channel is undefined, EXPECT error", async () => {
const channel = {
channel: undefined,
};
const interaction = {
options: {
get: jest.fn().mockReturnValue(channel),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
const remove = new Remove();
await remove.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Channel is required.");
});
test("GIVEN channel is not set up as a lobby, EXPECT error", async () => {
const channel = {
channel: {
id: "channelId",
},
};
const interaction = {
options: {
get: jest.fn().mockReturnValue(channel),
},
reply: jest.fn(),
} as unknown as CommandInteraction;
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue(null);
const remove = new Remove();
await remove.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Channel not found.");
});
});