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
8 changed files with 698 additions and 21 deletions
Showing only changes of commit 0315f80976 - Show all commits

View file

@ -2,7 +2,7 @@ import { MessageEmbed } from "discord.js";
import { ICommandContext } from "../../contracts/ICommandContext"; import { ICommandContext } from "../../contracts/ICommandContext";
export default class ErrorEmbed extends MessageEmbed { export default class ErrorEmbed extends MessageEmbed {
private _context: ICommandContext; public context: ICommandContext;
constructor(context: ICommandContext, message: String) { constructor(context: ICommandContext, message: String) {
super(); super();
@ -10,10 +10,10 @@ export default class ErrorEmbed extends MessageEmbed {
super.setColor(process.env.EMBED_COLOUR_ERROR!); super.setColor(process.env.EMBED_COLOUR_ERROR!);
super.setDescription(message); super.setDescription(message);
this._context = context; this.context = context;
} }
public SendToCurrentChannel() { public SendToCurrentChannel() {
this._context.message.channel.send(this); this.context.message.channel.send(this);
} }
} }

View file

@ -1,7 +1,7 @@
import { MessageEmbed, TextChannel, User, Guild } from "discord.js"; import { MessageEmbed, TextChannel, User, Guild } from "discord.js";
export default class EventEmbed extends MessageEmbed { export default class EventEmbed extends MessageEmbed {
private _guild: Guild; public guild: Guild;
constructor(guild: Guild, title: string) { constructor(guild: Guild, title: string) {
super(); super();
@ -9,25 +9,25 @@ export default class EventEmbed extends MessageEmbed {
super.setColor(process.env.EMBED_COLOUR!); super.setColor(process.env.EMBED_COLOUR!);
super.setTitle(title); super.setTitle(title);
this._guild = guild; this.guild = guild;
} }
// Detail methods // Detail methods
public AddUser(title: string, user: User, setThumbnail: boolean = false) { public AddUser(title: string, user: User, setThumbnail: boolean = false) {
super.addField(title, `${user} \`${user.tag}\``, true); this.addField(title, `${user} \`${user.tag}\``, true);
if (setThumbnail) { if (setThumbnail) {
super.setThumbnail(user.displayAvatarURL()); this.setThumbnail(user.displayAvatarURL());
} }
} }
public AddReason(message: String) { public AddReason(message: String) {
super.addField("Reason", message || "*none*"); this.addField("Reason", message || "*none*");
} }
// Send methods // Send methods
public SendToChannel(name: string) { public SendToChannel(name: string) {
const channel = this._guild.channels.cache const channel = this.guild.channels.cache
.find(channel => channel.name == name) as TextChannel; .find(channel => channel.name == name) as TextChannel;
if (!channel) { if (!channel) {

View file

@ -4,7 +4,7 @@ import { ICommandContext } from "../../contracts/ICommandContext";
import ErrorEmbed from "./ErrorEmbed"; import ErrorEmbed from "./ErrorEmbed";
export default class LogEmbed extends MessageEmbed { export default class LogEmbed extends MessageEmbed {
private _context: ICommandContext; public context: ICommandContext;
constructor(context: ICommandContext, title: string) { constructor(context: ICommandContext, title: string) {
super(); super();
@ -12,33 +12,33 @@ export default class LogEmbed extends MessageEmbed {
super.setColor(process.env.EMBED_COLOUR!); super.setColor(process.env.EMBED_COLOUR!);
super.setTitle(title); super.setTitle(title);
this._context = context; this.context = context;
} }
// Detail methods // Detail methods
public AddUser(title: string, user: User, setThumbnail: boolean = false) { public AddUser(title: string, user: User, setThumbnail: boolean = false) {
super.addField(title, `${user} \`${user.tag}\``, true); this.addField(title, `${user} \`${user.tag}\``, true);
if (setThumbnail) { if (setThumbnail) {
super.setThumbnail(user.displayAvatarURL()); this.setThumbnail(user.displayAvatarURL());
} }
} }
public AddReason(message: String) { public AddReason(message: String) {
super.addField("Reason", message || "*none*"); this.addField("Reason", message || "*none*");
} }
// Send methods // Send methods
public SendToCurrentChannel() { public SendToCurrentChannel() {
this._context.message.channel.send(this); this.context.message.channel.send(this);
} }
public SendToChannel(name: string) { public SendToChannel(name: string) {
const channel = this._context.message.guild?.channels.cache const channel = this.context.message.guild?.channels.cache
.find(channel => channel.name == name) as TextChannel; .find(channel => channel.name == name) as TextChannel;
if (!channel) { if (!channel) {
const errorEmbed = new ErrorEmbed(this._context, ErrorMessages.ChannelNotFound); const errorEmbed = new ErrorEmbed(this.context, ErrorMessages.ChannelNotFound);
errorEmbed.SendToCurrentChannel(); errorEmbed.SendToCurrentChannel();
return; return;
} }

View file

@ -2,7 +2,7 @@ import { MessageEmbed } from "discord.js";
import { ICommandContext } from "../../contracts/ICommandContext"; import { ICommandContext } from "../../contracts/ICommandContext";
export default class PublicEmbed extends MessageEmbed { export default class PublicEmbed extends MessageEmbed {
private _context: ICommandContext; public context: ICommandContext;
constructor(context: ICommandContext, title: string, description: string) { constructor(context: ICommandContext, title: string, description: string) {
super(); super();
@ -11,16 +11,16 @@ export default class PublicEmbed extends MessageEmbed {
super.setTitle(title); super.setTitle(title);
super.setDescription(description); super.setDescription(description);
this._context = context; this.context = context;
} }
// Detail methods // Detail methods
public AddReason(message: String) { public AddReason(message: String) {
super.addField("Reason", message || "*none*"); this.addField("Reason", message || "*none*");
} }
// Send methods // Send methods
public SendToCurrentChannel() { public SendToCurrentChannel() {
this._context.message.channel.send(this); this.context.message.channel.send(this);
} }
} }

View file

@ -0,0 +1,57 @@
import { Message } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext";
import ErrorEmbed from "../../../src/helpers/embeds/ErrorEmbed";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
EMBED_COLOUR_ERROR: '0xd52803'
}
const message = {} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new ErrorEmbed(context, 'Error Message');
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
expect(errorEmbed.description).toBe('Error Message');
expect(errorEmbed.context).toBe(context);
});
});
describe('SendToCurrentChannel', () => {
test('Expect embed to be sent to the current channel in context', () => {
process.env = {
EMBED_COLOUR_ERROR: '0xd52803'
}
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new ErrorEmbed(context, 'Error Message');
errorEmbed.SendToCurrentChannel();
expect(messageChannelSend).toBeCalledWith(errorEmbed);
});
});

View file

@ -0,0 +1,222 @@
import { Guild, Message, TextChannel, User } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext";
import EventEmbed from "../../../src/helpers/embeds/EventEmbed";
beforeEach(() => {
process.env = {};
jest.resetAllMocks();
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const guild = {} as unknown as Guild;
const errorEmbed = new EventEmbed(guild, 'Event Message');
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
expect(errorEmbed.title).toBe('Event Message');
expect(errorEmbed.guild).toBe(guild);
});
});
describe('AddUser', () => {
test('Given setThumbnail is false, add field WITHOUT user thumbnail', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const addField = jest.fn();
const setThumbnail = jest.fn();
const guild = {} as unknown as Guild;
const user = {
tag: 'USERTAG'
} as unknown as User;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.addField = addField;
errorEmbed.setThumbnail = setThumbnail;
errorEmbed.AddUser('User', user);
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
expect(setThumbnail).not.toBeCalled();
});
test('Given setThumbnail is true, add field WITH user thumbnail', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const addField = jest.fn();
const setThumbnail = jest.fn();
const displayAvatarURL = jest.fn()
.mockReturnValue('image0.png');
const guild = {} as unknown as Guild;
const user = {
tag: 'USERTAG',
displayAvatarURL: displayAvatarURL
} as unknown as User;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.addField = addField;
errorEmbed.setThumbnail = setThumbnail;
errorEmbed.AddUser('User', user, true);
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
expect(setThumbnail).toBeCalledWith('image0.png');
expect(displayAvatarURL).toBeCalled();
});
});
describe('SendToChannel', () => {
test('Given channel can be found, expect embed to be sent to that channel', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const channel = {
send: channelSend
} as unknown as TextChannel;
const guildChannelsCacheFind = jest.fn()
.mockReturnValue(channel);
const guild = {
channels: {
cache: {
find: guildChannelsCacheFind
}
}
} as unknown as Guild;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel('channel-name');
expect(guildChannelsCacheFind).toBeCalledTimes(1);
expect(channelSend).toBeCalledWith(errorEmbed);
});
test('Given channel can NOT be found, expect error logged', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const guildChannelsCacheFind = jest.fn()
.mockReturnValue(null);
const guild = {
channels: {
cache: {
find: guildChannelsCacheFind
}
}
} as unknown as Guild;
console.error = jest.fn();
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel('channel-name');
expect(guildChannelsCacheFind).toBeCalledTimes(1);
expect(console.error).toBeCalledWith('Unable to find channel channel-name');
});
});
describe('SendToMessageLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MESSAGE as parameter', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn();
const guild = {} as unknown as Guild;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
errorEmbed.SendToMessageLogsChannel();
expect(sendToChannel).toBeCalledWith('message-logs');
});
});
describe('SendToMemberLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MEMBER as parameter', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn();
const guild = {} as unknown as Guild;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
errorEmbed.SendToMemberLogsChannel();
expect(sendToChannel).toBeCalledWith('member-logs');
});
});
describe('SendToModLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MOD as parameter', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn();
const guild = {} as unknown as Guild;
const errorEmbed = new EventEmbed(guild, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
errorEmbed.SendToModLogsChannel();
expect(sendToChannel).toBeCalledWith('mod-logs');
});
});

