Add help command tests

This commit is contained in:
Ethan Lane 2022-01-02 14:56:08 +00:00
parent 6994a339b6
commit 451bfe1039
Signed by: Vylpes
GPG key ID: EED233CC06D12504
5 changed files with 83 additions and 16 deletions

View file

@ -2,4 +2,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ["./jest.setup.js"]
};

3
jest.setup.js Normal file
View file

@ -0,0 +1,3 @@
jest.setTimeout(1 * 1000); // 1 second
jest.resetModules();
jest.resetAllMocks();

View file

@ -30,7 +30,7 @@ export default class Help extends Command {
public SendAll(context: ICommandContext): ICommandReturnContext {
const allCommands = this.GetAllCommandData();
const cateogries = this.DetermineCategories(allCommands);
const cateogries = [...new Set(allCommands.map(x => x.Category!))];;
const embed = new PublicEmbed(context, "Commands", "");
@ -119,16 +119,4 @@ export default class Help extends Command {
return data;
}
public DetermineCategories(commands: ICommandData[]): string[] {
const result: string[] = [];
commands.forEach(cmd => {
if (!result.includes(cmd.Category!)) {
result.push(cmd.Category!);
}
});
return result;
}
}

View file

@ -0,0 +1,10 @@
import { Command } from "../../../src/type/command";
export default class MockCmd extends Command {
constructor() {
super();
super._category = "General";
super._roles = ["Moderator"];
}
}

View file

@ -2,6 +2,8 @@ import Help, { ICommandData } from "../../src/commands/help";
import { Message } from "discord.js";
import { ICommandContext } from "../../src/contracts/ICommandContext";
const oldCwd = process.cwd();
describe('Constructor', () => {
test('Expect properties to be set', () => {
const help = new Help();
@ -86,13 +88,10 @@ describe('SendAll', () => {
help.GetAllCommandData = jest.fn()
.mockReturnValue([commandData0, commandData1]);
help.DetermineCategories = jest.fn()
.mockReturnValue(['general']);
const result = help.SendAll(context);
expect(help.GetAllCommandData).toBeCalled();
expect(help.DetermineCategories).toBeCalled();
expect(messageChannelSend).toBeCalled();
expect(result.embeds.length).toBe(1);
@ -200,3 +199,69 @@ describe('SendSingle', () => {
expect(errorEmbed.description).toBe('Command does not exist');
});
});
describe('GetAllCommandData', () => {
test('Expect array of command data to be returned', () => {
process.env = {
FOLDERS_COMMANDS: "commands"
};
process.cwd = jest.fn()
.mockReturnValue(`${oldCwd}/tests/_mocks`);
const help = new Help();
const result = help.GetAllCommandData();
expect(result.length).toBe(1);
// Mock Command
const mockCommand = result[0];
expect(mockCommand.Exists).toBeTruthy();
expect(mockCommand.Name).toBe("mockCmd");
expect(mockCommand.Category).toBe("General");
expect(mockCommand.Roles!.length).toBe(1);
expect(mockCommand.Roles![0]).toBe("Moderator");
});
});
describe('GetCommandData', () => {
test('Given command exists, expect data to be returned', () => {
process.env = {
FOLDERS_COMMANDS: "commands"
};
process.cwd = jest.fn()
.mockReturnValue(`${oldCwd}/tests/_mocks`);
const help = new Help();
const result = help.GetCommandData('mockCmd');
expect(result.Exists).toBeTruthy();
expect(result.Name).toBe("mockCmd");
expect(result.Category).toBe("General");
expect(result.Roles!.length).toBe(1);
expect(result.Roles![0]).toBe("Moderator");
});
test('Given command does not exist, expect exists false to be returned', () => {
process.env = {
FOLDERS_COMMANDS: "commands"
};
const oldCwd = process.cwd();
process.cwd = jest.fn()
.mockReturnValue(`${oldCwd}/tests/_mocks`);
const help = new Help();
const result = help.GetCommandData('none');
expect(result.Exists).toBeFalsy();
});
});