Add 2 more tests to events
This commit is contained in:
parent
da96bbefa5
commit
49d3d9c449
2 changed files with 135 additions and 8 deletions
|
@ -21,11 +21,6 @@ export class Events {
|
||||||
// Emit when a message is sent
|
// Emit when a message is sent
|
||||||
// Used to check for commands
|
// Used to check for commands
|
||||||
public onMessage(message: Message): IEventResponse {
|
public onMessage(message: Message): IEventResponse {
|
||||||
if (!message) return {
|
|
||||||
valid: false,
|
|
||||||
message: "Message was not supplied.",
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!message.guild) return {
|
if (!message.guild) return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: "Message was not sent in a guild, ignoring.",
|
message: "Message was not sent in a guild, ignoring.",
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
import { Events } from "../../src/client/events";
|
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 * as dotenv from "dotenv";
|
||||||
import { Util } from "../../src/client/util";
|
import { Util } from "../../src/client/util";
|
||||||
|
|
||||||
jest.mock("dotenv");
|
jest.mock("dotenv");
|
||||||
jest.mock("../../src/client/util");
|
jest.mock("../../src/client/util");
|
||||||
|
|
||||||
test('Start_GivenMessageIsValid_ExpectMessageSent', () => {
|
test('OnMessage_GivenMessageIsValid_ExpectMessageSent', () => {
|
||||||
process.env = {
|
process.env = {
|
||||||
BOT_TOKEN: 'TOKEN',
|
BOT_TOKEN: 'TOKEN',
|
||||||
BOT_PREFIX: '!',
|
BOT_PREFIX: '!',
|
||||||
|
@ -42,7 +42,7 @@ test('Start_GivenMessageIsValid_ExpectMessageSent', () => {
|
||||||
discordClient,
|
discordClient,
|
||||||
{
|
{
|
||||||
content: "!test first",
|
content: "!test first",
|
||||||
author: { username: "BiggestBulb", discriminator: 1234 },
|
author: { username: "test-user", discriminator: 1234 },
|
||||||
id: "test",
|
id: "test",
|
||||||
},
|
},
|
||||||
new TextChannel(guild, {
|
new TextChannel(guild, {
|
||||||
|
@ -66,3 +66,135 @@ test('Start_GivenMessageIsValid_ExpectMessageSent', () => {
|
||||||
|
|
||||||
discordClient.destroy();
|
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();
|
||||||
|
});
|
||||||
|
|
Reference in a new issue