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

82 lines
3 KiB
TypeScript
Raw Normal View History

2024-03-27 11:01:18 +00:00
import { APIEmbedField, CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import List from "../../../../src/commands/501231711271780357/Lobby/list";
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
import EmbedColours from "../../../../src/constants/EmbedColours";
2024-02-03 19:29:28 +00:00
describe('constructor', () => {
2024-03-27 11:01:18 +00:00
test("EXPECT properties to be set", () => {
const list = new List();
expect(list.CommandBuilder).toBeDefined();
const commandBulder = list.CommandBuilder as SlashCommandBuilder;
expect(commandBulder.name).toBe("listlobby");
expect(commandBulder.description).toBe("Lists all channels set up as lobbies");
expect(commandBulder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
});
2024-02-03 19:29:28 +00:00
});
describe("execute", () => {
2024-03-27 11:01:18 +00:00
test('EXPECT list of lobby channels to be sent', async () => {
let repliedWith;
const interaction = {
guild: {
channels: {
cache: {
map: jest.fn().mockReturnValue([{
id: "channelId",
}]),
}
}
},
reply: jest.fn().mockImplementation((options) => {
repliedWith = options;
}),
} as unknown as CommandInteraction;
Lobby.FetchOneByChannelId = jest.fn().mockReturnValue({
Name: "lobbyName",
LastUsed: "lastUsed",
});
const list = new List();
await list.execute(interaction);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(repliedWith).toBeDefined();
expect(repliedWith!.embeds).toBeDefined();
expect(repliedWith!.embeds.length).toBe(1);
const repliedEmbed: EmbedBuilder = repliedWith!.embeds[0];
expect(repliedEmbed.data.color).toBe(EmbedColours.Ok);
expect(repliedEmbed.data.title).toBe("Lobbies");
expect(repliedEmbed.data.description).toBe("Channels: 1");
expect(repliedEmbed.data.fields).toBeDefined();
expect(repliedEmbed.data.fields!.length).toBe(1);
const repliedEmbedField1: APIEmbedField = repliedEmbed.data.fields![0];
expect(repliedEmbedField1.name).toBe("# lobbyName");
expect(repliedEmbedField1.value).toBe("Last Used: lastUsed");
});
test("GIVEN interaction.guild is null, EXPECT error", async () => {
const interaction = {
guild: null,
reply: jest.fn(),
} as unknown as CommandInteraction;
const list = new List();
await list.execute(interaction);
2024-02-03 19:29:28 +00:00
2024-03-27 11:01:18 +00:00
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Guild not found.");
});
2024-02-03 19:29:28 +00:00
});