vylbot-app/tests/buttonEvents/verify.test.ts

155 lines
5.2 KiB
TypeScript
Raw Normal View History

2024-02-05 15:38:24 +00:00
import { ButtonInteraction, GuildMember, Role } from "discord.js";
import Verify from "../../src/buttonEvents/verify";
import SettingsHelper from "../../src/helpers/SettingsHelper";
jest.mock("../../src/helpers/SettingsHelper");
2024-02-03 19:29:28 +00:00
describe("execute", () => {
2024-02-05 15:38:24 +00:00
let role: Role;
let member: GuildMember;
let interaction: ButtonInteraction;
let entity: Verify;
beforeEach(() => {
entity = new Verify();
role = {} as Role;
member = {
manageable: true,
roles: {
add: jest.fn(),
}
} as unknown as GuildMember;
interaction = {
reply: jest.fn(),
guildId: 'guildId',
guild: {
roles: {
cache: {
find: jest.fn().mockReturnValue(role),
}
},
members: {
cache: {
find: jest.fn().mockReturnValue(member),
}
}
}
} as unknown as ButtonInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("roleName");
});
test("EXPECT verification role to be given", async () => {
await entity.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith({
content: "Given role",
ephemeral: true,
});
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("verification.role", "guildId");
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
expect(interaction.guild?.members.cache.find).toHaveBeenCalledTimes(1);
expect(member.roles.add).toHaveBeenCalledWith(role);
});
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
interaction.guildId = null;
await entity.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
expect(interaction.guild?.roles.cache.find).not.toHaveBeenCalled();
expect(interaction.guild?.members.cache.find).not.toHaveBeenCalled();
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
interaction = {
reply: jest.fn(),
guildId: 'guildId',
guild: null,
} as unknown as ButtonInteraction;
await entity.execute(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN verification.role setting is not found, EXPECT nothing to happen", async () => {
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
await entity.execute(interaction);
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("verification.role", "guildId");
expect(interaction.reply).not.toHaveBeenCalled();
expect(interaction.guild?.roles.cache.find).not.toHaveBeenCalled();
expect(interaction.guild?.members.cache.find).not.toHaveBeenCalled();
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN role can not be found, EXPECT error", async () => {
interaction.guild!.roles.cache.find = jest.fn().mockReturnValue(undefined);
await entity.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith({
content: "Unable to find the role, roleName",
ephemeral: true,
});
expect(interaction.guild?.members.cache.find).not.toHaveBeenCalled();
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN member can not be found, EXPECT error", async () => {
interaction.guild!.members.cache.find = jest.fn().mockReturnValue(undefined);
await entity.execute(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith({
content: "Unable to give role to user",
ephemeral: true,
});
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
expect(interaction.guild?.members.cache.find).toHaveBeenCalledTimes(1);
2024-02-03 19:29:28 +00:00
2024-02-05 15:38:24 +00:00
expect(member.roles.add).not.toHaveBeenCalled();
});
2024-02-03 19:29:28 +00:00
2024-02-05 15:38:24 +00:00
test("GIVEN member is not manageable, EXPECT error", async () => {
member = {
manageable: false,
roles: {
add: jest.fn(),
}
} as unknown as GuildMember;
2024-02-03 19:29:28 +00:00
2024-02-05 15:38:24 +00:00
interaction.guild!.members.cache.find = jest.fn().mockReturnValue(member);
2024-02-03 19:29:28 +00:00
2024-02-05 15:38:24 +00:00
await entity.execute(interaction);
2024-02-03 19:29:28 +00:00
2024-02-05 15:38:24 +00:00
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith({
content: "Unable to give role to user",
ephemeral: true,
});
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
expect(interaction.guild?.members.cache.find).toHaveBeenCalledTimes(1);
2024-02-03 19:29:28 +00:00
2024-02-05 15:38:24 +00:00
expect(member.roles.add).not.toHaveBeenCalled();
});
2024-02-03 19:29:28 +00:00
});