Fix broken tests

This commit is contained in:
Ethan Lane 2022-03-27 10:12:19 +01:00
parent f0b8025ebc
commit 29b07e35c5
10 changed files with 398 additions and 263 deletions

View file

@ -1,6 +0,0 @@
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ["./jest.setup.js"]
};

5
jest.config.json Normal file
View file

@ -0,0 +1,5 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"setupFiles": ["./jest.setup.js"]
}

View file

@ -39,10 +39,14 @@ export class CoreClient extends Client {
} }
public async start() { public async start() {
if (!process.env.BOT_TOKEN) throw "BOT_TOKEN is not defined in .env"; if (!process.env.BOT_TOKEN) {
console.error("BOT_TOKEN is not defined in .env");
return;
}
await createConnection().catch(e => { await createConnection().catch(e => {
throw e; console.error(e);
return;
}); });
super.on("message", (message) => this._events.onMessage(message, this._commandItems)); super.on("message", (message) => this._events.onMessage(message, this._commandItems));

View file

@ -1,3 +1,44 @@
import { mock } from "jest-mock-extended";
const connectionMock = mock<Connection>();
const qbuilderMock = mock<SelectQueryBuilder<any>>();
let repositoryMock = mock<Repository<any>>();
let settingMock = mock<Setting>();
jest.mock('typeorm', () => {
qbuilderMock.where.mockReturnThis();
qbuilderMock.select.mockReturnThis();
repositoryMock.createQueryBuilder.mockReturnValue(qbuilderMock);
repositoryMock.findOne.mockImplementation(async () => {
return settingMock;
});
connectionMock.getRepository.mockReturnValue(repositoryMock);
return {
getConnection: () => connectionMock,
createConnection: () => connectionMock,
BaseEntity: class Mock {},
ObjectType: () => {},
Entity: () => {},
InputType: () => {},
Index: () => {},
PrimaryColumn: () => {},
Column: () => {},
CreateDateColumn: () => {},
UpdateDateColumn: () => {},
OneToMany: () => {},
ManyToOne: () => {},
}
});
jest.mock("discord.js");
jest.mock("dotenv");
jest.mock("../../src/client/events");
jest.mock("../../src/client/util");
jest.mock("../../src/constants/DefaultValues");
import { CoreClient } from "../../src/client/client"; import { CoreClient } from "../../src/client/client";
import { Client } from "discord.js"; import { Client } from "discord.js";
@ -5,139 +46,84 @@ import * as dotenv from "dotenv";
import { Events } from "../../src/client/events"; import { Events } from "../../src/client/events";
import { Util } from "../../src/client/util"; import { Util } from "../../src/client/util";
import { Command } from "../../src/type/command"; import { Command } from "../../src/type/command";
import { mock } from "jest-mock-extended";
import { Event } from "../../src/type/event"; import { Event } from "../../src/type/event";
import DefaultValues from "../../src/constants/DefaultValues";
import { Connection, Repository, SelectQueryBuilder } from "typeorm";
import Setting from "../../src/entity/Setting";
jest.mock("discord.js"); beforeEach(() => {
jest.mock("dotenv"); jest.resetAllMocks();
jest.mock("../../src/client/events"); jest.resetModules();
jest.mock("../../src/client/util"); })
describe('Constructor', () => { describe('Constructor', () => {
test('Constructor_ExpectSuccessfulInitialisation', () => { test('Expect Successful Initialisation', () => {
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(coreClient).toBeInstanceOf(Client); expect(coreClient).toBeInstanceOf(Client);
expect(dotenv.config).toBeCalledTimes(1); expect(dotenv.config).toBeCalledTimes(1);
expect(Events).toBeCalledTimes(1); expect(Events).toBeCalledTimes(1);
expect(Util).toBeCalledTimes(1); expect(Util).toBeCalledTimes(1);
expect(DefaultValues.useDevPrefix).toBe(false);
});
test('Given devmode parameter is true, Expect devmode prefix to be true', () => {
const coreClient = new CoreClient(true);
expect(coreClient).toBeInstanceOf(Client);
expect(dotenv.config).toBeCalledTimes(1);
expect(Events).toBeCalledTimes(1);
expect(Util).toBeCalledTimes(1);
expect(DefaultValues.useDevPrefix).toBe(true);
}); });
}); });
describe('Start', () => { describe('Start', () => {
test('Given Env Is Valid, Expect Successful Start', () => { test('Given Env Is Valid, Expect Successful Start', async () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: "TOKEN",
BOT_PREFIX: '!', };
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).not.toThrow(); await coreClient.start();
expect(coreClient.on).toBeCalledWith("message", expect.any(Function)); expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function)); expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
}); });
test('Given BOT_TOKEN Is Null, Expect Failure', () => { test('Given BOT_TOKEN Is Null, Expect Failure', async () => {
process.env = { process.env = {};
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands', const consoleError = jest.fn();
FOLDERS_EVENTS: 'events',
} console.error = consoleError;
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env"); await coreClient.start();
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
expect(coreClient.on).not.toBeCalled();
expect(coreClient.login).not.toBeCalled();
}); });
test('Given BOT_TOKEN Is Empty, Expect Failure', () => { test('Given BOT_TOKEN Is Empty, Expect Failure', async () => {
process.env = { process.env = {
BOT_TOKEN: '', BOT_TOKEN: '',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
} }
const consoleError = jest.fn();
console.error = consoleError;
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env"); await coreClient.start();
});
test('Given BOT_PREFIX Is Null, Expect Failure', () => { expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
process.env = { expect(coreClient.on).not.toBeCalled();
BOT_TOKEN: 'TOKEN', expect(coreClient.login).not.toBeCalled();
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
});
test('Given BOT_PREFIX Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
});
test('Given FOLDERS_COMMANDS Is Null, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_EVENTS: 'events',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
});
test('Given FOLDERS_COMMANDS Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: '',
FOLDERS_EVENTS: 'events',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
});
test('Given FOLDERS_EVENTS Is Null, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
});
test('Given FOLDERS_EVENTS Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: '',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
}); });
}); });

