Update command names, add describe categories

This commit is contained in:
Ethan Lane 2021-08-21 15:33:14 +01:00
parent bb848dcec3
commit 7e623b4b64
3 changed files with 548 additions and 536 deletions

View file

@ -10,16 +10,19 @@ jest.mock("dotenv");
jest.mock("../../src/client/events"); jest.mock("../../src/client/events");
jest.mock("../../src/client/util"); jest.mock("../../src/client/util");
test('Constructor_ExpectSuccessfulInitialisation', () => { describe('Constructor', () => {
test('Constructor_ExpectSuccessfulInitialisation', () => {
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);
});
}); });
test('Start_GivenEnvIsValid_ExpectSuccessfulStart', () => { describe('Start', () => {
test('Given Env Is Valid, Expect Successful Start', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -32,9 +35,9 @@ test('Start_GivenEnvIsValid_ExpectSuccessfulStart', () => {
expect(() => coreClient.start()).not.toThrow(); expect(() => coreClient.start()).not.toThrow();
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('Start_GivenBotTokenIsNull_ExpectFailure', () => { test('Given BOT_TOKEN Is Null, Expect Failure', () => {
process.env = { process.env = {
BOT_PREFIX: '!', BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands', FOLDERS_COMMANDS: 'commands',
@ -44,9 +47,9 @@ test('Start_GivenBotTokenIsNull_ExpectFailure', () => {
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env"); expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
}); });
test('Start_GivenBotTokenIsEmpty_ExpectFailure', () => { test('Given BOT_TOKEN Is Empty, Expect Failure', () => {
process.env = { process.env = {
BOT_TOKEN: '', BOT_TOKEN: '',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -57,9 +60,9 @@ test('Start_GivenBotTokenIsEmpty_ExpectFailure', () => {
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env"); expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
}); });
test('Start_GivenBotPrefixIsNull_ExpectFailure', () => { test('Given BOT_PREFIX Is Null, Expect Failure', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
FOLDERS_COMMANDS: 'commands', FOLDERS_COMMANDS: 'commands',
@ -69,9 +72,9 @@ test('Start_GivenBotPrefixIsNull_ExpectFailure', () => {
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env"); expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
}); });
test('Start_GivenBotPrefixIsEmpty_ExpectFailure', () => { test('Given BOT_PREFIX Is Empty, Expect Failure', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '', BOT_PREFIX: '',
@ -82,9 +85,9 @@ test('Start_GivenBotPrefixIsEmpty_ExpectFailure', () => {
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env"); expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
}); });
test('Start_GivenFoldersCommandsIsNull_ExpectFailure', () => { test('Given FOLDERS_COMMANDS Is Null, Expect Failure', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -94,9 +97,9 @@ test('Start_GivenFoldersCommandsIsNull_ExpectFailure', () => {
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env"); expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
}); });
test('Start_GivenFoldersCommandsIsEmpty_ExpectFailure', () => { test('Given FOLDERS_COMMANDS Is Empty, Expect Failure', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -107,9 +110,9 @@ test('Start_GivenFoldersCommandsIsEmpty_ExpectFailure', () => {
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env"); expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
}); });
test('Start_GivenFoldersEventsIsNull_ExpectFailure', () => { test('Given FOLDERS_EVENTS Is Null, Expect Failure', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -119,9 +122,9 @@ test('Start_GivenFoldersEventsIsNull_ExpectFailure', () => {
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env"); expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
}); });
test('Start_GivenFoldersCommandsIsEmpty_ExpectFailure', () => { test('Given FOLDERS_EVENTS Is Empty, Expect Failure', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -132,4 +135,5 @@ test('Start_GivenFoldersCommandsIsEmpty_ExpectFailure', () => {
const coreClient = new CoreClient(); const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env"); expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
});
}); });

View file

@ -9,7 +9,8 @@ beforeEach(() => {
Util.prototype.loadCommand = jest.fn(); Util.prototype.loadCommand = jest.fn();
}); });
test('OnMessage_GivenMessageIsValid_ExpectMessageSent', () => { describe('OnMessage', () => {
test('Given Message Is Valid Expect Message Sent', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -38,9 +39,9 @@ test('OnMessage_GivenMessageIsValid_ExpectMessageSent', () => {
expect(result.context?.args.length).toBe(1); expect(result.context?.args.length).toBe(1);
expect(result.context?.args[0]).toBe('first'); expect(result.context?.args[0]).toBe('first');
expect(result.context?.message).toBe(message); expect(result.context?.message).toBe(message);
}); });
test('OnMessage_GivenGuildIsNull_ExpectFailedResult', () => { test('Given Guild Is Null, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -64,9 +65,9 @@ test('OnMessage_GivenGuildIsNull_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was not sent in a guild, ignoring."); expect(result.message).toBe("Message was not sent in a guild, ignoring.");
}); });
test('OnMessage_GivenAuthorIsBot_ExpectFailedResult', () => { test('Given Author Is A Bot, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -90,9 +91,9 @@ test('OnMessage_GivenAuthorIsBot_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was sent by a bot, ignoring."); expect(result.message).toBe("Message was sent by a bot, ignoring.");
}); });
test('OnMessage_GivenMessageContentsWasNotACommand_ExpectFailedResult', () => { test('Given Message Content Was Not A Command, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -116,9 +117,9 @@ test('OnMessage_GivenMessageContentsWasNotACommand_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was not a command, ignoring."); expect(result.message).toBe("Message was not a command, ignoring.");
}); });
test('OnMessage_GivenMessageHadNoCommandName_ExpectFailedResult', () => { test('Given Message Had No Command Name, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -142,9 +143,9 @@ test('OnMessage_GivenMessageHadNoCommandName_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command name was not found"); expect(result.message).toBe("Command name was not found");
}); });
test('OnMessage_GivenCommandFailedToExecute_ExpectFailedResult', () => { test('Given Command Failed To Execute, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -168,9 +169,11 @@ test('OnMessage_GivenCommandFailedToExecute_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command failed"); expect(result.message).toBe("Command failed");
});
}); });
test('OnReady_ExpectConsoleLog', () => { describe('OnReady', () => {
test('Expect Console Log', () => {
console.log = jest.fn(); console.log = jest.fn();
const events = new Events(); const events = new Events();
@ -178,4 +181,5 @@ test('OnReady_ExpectConsoleLog', () => {
events.onReady(); events.onReady();
expect(console.log).toBeCalledWith("Ready"); expect(console.log).toBeCalledWith("Ready");
});
}); });

View file

@ -9,7 +9,8 @@ beforeEach(() => {
fs.existsSync = jest.fn(); fs.existsSync = jest.fn();
}); });
test('LoadCommand_GivenSuccessfulExection_ExpectSuccessfulResult', () => { describe('LoadCommand', () => {
test('Given Successful Exection, Expect Successful Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -36,9 +37,9 @@ test('LoadCommand_GivenSuccessfulExection_ExpectSuccessfulResult', () => {
const result = util.loadCommand("normal", [ "first" ], message); const result = util.loadCommand("normal", [ "first" ], message);
expect(result.valid).toBeTruthy(); expect(result.valid).toBeTruthy();
}); });
test('LoadCommand_GivenMemberIsNull_ExpectFailedResult', () => { test('Given Member Is Null, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -59,9 +60,9 @@ test('LoadCommand_GivenMemberIsNull_ExpectFailedResult', () => {
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");
}); });
test('LoadCommand_GivenFolderDoesNotExist_ExpectFailedResult', () => { test('Given Folder Does Not Exist, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -89,9 +90,9 @@ test('LoadCommand_GivenFolderDoesNotExist_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command folder does not exist"); expect(result.message).toBe("Command folder does not exist");
}); });
test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => { test('Given File Does Not Exist, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -120,9 +121,9 @@ test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("File does not exist"); expect(result.message).toBe("File does not exist");
}); });
test('LoadCommand_GivenUserDoesHaveRole_ExpectSuccessfulResult', () => { test('Given User Does Have Role, Expect Successful Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -149,9 +150,9 @@ test('LoadCommand_GivenUserDoesHaveRole_ExpectSuccessfulResult', () => {
const result = util.loadCommand("roles", [ "first" ], message); const result = util.loadCommand("roles", [ "first" ], message);
expect(result.valid).toBeTruthy(); expect(result.valid).toBeTruthy();
}); });
test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => { test('Given User Does Not Have Role, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -179,9 +180,9 @@ test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
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");
}); });
test('LoadCommand_GivenCommandCategoryIsNull_ExpectSuccessfulResultStill', () => { test('Given Command Category Is Null, Expect Successful Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -208,9 +209,11 @@ test('LoadCommand_GivenCommandCategoryIsNull_ExpectSuccessfulResultStill', () =>
const result = util.loadCommand("noCategory", [ "first" ], message); const result = util.loadCommand("noCategory", [ "first" ], message);
expect(result.valid).toBeTruthy(); expect(result.valid).toBeTruthy();
});
}); });
test('LoadEvents_GivenEventsAreLoaded_ExpectSuccessfulResult', () => { describe('LoadEvents', () => {
test('Given Events Are Loaded, Expect Successful Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -234,9 +237,9 @@ test('LoadEvents_GivenEventsAreLoaded_ExpectSuccessfulResult', () => {
expect(result.valid).toBeTruthy(); expect(result.valid).toBeTruthy();
expect(clientOn).toBeCalledTimes(13); expect(clientOn).toBeCalledTimes(13);
}); });
test('LoadEvents_GivenNoEventsFound_ExpectSuccessfulResultStill', () => { test('Given No Events Found, Expect Successful Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -260,9 +263,9 @@ test('LoadEvents_GivenNoEventsFound_ExpectSuccessfulResultStill', () => {
expect(result.valid).toBeTruthy(); expect(result.valid).toBeTruthy();
expect(clientOn).toBeCalledTimes(0); expect(clientOn).toBeCalledTimes(0);
}); });
test('LoadEvents_GivenEventFolderDoesNotExist_FailedResult', () => { test('Given Event Folder Does Not Exist, Expect Failed Result', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!', BOT_PREFIX: '!',
@ -284,4 +287,5 @@ test('LoadEvents_GivenEventFolderDoesNotExist_FailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Event folder does not exist"); expect(result.message).toBe("Event folder does not exist");
});
}); });