View file

@ -0,0 +1,331 @@
import { Guild, Message, TextChannel, User } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext";
import LogEmbed from "../../../src/helpers/embeds/LogEmbed";
beforeEach(() => {
process.env = {};
jest.resetAllMocks();
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Log Message');
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
expect(errorEmbed.title).toBe('Log Message');
expect(errorEmbed.context).toBe(context);
});
});
describe('AddUser', () => {
test('Given setThumbnail is false, add field WITHOUT user thumbnail', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const addField = jest.fn();
const setThumbnail = jest.fn();
const user = {
tag: 'USERTAG'
} as unknown as User;
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
},
author: user
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.addField = addField;
errorEmbed.setThumbnail = setThumbnail;
errorEmbed.AddUser('User', user);
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
expect(setThumbnail).not.toBeCalled();
});
test('Given setThumbnail is true, add field WITH user thumbnail', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const addField = jest.fn();
const setThumbnail = jest.fn();
const displayAvatarURL = jest.fn()
.mockReturnValue('image0.png');
const user = {
tag: 'USERTAG',
displayAvatarURL: displayAvatarURL
} as unknown as User;
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
},
author: user
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.addField = addField;
errorEmbed.setThumbnail = setThumbnail;
errorEmbed.AddUser('User', user, true);
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
expect(setThumbnail).toBeCalledWith('image0.png');
expect(displayAvatarURL).toBeCalled();
});
});
describe('SendToChannel', () => {
test('Given channel can be found, expect embed to be sent to that channel', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const channel = {
send: channelSend
} as unknown as TextChannel;
const guildChannelsCacheFind = jest.fn()
.mockReturnValue(channel);
const guild = {
channels: {
cache: {
find: guildChannelsCacheFind
}
}
} as unknown as Guild;
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
},
guild: guild
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToChannel('channel-name');
expect(guildChannelsCacheFind).toBeCalledTimes(1);
expect(channelSend).toBeCalledWith(errorEmbed);
});
test('Given channel can NOT be found, expect error logged', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const guildChannelsCacheFind = jest.fn()
.mockReturnValue(null);
const guild = {
channels: {
cache: {
find: guildChannelsCacheFind
}
}
} as unknown as Guild;
console.error = jest.fn();
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
},
guild: guild
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToChannel('channel-name');
expect(guildChannelsCacheFind).toBeCalledTimes(1);
});
});
describe('SendToMessageLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MESSAGE as parameter', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn();
const guild = {} as unknown as Guild;
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
errorEmbed.SendToMessageLogsChannel();
expect(sendToChannel).toBeCalledWith('message-logs');
});
});
describe('SendToMemberLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MEMBER as parameter', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn();
const guild = {} as unknown as Guild;
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
errorEmbed.SendToMemberLogsChannel();
expect(sendToChannel).toBeCalledWith('member-logs');
});
});
describe('SendToModLogsChannel', () => {
describe('Expect SendToChannel caleld with CHANNELS_LOGS_MOD as parameter', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const sendToChannel = jest.fn();
const guild = {} as unknown as Guild;
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new LogEmbed(context, 'Event Message');
errorEmbed.SendToChannel = sendToChannel;
errorEmbed.SendToModLogsChannel();
expect(sendToChannel).toBeCalledWith('mod-logs');
});
});