View file

@ -15,7 +15,7 @@ beforeEach(() => {
}); });
describe('LoadCommand', () => { describe('LoadCommand', () => {
test('Given Successful Exection, Expect Successful Result', () => { test('Given Successful Exection, Expect Successful Result', async () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -45,13 +45,13 @@ describe('LoadCommand', () => {
const util = new Util(); const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands); const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeTruthy(); expect(result.valid).toBeTruthy();
expect(cmd.execute).toBeCalled(); expect(cmd.execute).toBeCalled();
}); });
test('Given Member Is Null, Expect Failed Result', () => { test('Given Member Is Null, Expect Failed Result', async () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -74,14 +74,14 @@ describe('LoadCommand', () => {
const util = new Util(); const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands); const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Member is not part of message"); expect(result.message).toBe("Member is not part of message");
expect(cmd.execute).not.toBeCalled(); expect(cmd.execute).not.toBeCalled();
}); });
test('Given User Does Have Role, Expect Successful Result', () => { test('Given User Does Have Role, Expect Successful Result', async () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -111,13 +111,13 @@ describe('LoadCommand', () => {
const util = new Util(); const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands); const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeTruthy(); expect(result.valid).toBeTruthy();
expect(cmd.execute).toBeCalled(); expect(cmd.execute).toBeCalled();
}); });
test('Given User Does Not Have Role, Expect Failed Result', () => { test('Given User Does Not Have Role, Expect Failed Result', async () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -148,14 +148,14 @@ describe('LoadCommand', () => {
const util = new Util(); const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands); const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("You require the `Moderator` role to run this command"); expect(result.message).toBe("You require the `Moderator` role to run this command");
expect(cmd.execute).not.toBeCalled(); expect(cmd.execute).not.toBeCalled();
}); });
test('Given command is set to disabled, Expect command to not fire', () => { test('Given command is set to disabled, Expect command to not fire', async () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -189,7 +189,7 @@ describe('LoadCommand', () => {
const util = new Util(); const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands); const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled"); expect(result.message).toBe("Command is disabled");
@ -197,7 +197,7 @@ describe('LoadCommand', () => {
expect(cmd.execute).not.toBeCalled(); expect(cmd.execute).not.toBeCalled();
}); });
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', () => { test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', async () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -230,7 +230,7 @@ describe('LoadCommand', () => {
const util = new Util(); const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands); const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled"); expect(result.message).toBe("Command is disabled");
@ -238,7 +238,7 @@ describe('LoadCommand', () => {
expect(cmd.execute).not.toBeCalled(); expect(cmd.execute).not.toBeCalled();
}); });
test('Given a different command is disabled, Expect command to still fire', () => { test('Given a different command is disabled, Expect command to still fire', async () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -275,14 +275,14 @@ describe('LoadCommand', () => {
const util = new Util(); const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands); const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeTruthy(); expect(result.valid).toBeTruthy();
expect(cmd.execute).toBeCalled(); expect(cmd.execute).toBeCalled();
expect(otherCmd.execute).not.toBeCalled(); expect(otherCmd.execute).not.toBeCalled();
}); });
test('Given command is not found in register, expect command not found error', () => { test('Given command is not found in register, expect command not found error', async () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -305,7 +305,7 @@ describe('LoadCommand', () => {
const util = new Util(); const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands); const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe('Command not found'); expect(result.message).toBe('Command not found');

View file

@ -3,7 +3,7 @@ import MemberEvents from "../../src/events/MemberEvents";
import GuildMemberUpdate from "../../src/events/MemberEvents/GuildMemberUpdate"; import GuildMemberUpdate from "../../src/events/MemberEvents/GuildMemberUpdate";
describe('GuildMemberAdd', () => { describe('GuildMemberAdd', () => {
test('When event is fired, expect embed to be sent to logs channel', () => { test('When event is fired, expect embed to be sent to logs channel', async () => {
const currentDate = new Date(); const currentDate = new Date();
const textChannel = { const textChannel = {
@ -34,7 +34,7 @@ describe('GuildMemberAdd', () => {
const memberEvents = new MemberEvents(); const memberEvents = new MemberEvents();
const result = memberEvents.guildMemberAdd(guildMember); const result = await memberEvents.guildMemberAdd(guildMember);
expect(textChannel.send).toBeCalledTimes(1); expect(textChannel.send).toBeCalledTimes(1);
expect(userDisplayAvatarURL).toBeCalledTimes(1); expect(userDisplayAvatarURL).toBeCalledTimes(1);
@ -63,7 +63,7 @@ describe('GuildMemberAdd', () => {
}); });
describe('GuildMemberRemove', () => { describe('GuildMemberRemove', () => {
test('When event is fired, expect embed to be sent to logs channel', () => { test('When event is fired, expect embed to be sent to logs channel', async () => {
const currentDate = new Date(); const currentDate = new Date();
const textChannel = { const textChannel = {
@ -95,7 +95,7 @@ describe('GuildMemberRemove', () => {
const memberEvents = new MemberEvents(); const memberEvents = new MemberEvents();
const result = memberEvents.guildMemberRemove(guildMember); const result = await memberEvents.guildMemberRemove(guildMember);
expect(textChannel.send).toBeCalledTimes(1); expect(textChannel.send).toBeCalledTimes(1);
expect(userDisplayAvatarURL).toBeCalledTimes(1); expect(userDisplayAvatarURL).toBeCalledTimes(1);
@ -124,7 +124,7 @@ describe('GuildMemberRemove', () => {
}); });
describe('GuildMemberUpdate', () => { describe('GuildMemberUpdate', () => {
test('Given nicknames are the same, expect NicknameChanged NOT to be called', () => { test('Given nicknames are the same, expect NicknameChanged NOT to be called', async () => {
const member = { const member = {
nickname: 'member' nickname: 'member'
} as unknown as GuildMember; } as unknown as GuildMember;
@ -135,13 +135,13 @@ describe('GuildMemberUpdate', () => {
const memberEvents = new MemberEvents(); const memberEvents = new MemberEvents();
const result = memberEvents.guildMemberUpdate(member, member); const result = await memberEvents.guildMemberUpdate(member, member);
expect(result.embeds.length).toBe(0); expect(result.embeds.length).toBe(0);
expect(nicknameChanged).not.toBeCalled(); expect(nicknameChanged).not.toBeCalled();
}); });
test('Given nicknames are the different, expect NicknameChanged to be called', () => { test('Given nicknames are the different, expect NicknameChanged to be called', async () => {
const oldMember = { const oldMember = {
nickname: 'oldMember' nickname: 'oldMember'
} as unknown as GuildMember; } as unknown as GuildMember;
@ -156,7 +156,7 @@ describe('GuildMemberUpdate', () => {
const memberEvents = new MemberEvents(); const memberEvents = new MemberEvents();
const result = memberEvents.guildMemberUpdate(oldMember, newMember); const result = await memberEvents.guildMemberUpdate(oldMember, newMember);
expect(result.embeds.length).toBe(0); expect(result.embeds.length).toBe(0);
expect(nicknameChanged).toBeCalledTimes(1); expect(nicknameChanged).toBeCalledTimes(1);

View file

@ -23,7 +23,7 @@ describe('Constructor', () => {
}); });
describe('NicknameChanged', () => { describe('NicknameChanged', () => {
test('Given nickname has changed from one to another, expect embed to be sent with both', () => { test('Given nickname has changed from one to another, expect embed to be sent with both', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -61,7 +61,7 @@ describe('NicknameChanged', () => {
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember); const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
const result = guildMemberUpdate.NicknameChanged(); const result = await guildMemberUpdate.NicknameChanged();
expect(channelSend).toBeCalledTimes(1); expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1); expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
@ -94,7 +94,7 @@ describe('NicknameChanged', () => {
expect(embedFieldAfter.value).toBe('New Nickname'); expect(embedFieldAfter.value).toBe('New Nickname');
}); });
test('Given old nickname was null, expect embed to say old nickname was none', () => { test('Given old nickname was null, expect embed to say old nickname was none', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -130,7 +130,7 @@ describe('NicknameChanged', () => {
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember); const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
const result = guildMemberUpdate.NicknameChanged(); const result = await guildMemberUpdate.NicknameChanged();
expect(channelSend).toBeCalledTimes(1); expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1); expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
@ -163,7 +163,7 @@ describe('NicknameChanged', () => {
expect(embedFieldAfter.value).toBe('New Nickname'); expect(embedFieldAfter.value).toBe('New Nickname');
}); });
test('Given new nickname was null, expect embed to say new nickname was none', () => { test('Given new nickname was null, expect embed to say new nickname was none', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -200,7 +200,7 @@ describe('NicknameChanged', () => {
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember); const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
const result = guildMemberUpdate.NicknameChanged(); const result = await guildMemberUpdate.NicknameChanged();
expect(channelSend).toBeCalledTimes(1); expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1); expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);

View file

@ -6,7 +6,7 @@ beforeEach(() => {
}); });
describe('MessageDelete', () => { describe('MessageDelete', () => {
test('Given message was in a guild AND user was NOT a bot, expect message deleted embed to be sent', () => { test('Given message was in a guild AND user was NOT a bot, expect message deleted embed to be sent', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -57,7 +57,7 @@ describe('MessageDelete', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageDelete(message); const result = await messageEvents.messageDelete(message);
expect(channelSend).toBeCalledTimes(1); expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1); expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
@ -95,7 +95,7 @@ describe('MessageDelete', () => {
expect(embedFieldAttachments.value).toBe('```image0.png\nimage1.png```'); expect(embedFieldAttachments.value).toBe('```image0.png\nimage1.png```');
}); });
test('Given message was not sent in a guild, expect execution stopped', () => { test('Given message was not sent in a guild, expect execution stopped', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -139,7 +139,7 @@ describe('MessageDelete', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageDelete(message); const result = await messageEvents.messageDelete(message);
expect(channelSend).not.toBeCalled(); expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled(); expect(memberGuildChannelsCacheFind).not.toBeCalled();
@ -147,7 +147,7 @@ describe('MessageDelete', () => {
expect(result.embeds.length).toBe(0); expect(result.embeds.length).toBe(0);
}); });
test('Given author is a bot, expect execution stopped', () => { test('Given author is a bot, expect execution stopped', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -198,7 +198,7 @@ describe('MessageDelete', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageDelete(message); const result = await messageEvents.messageDelete(message);
expect(channelSend).not.toBeCalled(); expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled(); expect(memberGuildChannelsCacheFind).not.toBeCalled();
@ -206,7 +206,7 @@ describe('MessageDelete', () => {
expect(result.embeds.length).toBe(0); expect(result.embeds.length).toBe(0);
}); });
test('Given message does not contain any attachments, expect attachments field to be omitted', () => { test('Given message does not contain any attachments, expect attachments field to be omitted', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -244,7 +244,7 @@ describe('MessageDelete', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageDelete(message); const result = await messageEvents.messageDelete(message);
expect(channelSend).toBeCalledTimes(1); expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1); expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
@ -278,7 +278,7 @@ describe('MessageDelete', () => {
}); });
describe('MessageUpdate', () => { describe('MessageUpdate', () => {
test('Given message is in a guild AND user is not a bot AND the content has actually changed, e xpect log embed to be sent', () => { test('Given message is in a guild AND user is not a bot AND the content has actually changed, e xpect log embed to be sent', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -317,7 +317,7 @@ describe('MessageUpdate', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageUpdate(oldMessage, newMessage); const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).toBeCalledTimes(1); expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1); expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
@ -357,7 +357,7 @@ describe('MessageUpdate', () => {
expect(embedFieldAfter.value).toBe('```New Message```'); expect(embedFieldAfter.value).toBe('```New Message```');
}); });
test('Given message was not in a guild, expect execution stopped', () => { test('Given message was not in a guild, expect execution stopped', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -389,7 +389,7 @@ describe('MessageUpdate', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageUpdate(oldMessage, newMessage); const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).not.toBeCalled(); expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled(); expect(memberGuildChannelsCacheFind).not.toBeCalled();
@ -397,7 +397,7 @@ describe('MessageUpdate', () => {
expect(result.embeds.length).toBe(0); expect(result.embeds.length).toBe(0);
}); });
test('Given author is a bot, expect execution stopped', () => { test('Given author is a bot, expect execution stopped', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -436,7 +436,7 @@ describe('MessageUpdate', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageUpdate(oldMessage, newMessage); const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).not.toBeCalled(); expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled(); expect(memberGuildChannelsCacheFind).not.toBeCalled();
@ -444,7 +444,7 @@ describe('MessageUpdate', () => {
expect(result.embeds.length).toBe(0); expect(result.embeds.length).toBe(0);
}); });
test('Given the message contents are the same, expect execution stopped', () => { test('Given the message contents are the same, expect execution stopped', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -483,7 +483,7 @@ describe('MessageUpdate', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageUpdate(oldMessage, newMessage); const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).not.toBeCalled(); expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled(); expect(memberGuildChannelsCacheFind).not.toBeCalled();
@ -491,7 +491,7 @@ describe('MessageUpdate', () => {
expect(result.embeds.length).toBe(0); expect(result.embeds.length).toBe(0);
}); });
test('Given Old Message did not have a content, expect field to account for this', () => { test('Given Old Message did not have a content, expect field to account for this', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -528,7 +528,7 @@ describe('MessageUpdate', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageUpdate(oldMessage, newMessage); const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).toBeCalledTimes(1); expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1); expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
@ -568,7 +568,7 @@ describe('MessageUpdate', () => {
expect(embedFieldAfter.value).toBe('```New Message```'); expect(embedFieldAfter.value).toBe('```New Message```');
}); });
test('Given New Message does not have a content, expect field to account for this', () => { test('Given New Message does not have a content, expect field to account for this', async () => {
process.env = { process.env = {
CHANNELS_LOGS_MOD: 'mod-logs' CHANNELS_LOGS_MOD: 'mod-logs'
} }
@ -606,7 +606,7 @@ describe('MessageUpdate', () => {
const messageEvents = new MessageEvents(); const messageEvents = new MessageEvents();
const result = messageEvents.messageUpdate(oldMessage, newMessage); const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).toBeCalledTimes(1); expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1); expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);

View file

@ -1,6 +1,7 @@
import { Guild, Message, TextChannel, User } from "discord.js"; import { Guild, Message, TextChannel, User } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext"; import { ICommandContext } from "../../../src/contracts/ICommandContext";
import EventEmbed from "../../../src/helpers/embeds/EventEmbed"; import EventEmbed from "../../../src/helpers/embeds/EventEmbed";
import SettingsHelper from "../../../src/helpers/SettingsHelper";
beforeEach(() => { beforeEach(() => {
process.env = {}; process.env = {};
@ -9,18 +10,11 @@ beforeEach(() => {
describe('Constructor', () => { describe('Constructor', () => {
test('Expect properties to be set', () => { test('Expect properties to be set', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const guild = {} as unknown as Guild; const guild = {} as unknown as Guild;
const errorEmbed = new EventEmbed(guild, 'Event Message'); const errorEmbed = new EventEmbed(guild, 'Event Message');
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal expect(errorEmbed.color?.toString()).toBe('3166394'); // 0x3050ba in decimal
expect(errorEmbed.title).toBe('Event Message'); expect(errorEmbed.title).toBe('Event Message');
expect(errorEmbed.guild).toBe(guild); expect(errorEmbed.guild).toBe(guild);
}); });
@ -88,15 +82,34 @@ describe('AddUser', () => {
}); });
}); });
describe('AddReason', () => {
test('Given a non-empty string is supplied, expect field with message', () => {
const guild = {} as Guild;
const eventEmbed = new EventEmbed(guild, "Event Embed");
eventEmbed.addField = jest.fn();
eventEmbed.AddReason("Test reason");
expect(eventEmbed.addField).toBeCalledWith("Reason", "Test reason");
});
test('Given an empty string is supplied, expect field with default message', () => {
const guild = {} as Guild;
const eventEmbed = new EventEmbed(guild, "Event Embed");
eventEmbed.addField = jest.fn();
eventEmbed.AddReason("");
expect(eventEmbed.addField).toBeCalledWith("Reason", "*none*");
});
});
describe('SendToChannel', () => { describe('SendToChannel', () => {
test('Given channel can be found, expect embed to be sent to that channel', () => { test('Given channel can be found, expect embed to be sent to that channel', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn(); const channelSend = jest.fn();
const channel = { const channel = {
@ -153,70 +166,127 @@ describe('SendToChannel', () => {
}); });
describe('SendToMessageLogsChannel', () => { describe('SendToMessageLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MESSAGE as parameter', () => { test('Given setting is set, expect SendToChannel to be called with value', async () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn(); const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("message-logs");
const guild = {} as unknown as Guild; const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message'); const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel; errorEmbed.SendToChannel = sendToChannel;
errorEmbed.SendToMessageLogsChannel(); await errorEmbed.SendToMessageLogsChannel();
expect(sendToChannel).toBeCalledWith('message-logs'); expect(sendToChannel).toBeCalledWith('message-logs');
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
await errorEmbed.SendToMessageLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
}); });
}); });
describe('SendToMemberLogsChannel', () => { describe('SendToMemberLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MEMBER as parameter', () => { test('Given setting is set, expect SendToChannel to be called with value', async () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn(); const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("member-logs");
const guild = {} as unknown as Guild; const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message'); const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel; errorEmbed.SendToChannel = sendToChannel;
errorEmbed.SendToMemberLogsChannel(); await errorEmbed.SendToMemberLogsChannel();
expect(sendToChannel).toBeCalledWith('member-logs'); expect(sendToChannel).toBeCalledWith('member-logs');
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
await errorEmbed.SendToMemberLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
}); });
}); });
describe('SendToModLogsChannel', () => { describe('SendToModLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MOD as parameter', () => { test('Given setting is set, expect SendToChannel to be called with value', async () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn(); const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("mod-logs");
const guild = {} as unknown as Guild; const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message'); const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel; errorEmbed.SendToChannel = sendToChannel;
errorEmbed.SendToModLogsChannel(); await errorEmbed.SendToModLogsChannel();
expect(sendToChannel).toBeCalledWith('mod-logs'); expect(sendToChannel).toBeCalledWith('mod-logs');
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
SettingsHelper.GetSetting = getSetting;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
await errorEmbed.SendToModLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
}); });
}); });

View file

@ -1,6 +1,7 @@
import { Guild, Message, TextChannel, User } from "discord.js"; import { Guild, Message, TextChannel, User } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext"; import { ICommandContext } from "../../../src/contracts/ICommandContext";
import LogEmbed from "../../../src/helpers/embeds/LogEmbed"; import LogEmbed from "../../../src/helpers/embeds/LogEmbed";
import SettingsHelper from "../../../src/helpers/SettingsHelper";
beforeEach(() => { beforeEach(() => {
process.env = {}; process.env = {};
@ -32,7 +33,7 @@ describe('Constructor', () => {
const errorEmbed = new LogEmbed(context, 'Log Message'); const errorEmbed = new LogEmbed(context, 'Log Message');
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal expect(errorEmbed.color?.toString()).toBe('3166394'); // 0x3050ba in decimal
expect(errorEmbed.title).toBe('Log Message'); expect(errorEmbed.title).toBe('Log Message');
expect(errorEmbed.context).toBe(context); expect(errorEmbed.context).toBe(context);
}); });
@ -220,112 +221,187 @@ describe('SendToChannel', () => {
}); });
describe('SendToMessageLogsChannel', () => { describe('SendToMessageLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MESSAGE as parameter', () => { test('Given setting is set, expect SendToChannel to be called with value', async () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn(); const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("message-logs");
const guild = {} as unknown as Guild; const guild = {
id: "guildId"
const messageChannelSend = jest.fn(); } as unknown as Guild;
const message = { const message = {
channel: { guild: guild
send: messageChannelSend } as Message;
}
} as unknown as Message;
const context: ICommandContext = { const context: ICommandContext = {
name: 'command', name: 'log',
args: [], args: [],
message: message message: message
}; };
const errorEmbed = new LogEmbed(context, 'Event Message'); SettingsHelper.GetSetting = getSetting;
errorEmbed.SendToChannel = sendToChannel; const logEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToMessageLogsChannel(); logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToMessageLogsChannel();
expect(sendToChannel).toBeCalledWith('message-logs'); expect(sendToChannel).toBeCalledWith('message-logs');
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
const message = {
guild: guild
} as Message;
const context: ICommandContext = {
name: 'log',
args: [],
message: message
};
SettingsHelper.GetSetting = getSetting;
const logEmbed = new LogEmbed(context, 'Event Message');
logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToMessageLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
}); });
}); });
describe('SendToMemberLogsChannel', () => { describe('SendToMemberLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MEMBER as parameter', () => { test('Given setting is set, expect SendToChannel to be called with value', async () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn(); const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("member-logs");
const guild = {} as unknown as Guild; const guild = {
id: "guildId"
const messageChannelSend = jest.fn(); } as unknown as Guild;
const message = { const message = {
channel: { guild: guild
send: messageChannelSend } as Message;
}
} as unknown as Message;
const context: ICommandContext = { const context: ICommandContext = {
name: 'command', name: 'log',
args: [], args: [],
message: message message: message
}; };
const errorEmbed = new LogEmbed(context, 'Event Message'); SettingsHelper.GetSetting = getSetting;
errorEmbed.SendToChannel = sendToChannel; const logEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToMemberLogsChannel(); logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToMemberLogsChannel();
expect(sendToChannel).toBeCalledWith('member-logs'); expect(sendToChannel).toBeCalledWith('member-logs');
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
const message = {
guild: guild
} as Message;
const context: ICommandContext = {
name: 'log',
args: [],
message: message
};
SettingsHelper.GetSetting = getSetting;
const logEmbed = new LogEmbed(context, 'Event Message');
logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToMemberLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
}); });
}); });
describe('SendToModLogsChannel', () => { describe('SendToModLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MOD as parameter', () => { test('Given setting is set, expect SendToChannel to be called with value', async () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn(); const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue("mod-logs");
const guild = {} as unknown as Guild; const guild = {
id: "guildId"
const messageChannelSend = jest.fn(); } as unknown as Guild;
const message = { const message = {
channel: { guild: guild
send: messageChannelSend } as Message;
}
} as unknown as Message;
const context: ICommandContext = { const context: ICommandContext = {
name: 'command', name: 'log',
args: [], args: [],
message: message message: message
}; };
const errorEmbed = new LogEmbed(context, 'Event Message'); SettingsHelper.GetSetting = getSetting;
errorEmbed.SendToChannel = sendToChannel; const logEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToModLogsChannel(); logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToModLogsChannel();
expect(sendToChannel).toBeCalledWith('mod-logs'); expect(sendToChannel).toBeCalledWith('mod-logs');
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
});
test('Given setting is not set, expect function to return', async () => {
const sendToChannel = jest.fn();
const getSetting = jest.fn().mockResolvedValue(undefined);
const guild = {
id: "guildId"
} as unknown as Guild;
const message = {
guild: guild
} as Message;
const context: ICommandContext = {
name: 'log',
args: [],
message: message
};
SettingsHelper.GetSetting = getSetting;
const logEmbed = new LogEmbed(context, 'Event Message');
logEmbed.SendToChannel = sendToChannel;
await logEmbed.SendToModLogsChannel();
expect(sendToChannel).not.toBeCalled();
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
}); });
}); });