vylbot-app/tests/commands/501231711271780357/Lobby/remove.test.ts

116 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-03-27 11:51:26 +00:00
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";
2024-02-03 19:29:28 +00:00
describe('constructor', () => {
2024-03-27 11:51:26 +00:00
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);
});
2024-02-03 19:29:28 +00:00
});
describe("execute", () => {
2024-03-27 11:51:26 +00:00
test("EXPECT channel to be removed from database", 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({});
BaseEntity.Remove = jest.fn();
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;
2024-02-03 19:29:28 +00:00
2024-03-27 11:51:26 +00:00
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue(null);
2024-02-03 19:29:28 +00:00
2024-03-27 11:51:26 +00:00
const remove = new Remove();
await remove.execute(interaction);
2024-02-03 19:29:28 +00:00
2024-03-27 11:51:26 +00:00
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Channel not found.");
});
2024-02-03 19:29:28 +00:00
});