diff --git a/src/helpers/embeds/ErrorEmbed.ts b/src/helpers/embeds/ErrorEmbed.ts index ded3d2d..6b2c06d 100644 --- a/src/helpers/embeds/ErrorEmbed.ts +++ b/src/helpers/embeds/ErrorEmbed.ts @@ -2,7 +2,7 @@ import { MessageEmbed } from "discord.js"; import { ICommandContext } from "../../contracts/ICommandContext"; export default class ErrorEmbed extends MessageEmbed { - private _context: ICommandContext; + public context: ICommandContext; constructor(context: ICommandContext, message: String) { super(); @@ -10,10 +10,10 @@ export default class ErrorEmbed extends MessageEmbed { super.setColor(process.env.EMBED_COLOUR_ERROR!); super.setDescription(message); - this._context = context; + this.context = context; } public SendToCurrentChannel() { - this._context.message.channel.send(this); + this.context.message.channel.send(this); } } \ No newline at end of file diff --git a/src/helpers/embeds/EventEmbed.ts b/src/helpers/embeds/EventEmbed.ts index 8b4d364..885961b 100644 --- a/src/helpers/embeds/EventEmbed.ts +++ b/src/helpers/embeds/EventEmbed.ts @@ -1,7 +1,7 @@ import { MessageEmbed, TextChannel, User, Guild } from "discord.js"; export default class EventEmbed extends MessageEmbed { - private _guild: Guild; + public guild: Guild; constructor(guild: Guild, title: string) { super(); @@ -9,25 +9,25 @@ export default class EventEmbed extends MessageEmbed { super.setColor(process.env.EMBED_COLOUR!); super.setTitle(title); - this._guild = guild; + this.guild = guild; } // Detail methods 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) { - super.setThumbnail(user.displayAvatarURL()); + this.setThumbnail(user.displayAvatarURL()); } } public AddReason(message: String) { - super.addField("Reason", message || "*none*"); + this.addField("Reason", message || "*none*"); } // Send methods public SendToChannel(name: string) { - const channel = this._guild.channels.cache + const channel = this.guild.channels.cache .find(channel => channel.name == name) as TextChannel; if (!channel) { diff --git a/src/helpers/embeds/LogEmbed.ts b/src/helpers/embeds/LogEmbed.ts index 00eb17a..f5e2e36 100644 --- a/src/helpers/embeds/LogEmbed.ts +++ b/src/helpers/embeds/LogEmbed.ts @@ -4,7 +4,7 @@ import { ICommandContext } from "../../contracts/ICommandContext"; import ErrorEmbed from "./ErrorEmbed"; export default class LogEmbed extends MessageEmbed { - private _context: ICommandContext; + public context: ICommandContext; constructor(context: ICommandContext, title: string) { super(); @@ -12,33 +12,33 @@ export default class LogEmbed extends MessageEmbed { super.setColor(process.env.EMBED_COLOUR!); super.setTitle(title); - this._context = context; + this.context = context; } // Detail methods 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) { - super.setThumbnail(user.displayAvatarURL()); + this.setThumbnail(user.displayAvatarURL()); } } public AddReason(message: String) { - super.addField("Reason", message || "*none*"); + this.addField("Reason", message || "*none*"); } // Send methods public SendToCurrentChannel() { - this._context.message.channel.send(this); + this.context.message.channel.send(this); } 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; if (!channel) { - const errorEmbed = new ErrorEmbed(this._context, ErrorMessages.ChannelNotFound); + const errorEmbed = new ErrorEmbed(this.context, ErrorMessages.ChannelNotFound); errorEmbed.SendToCurrentChannel(); return; } diff --git a/src/helpers/embeds/PublicEmbed.ts b/src/helpers/embeds/PublicEmbed.ts index 4a02f17..2bd40cd 100644 --- a/src/helpers/embeds/PublicEmbed.ts +++ b/src/helpers/embeds/PublicEmbed.ts @@ -2,7 +2,7 @@ import { MessageEmbed } from "discord.js"; import { ICommandContext } from "../../contracts/ICommandContext"; export default class PublicEmbed extends MessageEmbed { - private _context: ICommandContext; + public context: ICommandContext; constructor(context: ICommandContext, title: string, description: string) { super(); @@ -11,16 +11,16 @@ export default class PublicEmbed extends MessageEmbed { super.setTitle(title); super.setDescription(description); - this._context = context; + this.context = context; } // Detail methods public AddReason(message: String) { - super.addField("Reason", message || "*none*"); + this.addField("Reason", message || "*none*"); } // Send methods public SendToCurrentChannel() { - this._context.message.channel.send(this); + this.context.message.channel.send(this); } } \ No newline at end of file diff --git a/tests/helpers/embeds/ErrorEmbed.test.ts b/tests/helpers/embeds/ErrorEmbed.test.ts new file mode 100644 index 0000000..76f0b9b --- /dev/null +++ b/tests/helpers/embeds/ErrorEmbed.test.ts @@ -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); + }); +}); \ No newline at end of file diff --git a/tests/helpers/embeds/EventEmbed.test.ts b/tests/helpers/embeds/EventEmbed.test.ts new file mode 100644 index 0000000..f5932e3 --- /dev/null +++ b/tests/helpers/embeds/EventEmbed.test.ts @@ -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'); + }); +}); \ No newline at end of file diff --git a/tests/helpers/embeds/LogEmbed.test.ts b/tests/helpers/embeds/LogEmbed.test.ts new file mode 100644 index 0000000..40abd19 --- /dev/null +++ b/tests/helpers/embeds/LogEmbed.test.ts @@ -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'); + }); +}); \ No newline at end of file diff --git a/tests/helpers/embeds/PublicEmbed.test.ts b/tests/helpers/embeds/PublicEmbed.test.ts new file mode 100644 index 0000000..6b45797 --- /dev/null +++ b/tests/helpers/embeds/PublicEmbed.test.ts @@ -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); + }); +}); \ No newline at end of file