View file

@ -0,0 +1,67 @@
import { Guild, Message, TextChannel, User } from "discord.js";
import { ICommandContext } from "../../../src/contracts/ICommandContext";
import PublicEmbed from "../../../src/helpers/embeds/PublicEmbed";
beforeEach(() => {
process.env = {};
});
describe('Constructor', () => {
test('Expect properties to be set', () => {
process.env = {
EMBED_COLOUR: '0xd52803',
CHANNELS_LOGS_MESSAGE: 'message-logs',
CHANNELS_LOGS_MEMBER: 'member-logs',
CHANNELS_LOGS_MOD: 'mod-logs'
}
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new PublicEmbed(context, 'Log Message', 'Log Description');
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
expect(errorEmbed.title).toBe('Log Message');
expect(errorEmbed.description).toBe('Log Description');
expect(errorEmbed.context).toBe(context);
});
});
describe('SendToCurrentChannel', () => {
test('Expect embed to be sent to the current channel in context', () => {
process.env = {
EMBED_COLOUR_ERROR: '0xd52803'
}
const messageChannelSend = jest.fn();
const message = {
channel: {
send: messageChannelSend
}
} as unknown as Message;
const context: ICommandContext = {
name: 'command',
args: [],
message: message
};
const errorEmbed = new PublicEmbed(context, 'Message', 'Description');
errorEmbed.SendToCurrentChannel();
expect(messageChannelSend).toBeCalledWith(errorEmbed);
});
});