Feature/12 create tests #102
5 changed files with 83 additions and 16 deletions
|
@ -2,4 +2,5 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
preset: 'ts-jest',
|
preset: 'ts-jest',
|
||||||
testEnvironment: 'node',
|
testEnvironment: 'node',
|
||||||
|
setupFiles: ["./jest.setup.js"]
|
||||||
};
|
};
|
3
jest.setup.js
Normal file
3
jest.setup.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
jest.setTimeout(1 * 1000); // 1 second
|
||||||
|
jest.resetModules();
|
||||||
|
jest.resetAllMocks();
|
|
@ -30,7 +30,7 @@ export default class Help extends Command {
|
||||||
|
|
||||||
public SendAll(context: ICommandContext): ICommandReturnContext {
|
public SendAll(context: ICommandContext): ICommandReturnContext {
|
||||||
const allCommands = this.GetAllCommandData();
|
const allCommands = this.GetAllCommandData();
|
||||||
const cateogries = this.DetermineCategories(allCommands);
|
const cateogries = [...new Set(allCommands.map(x => x.Category!))];;
|
||||||
|
|
||||||
const embed = new PublicEmbed(context, "Commands", "");
|
const embed = new PublicEmbed(context, "Commands", "");
|
||||||
|
|
||||||
|
@ -119,16 +119,4 @@ export default class Help extends Command {
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DetermineCategories(commands: ICommandData[]): string[] {
|
|
||||||
const result: string[] = [];
|
|
||||||
|
|
||||||
commands.forEach(cmd => {
|
|
||||||
if (!result.includes(cmd.Category!)) {
|
|
||||||
result.push(cmd.Category!);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
10
tests/_mocks/commands/mockCmd.ts
Normal file
10
tests/_mocks/commands/mockCmd.ts
Normal 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"];
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,8 @@ import Help, { ICommandData } from "../../src/commands/help";
|
||||||
import { Message } from "discord.js";
|
import { Message } from "discord.js";
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
||||||
|
|
||||||
|
const oldCwd = process.cwd();
|
||||||
|
|
||||||
describe('Constructor', () => {
|
describe('Constructor', () => {
|
||||||
test('Expect properties to be set', () => {
|
test('Expect properties to be set', () => {
|
||||||
const help = new Help();
|
const help = new Help();
|
||||||
|
@ -86,13 +88,10 @@ describe('SendAll', () => {
|
||||||
|
|
||||||
help.GetAllCommandData = jest.fn()
|
help.GetAllCommandData = jest.fn()
|
||||||
.mockReturnValue([commandData0, commandData1]);
|
.mockReturnValue([commandData0, commandData1]);
|
||||||
help.DetermineCategories = jest.fn()
|
|
||||||
.mockReturnValue(['general']);
|
|
||||||
|
|
||||||
const result = help.SendAll(context);
|
const result = help.SendAll(context);
|
||||||
|
|
||||||
expect(help.GetAllCommandData).toBeCalled();
|
expect(help.GetAllCommandData).toBeCalled();
|
||||||
expect(help.DetermineCategories).toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalled();
|
expect(messageChannelSend).toBeCalled();
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
expect(result.embeds.length).toBe(1);
|
||||||
|
@ -200,3 +199,69 @@ describe('SendSingle', () => {
|
||||||
expect(errorEmbed.description).toBe('Command does not exist');
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue