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
5 changed files with 347 additions and 7 deletions
Showing only changes of commit ceb5f6c148 - Show all commits

View file

@ -3,6 +3,7 @@ import { TextChannel } from "discord.js";
import PublicEmbed from "../helpers/embeds/PublicEmbed"; import PublicEmbed from "../helpers/embeds/PublicEmbed";
import { Command } from "../type/command"; import { Command } from "../type/command";
import { ICommandContext } from "../contracts/ICommandContext"; import { ICommandContext } from "../contracts/ICommandContext";
import ICommandReturnContext from "../contracts/ICommandReturnContext";
export default class Clear extends Command { export default class Clear extends Command {
constructor() { constructor() {
@ -14,11 +15,15 @@ export default class Clear extends Command {
]; ];
} }
public override async execute(context: ICommandContext) { public override async execute(context: ICommandContext): Promise<ICommandReturnContext> {
if (context.args.length == 0) { if (context.args.length == 0) {
const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100"); const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100");
errorEmbed.SendToCurrentChannel(); errorEmbed.SendToCurrentChannel();
return;
return {
commandContext: context,
embeds: [errorEmbed]
};
} }
const totalToClear = Number.parseInt(context.args[0]); const totalToClear = Number.parseInt(context.args[0]);
@ -26,12 +31,20 @@ export default class Clear extends Command {
if (!totalToClear || totalToClear <= 0 || totalToClear > 100) { if (!totalToClear || totalToClear <= 0 || totalToClear > 100) {
const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100"); const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100");
errorEmbed.SendToCurrentChannel(); errorEmbed.SendToCurrentChannel();
return; return {
commandContext: context,
embeds: [errorEmbed]
};
} }
await (context.message.channel as TextChannel).bulkDelete(totalToClear); await (context.message.channel as TextChannel).bulkDelete(totalToClear);
const embed = new PublicEmbed(context, "", `${totalToClear} message(s) were removed`); const embed = new PublicEmbed(context, "", `${totalToClear} message(s) were removed`);
embed.SendToCurrentChannel(); embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [embed]
};
} }
} }

View file

@ -1,4 +1,5 @@
import { ICommandContext } from "../contracts/ICommandContext"; import { ICommandContext } from "../contracts/ICommandContext";
import ICommandReturnContext from "../contracts/ICommandReturnContext";
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 { Command } from "../type/command"; import { Command } from "../type/command";
@ -10,9 +11,12 @@ export default class Evaluate extends Command {
super._category = "Owner"; super._category = "Owner";
} }
public override execute(context: ICommandContext) { public override execute(context: ICommandContext): ICommandReturnContext {
if (context.message.author.id != process.env.BOT_OWNERID) { if (context.message.author.id != process.env.BOT_OWNERID) {
return; return {
commandContext: context,
embeds: []
};
} }
const stmt = context.args; const stmt = context.args;
@ -24,10 +28,20 @@ export default class Evaluate extends Command {
const embed = new PublicEmbed(context, "", result); const embed = new PublicEmbed(context, "", result);
embed.SendToCurrentChannel(); embed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [embed]
};
} }
catch (err: any) { catch (err: any) {
const errorEmbed = new ErrorEmbed(context, err); const errorEmbed = new ErrorEmbed(context, err);
errorEmbed.SendToCurrentChannel(); errorEmbed.SendToCurrentChannel();
return {
commandContext: context,
embeds: [errorEmbed]
};
} }
} }
} }

View file

@ -1,5 +1,4 @@
import { Collection, Guild, GuildChannel, GuildChannelManager, GuildManager, GuildMember, Message, MessageEmbed, TextChannel, User } from "discord.js"; import { GuildMember, Message, TextChannel, User } from "discord.js";
import { mock } from "jest-mock-extended";
import Ban from "../../src/commands/ban"; import Ban from "../../src/commands/ban";
import { ICommandContext } from "../../src/contracts/ICommandContext"; import { ICommandContext } from "../../src/contracts/ICommandContext";

View file

