2021-08-12 20:18:09 +01:00
|
|
|
import { Events } from "../../src/client/events";
|
|
|
|
|
2021-08-18 21:27:44 +01:00
|
|
|
import { Message, Client, TextChannel, Guild, SnowflakeUtil, DMChannel } from "discord.js";
|
2021-08-12 20:18:09 +01:00
|
|
|
import { Util } from "../../src/client/util";
|
|
|
|
|
|
|
|
jest.mock("../../src/client/util");
|
|
|
|
|
2021-08-18 21:11:49 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
Util.prototype.loadCommand = jest.fn();
|
|
|
|
});
|
|
|
|
|
2021-08-21 15:33:14 +01:00
|
|
|
describe('OnMessage', () => {
|
2021-09-26 14:32:56 +01:00
|
|
|
test('Given Message Is Valid Expect Message Sent', async () => {
|
2021-08-21 15:33:14 +01:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
};
|
|
|
|
|
|
|
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
guild: {},
|
|
|
|
author: {
|
|
|
|
bot: false,
|
|
|
|
},
|
|
|
|
content: "!test first",
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const events = new Events();
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
const result = await events.onMessage(message);
|
2021-08-21 15:33:14 +01:00
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
|
|
|
|
|
|
|
expect(result.context?.prefix).toBe('!');
|
|
|
|
expect(result.context?.name).toBe('test');
|
|
|
|
expect(result.context?.args.length).toBe(1);
|
|
|
|
expect(result.context?.args[0]).toBe('first');
|
|
|
|
expect(result.context?.message).toBe(message);
|
|
|
|
});
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
test('Given Guild Is Null, Expect Failed Result', async () => {
|
2021-08-21 15:33:14 +01:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
guild: null,
|
|
|
|
author: {
|
|
|
|
bot: false,
|
|
|
|
},
|
|
|
|
content: "!test first",
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const events = new Events();
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
const result = await events.onMessage(message);
|
2021-08-21 15:33:14 +01:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Message was not sent in a guild, ignoring.");
|
|
|
|
});
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
test('Given Author Is A Bot, Expect Failed Result', async () => {
|
2021-08-21 15:33:14 +01:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
guild: {},
|
|
|
|
author: {
|
|
|
|
bot: true,
|
|
|
|
},
|
|
|
|
content: "!test first",
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const events = new Events();
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
const result = await events.onMessage(message);
|
2021-08-21 15:33:14 +01:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Message was sent by a bot, ignoring.");
|
|
|
|
});
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
test('Given Message Content Was Not A Command, Expect Failed Result', async () => {
|
2021-08-21 15:33:14 +01:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
guild: {},
|
|
|
|
author: {
|
|
|
|
bot: false,
|
|
|
|
},
|
|
|
|
content: "This is a standard message",
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const events = new Events();
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
const result = await events.onMessage(message);
|
2021-08-21 15:33:14 +01:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Message was not a command, ignoring.");
|
|
|
|
});
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
test('Given Message Had No Command Name, Expect Failed Result', async () => {
|
2021-08-21 15:33:14 +01:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
guild: {},
|
|
|
|
author: {
|
|
|
|
bot: false,
|
|
|
|
},
|
|
|
|
content: "!",
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const events = new Events();
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
const result = await events.onMessage(message);
|
2021-08-21 15:33:14 +01:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Command name was not found");
|
|
|
|
});
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
test('Given Command Failed To Execute, Expect Failed Result', async () => {
|
2021-08-21 15:33:14 +01:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: false, message: "Command failed" });
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
guild: {},
|
|
|
|
author: {
|
|
|
|
bot: false,
|
|
|
|
},
|
|
|
|
content: "!test first",
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const events = new Events();
|
|
|
|
|
2021-09-26 14:32:56 +01:00
|
|
|
const result = await events.onMessage(message);
|
2021-08-21 15:33:14 +01:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Command failed");
|
|
|
|
});
|
2021-08-18 21:11:49 +01:00
|
|
|
});
|
|
|
|
|
2021-08-21 15:33:14 +01:00
|
|
|
describe('OnReady', () => {
|
|
|
|
test('Expect Console Log', () => {
|
|
|
|
console.log = jest.fn();
|
|
|
|
|
|
|
|
const events = new Events();
|
|
|
|
|
|
|
|
events.onReady();
|
|
|
|
|
|
|
|
expect(console.log).toBeCalledWith("Ready");
|
|
|
|
});
|
2021-09-26 14:32:56 +01:00
|
|
|
});
|