Feature/12 create tests #102

Merged
Vylpes merged 25 commits from feature/12-create-tests into develop 2022-01-30 17:03:37 +00:00
2 changed files with 228 additions and 11 deletions
Showing only changes of commit 6994a339b6 - Show all commits

View file

@ -3,9 +3,10 @@ import { ICommandContext } from "../contracts/ICommandContext";
import ErrorEmbed from "../helpers/embeds/ErrorEmbed"; import ErrorEmbed from "../helpers/embeds/ErrorEmbed";
import PublicEmbed from "../helpers/embeds/PublicEmbed"; import PublicEmbed from "../helpers/embeds/PublicEmbed";
import StringTools from "../helpers/StringTools"; import StringTools from "../helpers/StringTools";
import ICommandReturnContext from "../contracts/ICommandReturnContext";
import { Command } from "../type/command"; import { Command } from "../type/command";
interface ICommandData { export interface ICommandData {
Exists: boolean; Exists: boolean;
Name?: string; Name?: string;
Category?: string; Category?: string;
@ -19,15 +20,15 @@ export default class Help extends Command {
super._category = "General"; super._category = "General";
} }
public override execute(context: ICommandContext) { public override execute(context: ICommandContext): ICommandReturnContext {
if (context.args.length == 0) { if (context.args.length == 0) {
this.SendAll(context); return this.SendAll(context);
} else { } else {
this.SendSingle(context); return this.SendSingle(context);
} }
} }
private SendAll(context: ICommandContext) { public SendAll(context: ICommandContext): ICommandReturnContext {
const allCommands = this.GetAllCommandData(); const allCommands = this.GetAllCommandData();
const cateogries = this.DetermineCategories(allCommands); const cateogries = this.DetermineCategories(allCommands);
@ -40,15 +41,24 @@ export default class Help extends Command {
}); });
embed.SendToCurrentChannel(); embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [ embed ]
};
} }
private SendSingle(context: ICommandContext) { public SendSingle(context: ICommandContext): ICommandReturnContext {
const command = this.GetCommandData(context.args[0]); const command = this.GetCommandData(context.args[0]);
if (!command.Exists) { if (!command.Exists) {
const errorEmbed = new ErrorEmbed(context, "Command does not exist"); const errorEmbed = new ErrorEmbed(context, "Command does not exist");
errorEmbed.SendToCurrentChannel(); errorEmbed.SendToCurrentChannel();
return;
return {
commandContext: context,
embeds: [ errorEmbed ]
};
} }
const embed = new PublicEmbed(context, StringTools.Capitalise(command.Name!), ""); const embed = new PublicEmbed(context, StringTools.Capitalise(command.Name!), "");
@ -56,9 +66,14 @@ export default class Help extends Command {
embed.addField("Required Roles", StringTools.Capitalise(command.Roles!.join(", ")) || "*none*"); embed.addField("Required Roles", StringTools.Capitalise(command.Roles!.join(", ")) || "*none*");
embed.SendToCurrentChannel(); embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [ embed ]
};
} }
private GetAllCommandData(): ICommandData[] { public GetAllCommandData(): ICommandData[] {
const result: ICommandData[] = []; const result: ICommandData[] = [];
const folder = process.env.FOLDERS_COMMANDS!; const folder = process.env.FOLDERS_COMMANDS!;
@ -82,7 +97,7 @@ export default class Help extends Command {
return result; return result;
} }
private GetCommandData(name: string): ICommandData { public GetCommandData(name: string): ICommandData {
const folder = process.env.FOLDERS_COMMANDS!; const folder = process.env.FOLDERS_COMMANDS!;
const path = `${process.cwd()}/${folder}/${name}.ts`; const path = `${process.cwd()}/${folder}/${name}.ts`;
@ -105,7 +120,7 @@ export default class Help extends Command {
return data; return data;
} }
private DetermineCategories(commands: ICommandData[]): string[] { public DetermineCategories(commands: ICommandData[]): string[] {
const result: string[] = []; const result: string[] = [];
commands.forEach(cmd => { commands.forEach(cmd => {
@ -116,4 +131,4 @@ export default class Help extends Command {
return result; return result;
} }
} }

202
tests/commands/help.test.ts Normal file
View file

@ -0,0 +1,202 @@
import Help, { ICommandData } from "../../src/commands/help";
import { Message } from "discord.js";
import { ICommandContext } from "../../src/contracts/ICommandContext";
describe('Constructor', () => {
test('Expect properties to be set', () => {
const help = new Help();
expect(help._category).toBe('General');
});
});
describe('Execute', () => {
test('Given no arguments were given, expect SendAll to be executed', () => {
const message = {} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: [],
message: message
};
const help = new Help();
help.SendAll = jest.fn();
help.SendSingle = jest.fn();
help.execute(context);
expect(help.SendAll).toBeCalled();
expect(help.SendSingle).not.toBeCalled();
});
test('Given an argument was given, expect SendSingle to be executed', () => {
const message = {} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: ['about'],
message: message
};
const help = new Help();
help.SendAll = jest.fn();
help.SendSingle = jest.fn();
help.execute(context);
expect(help.SendAll).not.toBeCalled();
expect(help.SendSingle).toBeCalled();
});
});
describe('SendAll', () => {
test('Expect embed with all commands to be sent', () => {
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: [],
message: message
};
const help = new Help();
const commandData0: ICommandData = {
Exists: true,
Name: 'about',
Category: 'general',
Roles: []
};
const commandData1: ICommandData = {
Exists: true,
Name: 'role',
Category: 'general',
Roles: []
};
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);
// PublicEmbed
const publicEmbed = result.embeds[0];
expect(publicEmbed.fields.length).toBe(1);
// PublicEmbed -> GeneralCategory Field
const publicEmbedFieldGeneral = publicEmbed.fields[0];
expect(publicEmbedFieldGeneral.name).toBe('General');
expect(publicEmbedFieldGeneral.value).toBe('about, role');
});
});
describe('SendSingle', () => {
test('Given command exists, expect embed to be sent with command fields', () => {
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: ['about'],
message: message
};
const commandData: ICommandData = {
Exists: true,
Name: 'about',
Category: 'general',
Roles: ['role1', 'role2']
};
const help = new Help();
help.GetCommandData = jest.fn()
.mockReturnValue(commandData);
const result = help.SendSingle(context);
expect(help.GetCommandData).toBeCalledWith('about');
expect(messageChannelSend).toBeCalled();
expect(result.embeds.length).toBe(1);
// PublicEmbed
const publicEmbed = result.embeds[0];
expect(publicEmbed.title).toBe('About');
expect(publicEmbed.description).toBe('');
expect(publicEmbed.fields.length).toBe(2);
// PublicEmbed -> Category Field
const fieldCategory = publicEmbed.fields[0];
expect(fieldCategory.name).toBe('Category');
expect(fieldCategory.value).toBe('General');
// PublicEmbed -> RequiredRoles Field
const fieldRoles = publicEmbed.fields[1];
expect(fieldRoles.name).toBe('Required Roles');
expect(fieldRoles.value).toBe('Role1, Role2');
});
test('Given command does not exist, expect error embed to be sent', () => {
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'help',
args: ['about'],
message: message
};
const commandData: ICommandData = {
Exists: false
};
const help = new Help();
help.GetCommandData = jest.fn()
.mockReturnValue(commandData);
const result = help.SendSingle(context);
expect(help.GetCommandData).toBeCalledWith('about');
expect(messageChannelSend).toBeCalled();
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.description).toBe('Command does not exist');
});
});