@ -0,0 +1,178 @@
import { Message } from "discord.js";
import Clear from "../../src/commands/clear";
import { ICommandContext } from "../../src/contracts/ICommandContext";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect values to be set', () => {
process.env = {
ROLES_MODERATOR: "Moderator"
};
const clear = new Clear();
expect(clear._category).toBe('Moderation');
expect(clear._roles.length).toBe(1);
expect(clear._roles[0]).toBe('Moderator');
});
});
describe('Execute', () => {
test('Given valid arguments, expect messages to be cleared', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
const context: ICommandContext = {
name: 'clear',
args: ['5'],
message: message
};
const clear = new Clear();
const result = await clear.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).toBeCalledWith(5);
expect(result.embeds.length).toBe(1);
// PublicEmbed
const publicEmbed = result.embeds[0];
expect(publicEmbed.title).toBe('');
expect(publicEmbed.description).toBe('5 message(s) were removed');
});
test('Given argument is not given, expect error embed to be sent', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
const context: ICommandContext = {
name: 'clear',
args: [],
message: message
};
const clear = new Clear();
const result = await clear.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
});
test('Given argument is not a number, expect error embed to be sent', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
const context: ICommandContext = {
name: 'clear',
args: ['A'],
message: message
};
const clear = new Clear();
const result = await clear.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
});
test('Given argument is less than 1, expect error embed to be sent', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
const context: ICommandContext = {
name: 'clear',
args: ['0'],
message: message
};
const clear = new Clear();
const result = await clear.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
});
test('Given argument is more than 100, expect error embed to be sent', async () => {
const messageChannelSend = jest.fn();
const messageChannelBulkDelete = jest.fn();
const message = {
channel: {
send: messageChannelSend,
bulkDelete: messageChannelBulkDelete
}
} as unknown as Message;
const context: ICommandContext = {
name: 'clear',
args: ['101'],
message: message
};
const clear = new Clear();
const result = await clear.execute(context);
expect(messageChannelSend).toBeCalledTimes(1);
expect(messageChannelBulkDelete).not.toBeCalled();
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
});
});

136
tests/commands/eval.test.ts Normal file
View file

@ -0,0 +1,136 @@
import { Message } from "discord.js";
import Evaluate from "../../src/commands/eval";
import { ICommandContext } from "../../src/contracts/ICommandContext";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect values to be set', () => {
const evaluate = new Evaluate();
expect(evaluate._category).toBe('Owner');
});
});
describe('Execute', () => {
test('Given user has permission, expect eval statement ran', () => {
process.env = {
BOT_OWNERID: 'OWNERID'
};
console.log = jest.fn();
global.eval = jest.fn()
.mockReturnValue('General Kenobi');
const messageChannelSend = jest.fn();
const message = {
author: {
id: 'OWNERID'
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'eval',
args: ['echo', 'Hello', 'there'],
message: message
};
const evaluate = new Evaluate();
const result = evaluate.execute(context);
expect(console.log).toBeCalledWith('Eval Statement: echo Hello there');
expect(global.eval).toBeCalledWith('echo Hello there');
expect(result.embeds.length).toBe(1);
// PublicEmbed
const publicEmbed = result.embeds[0];
expect(publicEmbed.title).toBe('');
expect(publicEmbed.description).toBe('General Kenobi');
});
test('Given user does not have permission, expect nothing to occur', () => {
process.env = {
BOT_OWNERID: 'DIFFERENT'
};
console.log = jest.fn();
global.eval = jest.fn()
.mockReturnValue('General Kenobi');
const messageChannelSend = jest.fn();
const message = {
author: {
id: 'OWNERID'
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'eval',
args: ['echo', 'Hello', 'there'],
message: message
};
const evaluate = new Evaluate();
const result = evaluate.execute(context);
expect(console.log).not.toBeCalled();
expect(global.eval).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given eval failed, expect error embed to be sent', () => {
process.env = {
BOT_OWNERID: 'OWNERID'
};
console.log = jest.fn();
global.eval = jest.fn()
.mockImplementation(() => {
throw new Error('Error message');
});
const messageChannelSend = jest.fn();
const message = {
author: {
id: 'OWNERID'
},
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'eval',
args: ['echo', 'Hello', 'there'],
message: message
};
const evaluate = new Evaluate();
const result = evaluate.execute(context);
expect(console.log).toBeCalledWith('Eval Statement: echo Hello there');
expect(global.eval).toBeCalledWith('echo Hello there');
expect(result.embeds.length).toBe(1);
// ErrorEmbed
const errorEmbed = result.embeds[0];
expect(errorEmbed.title).toBeNull();
expect(errorEmbed.description).toBe('Error: Error message');
});
});