Tests and CI #44

Merged
Vylpes merged 17 commits from feature/tests into develop 2021-08-21 16:00:28 +01:00
2 changed files with 135 additions and 8 deletions
Showing only changes of commit 49d3d9c449 - Show all commits

View file

@ -21,11 +21,6 @@ export class Events {
// Emit when a message is sent
// Used to check for commands
public onMessage(message: Message): IEventResponse {
if (!message) return {
valid: false,
message: "Message was not supplied.",
};
if (!message.guild) return {
valid: false,
message: "Message was not sent in a guild, ignoring.",

View file

@ -1,13 +1,13 @@
import { Events } from "../../src/client/events";
import { Message, Client, User, GuildMember, TextChannel, Guild, SnowflakeUtil, Role } from "discord.js";
import { Message, Client, User, GuildMember, TextChannel, Guild, SnowflakeUtil, Role, DMChannel } from "discord.js";
import * as dotenv from "dotenv";
import { Util } from "../../src/client/util";
jest.mock("dotenv");
jest.mock("../../src/client/util");
test('Start_GivenMessageIsValid_ExpectMessageSent', () => {
test('OnMessage_GivenMessageIsValid_ExpectMessageSent', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
@ -42,7 +42,7 @@ test('Start_GivenMessageIsValid_ExpectMessageSent', () => {
discordClient,
{
content: "!test first",
author: { username: "BiggestBulb", discriminator: 1234 },
author: { username: "test-user", discriminator: 1234 },
id: "test",
},
new TextChannel(guild, {
@ -66,3 +66,135 @@ test('Start_GivenMessageIsValid_ExpectMessageSent', () => {
discordClient.destroy();
});
test('OnMessage_GivenGuildIsNull_ExpectFailedResult', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
Util.prototype.loadCommand = jest.fn().mockImplementation((name: string, args: string[], message: Message) => {
return {
valid: true
}
});
let client = new Client();
let guild = new Guild(client, {
id: SnowflakeUtil.generate(),
});
let user = new User(client, {
id: SnowflakeUtil.generate(),
});
let member = new GuildMember(
client,
{
id: SnowflakeUtil.generate(),
user: {
id: user.id
}
},
guild
);
let role = new Role(
client,
{
id: SnowflakeUtil.generate()
},
guild
);
let message = new Message(
client,
{
content: "!test first",
author: {
username: "test-user",
discriminator: 1234
},
id: "test",
},
new DMChannel(
client,
{
id: "channel-id",
}
)
);
const events = new Events();
const result = events.onMessage(message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was not sent in a guild, ignoring.");
client.destroy();
});
test('OnMessage_GivenAuthorIsBot_ExpectFailedResult', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
Util.prototype.loadCommand = jest.fn().mockImplementation((name: string, args: string[], message: Message) => {
return {
valid: true
}
});
let client = new Client();
let guild = new Guild(client, {
id: SnowflakeUtil.generate(),
});
let user = new User(client, {
id: SnowflakeUtil.generate(),
});
let member = new GuildMember(
client,
{
id: SnowflakeUtil.generate(),
user: {
id: user.id
}
},
guild
);
let role = new Role(
client,
{
id: SnowflakeUtil.generate()
},
guild
);
let message = new Message(
client,
{
content: "!test first",
author: {
username: "test-user",
discriminator: 1234,
bot: true,
},
id: "test",
},
new TextChannel(guild, {
client: client,
guild: guild,
id: "channel-id",
})
);
const events = new Events();
const result = events.onMessage(message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was sent by a bot, ignoring.");
client.destroy();
});