Add verify and button tests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ethan Lane 2024-02-05 15:38:24 +00:00
parent 69ef94dbb5
commit 4e9ec89108
3 changed files with 223 additions and 32 deletions

View file

@ -13,32 +13,20 @@ import ButtonEventItem from "../contracts/ButtonEventItem";
import { ButtonEvent } from "../type/buttonEvent";
export class CoreClient extends Client {
private static _commandItems: ICommandItem[];
private static _eventItems: IEventItem[];
private static _buttonEvents: ButtonEventItem[];
public static commandItems: ICommandItem[];
public static eventItems: IEventItem[];
public static buttonEvents: ButtonEventItem[];
private _events: Events;
private _util: Util;
public static get commandItems(): ICommandItem[] {
return this._commandItems;
}
public static get eventItems(): IEventItem[] {
return this._eventItems;
}
public static get buttonEvents(): ButtonEventItem[] {
return this._buttonEvents;
}
constructor(intents: number[], partials: Partials[]) {
super({ intents: intents, partials: partials });
dotenv.config();
CoreClient._commandItems = [];
CoreClient._eventItems = [];
CoreClient._buttonEvents = [];
CoreClient.commandItems = [];
CoreClient.eventItems = [];
CoreClient.buttonEvents = [];
this._events = new Events();
this._util = new Util();
@ -59,7 +47,7 @@ export class CoreClient extends Client {
await super.login(process.env.BOT_TOKEN);
this._util.loadEvents(this, CoreClient._eventItems);
this._util.loadEvents(this, CoreClient.eventItems);
this._util.loadSlashCommands(this);
}
@ -70,7 +58,7 @@ export class CoreClient extends Client {
ServerId: serverId,
};
CoreClient._commandItems.push(item);
CoreClient.commandItems.push(item);
}
public static RegisterEvent(eventType: EventType, func: Function) {
@ -79,7 +67,7 @@ export class CoreClient extends Client {
ExecutionFunction: func,
};
CoreClient._eventItems.push(item);
CoreClient.eventItems.push(item);
}
public static RegisterButtonEvent(buttonId: string, event: ButtonEvent) {
@ -88,6 +76,6 @@ export class CoreClient extends Client {
Event: event,
};
CoreClient._buttonEvents.push(item);
CoreClient.buttonEvents.push(item);
}
}

View file

@ -1,15 +1,155 @@
import { ButtonInteraction, GuildMember, Role } from "discord.js";
import Verify from "../../src/buttonEvents/verify";
import SettingsHelper from "../../src/helpers/SettingsHelper";
jest.mock("../../src/helpers/SettingsHelper");
describe("execute", () => {
test.todo("EXPECT verification role to be given");
let role: Role;
let member: GuildMember;
let interaction: ButtonInteraction;
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen");
let entity: Verify;
test.todo("GIVEN interaction.guild is null, EXPECT nothing to happen");
beforeEach(() => {
entity = new Verify();
test.todo("GIVEN verification.role setting is not found, EXPECT nothing to happen");
role = {} as Role;
test.todo("GIVEN role can not be found, EXPECT error");
member = {
manageable: true,
roles: {
add: jest.fn(),
}
} as unknown as GuildMember;
test.todo("GIVEN member can not be found, EXPECT error");
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;
test.todo("GIVEN member is not manageable, EXPECT error");
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);
expect(member.roles.add).not.toHaveBeenCalled();
});
test("GIVEN member is not manageable, EXPECT error", async () => {
member = {
manageable: false,
roles: {
add: jest.fn(),
}
} as unknown as GuildMember;
interaction.guild!.members.cache.find = jest.fn().mockReturnValue(member);
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);
expect(member.roles.add).not.toHaveBeenCalled();
});
});

View file

@ -1,7 +1,70 @@
import Button from "../../../src/client/interactionCreate/button";
import { CoreClient } from "../../../src/client/client";
import ButtonEventItem from "../../../src/contracts/ButtonEventItem";
import { ButtonInteraction } from "discord.js";
import { ButtonEvent } from "../../../src/type/buttonEvent";
describe('onButtonClicked', () => {
test.todo("EXPECT button event to be executed");
let item: ButtonEventItem;
let interaction: ButtonInteraction;
test.todo("GIVEN interaction is not a button, EXEPCT nothing to happen");
beforeEach(() => {
item = {
ButtonId: "buttonId",
Event: {
execute: jest.fn(),
} as unknown as ButtonEvent,
} as unknown as ButtonEventItem;
test.todo("GIVEN button event is not registered, EXPECT error");
interaction = {
reply: jest.fn(),
isButton: true,
customId: "buttonId test",
} as unknown as ButtonInteraction;
CoreClient.buttonEvents = [ item ];
});
test("EXPECT button event to be executed", async () => {
await Button.onButtonClicked(interaction);
expect(item.Event.execute).toHaveBeenCalledTimes(1);
expect(item.Event.execute).toHaveBeenCalledWith(interaction);
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN interaction is not a button, EXEPCT nothing to happen", async () => {
interaction = {
reply: jest.fn(),
isButton: false,
} as unknown as ButtonInteraction;
await Button.onButtonClicked(interaction);
expect(item.Event.execute).not.toHaveBeenCalled();
expect(interaction.reply).not.toHaveBeenCalled();
});
test("GIVEN no button event is registered, EXPECT error", async () => {
CoreClient.buttonEvents = [];
await Button.onButtonClicked(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Event not found.");
expect(item.Event.execute).not.toHaveBeenCalled();
});
test("GIVEN button event is not registered, EXPECT error", async () => {
interaction.customId = "anotherButtonId test";
await Button.onButtonClicked(interaction);
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Event not found.");
expect(item.Event.execute).not.toHaveBeenCalled();
});
});