Improvements to Tests

This commit is contained in:
Ethan Lane 2021-08-20 15:53:34 +01:00
parent 338514ba1e
commit f11f3954a1
3 changed files with 96 additions and 1 deletions

View file

@ -1,5 +1,8 @@
import { Command } from "../../../src/type/command"; import { Command } from "../../../src/type/command";
export class name extends Command { export class name extends Command {
constructor() {
super();
this._category = "General";
}
} }

View file

@ -0,0 +1,8 @@
import { Command } from "../../../src/type/command";
export class noCategory extends Command {
constructor() {
super();
}
}

View file

@ -122,6 +122,35 @@ test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => {
expect(result.message).toBe("File does not exist"); expect(result.message).toBe("File does not exist");
}); });
test('LoadCommand_GivenUserDoesHaveRole_ExpectSuccessfulResult', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
const result = util.loadCommand("roles", [ "first" ], message);
expect(result.valid).toBeTruthy();
});
test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => { test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
@ -152,6 +181,35 @@ test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
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', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
const result = util.loadCommand("noCategory", [ "first" ], message);
expect(result.valid).toBeTruthy();
});
test('LoadEvents_GivenEventsAreLoaded_ExpectSuccessfulResult', () => { test('LoadEvents_GivenEventsAreLoaded_ExpectSuccessfulResult', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',
@ -178,6 +236,32 @@ test('LoadEvents_GivenEventsAreLoaded_ExpectSuccessfulResult', () => {
expect(clientOn).toBeCalledTimes(13); expect(clientOn).toBeCalledTimes(13);
}); });
test('LoadEvents_GivenNoEventsFound_ExpectSuccessfulResultStill', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
fs.readdirSync = jest.fn().mockReturnValue(["name"]);
const client = {
on: jest.fn(),
} as unknown as Client;
const util = new Util();
const result = util.loadEvents(client);
const clientOn = jest.spyOn(client, 'on');
expect(result.valid).toBeTruthy();
expect(clientOn).toBeCalledTimes(0);
});
test('LoadEvents_GivenEventFolderDoesNotExist_FailedResult', () => { test('LoadEvents_GivenEventFolderDoesNotExist_FailedResult', () => {
process.env = { process.env = {
BOT_TOKEN: 'TOKEN', BOT_TOKEN: 'TOKEN',