vylbot-app/tests/helpers/embeds/EventEmbed.test.ts
Vylpes f61c4c728a
Feature/12 create tests (#102)
* Fix tests

* Update coverage

* Remove unrequired mock files

* Add about command test

* Update about tests

* Ban command tests

* eval command tests

* Start help command tests

* Add help command tests

* Add kick command tests

* Mute command tests

* Poll command tests

* Add role command tests

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Add rules command tests

* Add unmute command tests

* Add warn command tests

* Add MemberEvents tests

* Add GuildMemberUpdate tests

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Add MessageEvents tests

* Add StringTools test

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Add embed tests

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Add GitHub Actions

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Move to tslint

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Remove tslint

Signed-off-by: Ethan Lane <ethan@vylpes.com>

* Remove linting script

Signed-off-by: Ethan Lane <ethan@vylpes.com>
2022-01-30 17:03:36 +00:00

222 lines
No EOL
6.6 KiB
TypeScript

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');
});
});