This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
vylbot-core/tests/client/events.test.ts

181 lines
4.5 KiB
TypeScript
Raw Normal View History

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-16 19:11:42 +01:00
test('OnMessage_GivenMessageIsValid_ExpectMessageSent', () => {
2021-08-12 20:18:09 +01:00
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
2021-08-20 15:42:46 +01:00
};
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
2021-08-12 20:18:09 +01:00
2021-08-20 15:42:46 +01:00
const message = {
guild: {},
author: {
bot: false,
},
2021-08-12 20:18:09 +01:00
content: "!test first",
2021-08-20 15:42:46 +01:00
} as unknown as Message;
2021-08-12 20:18:09 +01:00
const events = new Events();
const result = events.onMessage(message);
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-08-16 19:11:42 +01:00
test('OnMessage_GivenGuildIsNull_ExpectFailedResult', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
2021-08-20 15:42:46 +01:00
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
const message = {
guild: null,
author: {
bot: false,
2021-08-16 19:11:42 +01:00
},
2021-08-20 15:42:46 +01:00
content: "!test first",
} as unknown as Message;
2021-08-16 19:11:42 +01:00
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.");
});
test('OnMessage_GivenAuthorIsBot_ExpectFailedResult', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
2021-08-20 15:42:46 +01:00
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
const message = {
guild: {},
author: {
bot: true,
2021-08-16 19:11:42 +01:00
},
2021-08-20 15:42:46 +01:00
content: "!test first",
} as unknown as Message;
2021-08-16 19:11:42 +01:00
const events = new Events();
const result = events.onMessage(message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was sent by a bot, ignoring.");
});
2021-08-18 21:11:49 +01:00
test('OnMessage_GivenMessageContentsWasNotACommand_ExpectFailedResult', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
2021-08-20 15:42:46 +01:00
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
const message = {
guild: {},
author: {
bot: false,
2021-08-18 21:11:49 +01:00
},
2021-08-20 15:42:46 +01:00
content: "This is a standard message",
} as unknown as Message;
2021-08-18 21:11:49 +01:00
const events = new Events();
const result = events.onMessage(message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was not a command, ignoring.");
});
test('OnMessage_GivenMessageHadNoCommandName_ExpectFailedResult', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
2021-08-20 15:42:46 +01:00
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
const message = {
guild: {},
author: {
bot: false,
},
2021-08-18 21:11:49 +01:00
content: "!",
2021-08-20 15:42:46 +01:00
} as unknown as Message;
2021-08-18 21:11:49 +01:00
const events = new Events();
const result = events.onMessage(message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command name was not found");
});
test('OnMessage_GivenCommandFailedToExecute_ExpectFailedResult', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
2021-08-20 15:42:46 +01:00
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: false, message: "Command failed" });
const message = {
guild: {},
author: {
bot: false,
},
2021-08-18 21:11:49 +01:00
content: "!test first",
2021-08-20 15:42:46 +01:00
} as unknown as Message;
2021-08-18 21:11:49 +01:00
const events = new Events();
const result = events.onMessage(message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command failed");
});
test('OnReady_ExpectConsoleLog', () => {
console.log = jest.fn();
const events = new Events();
events.onReady();
expect(console.log).toBeCalledWith("Ready");
});