Vylpes
f61c4c728a
* 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>
241 lines
6.6 KiB
TypeScript
241 lines
6.6 KiB
TypeScript
import { Events } from "../../src/client/events";
|
|
import { Message } from "discord.js";
|
|
import { Util } from "../../src/client/util";
|
|
import ICommandItem from "../../src/contracts/ICommandItem";
|
|
import { Command } from "../../src/type/command";
|
|
import { mock } from "jest-mock-extended";
|
|
|
|
jest.mock("../../src/client/util");
|
|
|
|
beforeEach(() => {
|
|
Util.prototype.loadCommand = jest.fn();
|
|
});
|
|
|
|
describe('OnMessage', () => {
|
|
test('Given Message Is Valid Expect Message Sent', async () => {
|
|
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 cmd = mock<Command>();
|
|
|
|
const commandItem: ICommandItem = {
|
|
Name: "test",
|
|
Command: cmd
|
|
};
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
|
|
|
const events = new Events();
|
|
|
|
const result = await events.onMessage(message, commands);
|
|
|
|
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);
|
|
});
|
|
|
|
test('Given Guild Is Null, Expect Failed Result', async () => {
|
|
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 cmd = mock<Command>();
|
|
|
|
const commandItem: ICommandItem = {
|
|
Name: "test",
|
|
Command: cmd
|
|
};
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
|
|
|
const events = new Events();
|
|
|
|
const result = await events.onMessage(message, commands);
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
expect(result.message).toBe("Message was not sent in a guild, ignoring.");
|
|
});
|
|
|
|
test('Given Author Is A Bot, Expect Failed Result', async () => {
|
|
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 cmd = mock<Command>();
|
|
|
|
const commandItem: ICommandItem = {
|
|
Name: "test",
|
|
Command: cmd
|
|
};
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
|
|
|
const events = new Events();
|
|
|
|
const result = await events.onMessage(message, commands);
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
expect(result.message).toBe("Message was sent by a bot, ignoring.");
|
|
});
|
|
|
|
test('Given Message Content Was Not A Command, Expect Failed Result', async () => {
|
|
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 cmd = mock<Command>();
|
|
|
|
const commandItem: ICommandItem = {
|
|
Name: "test",
|
|
Command: cmd
|
|
};
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
|
|
|
const events = new Events();
|
|
|
|
const result = await events.onMessage(message, commands);
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
expect(result.message).toBe("Message was not a command, ignoring.");
|
|
});
|
|
|
|
test('Given Message Had No Command Name, Expect Failed Result', async () => {
|
|
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 cmd = mock<Command>();
|
|
|
|
const commandItem: ICommandItem = {
|
|
Name: "test",
|
|
Command: cmd
|
|
};
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
|
|
|
const events = new Events();
|
|
|
|
const result = await events.onMessage(message, commands);
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
expect(result.message).toBe("Command name was not found");
|
|
});
|
|
|
|
test('Given Command Failed To Execute, Expect Failed Result', async () => {
|
|
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 cmd = mock<Command>();
|
|
|
|
const commandItem: ICommandItem = {
|
|
Name: "test",
|
|
Command: cmd
|
|
};
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
|
|
|
const events = new Events();
|
|
|
|
const result = await events.onMessage(message, commands);
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
expect(result.message).toBe("Command failed");
|
|
});
|
|
});
|
|
|
|
describe('OnReady', () => {
|
|
test('Expect Console Log', () => {
|
|
console.log = jest.fn();
|
|
|
|
const events = new Events();
|
|
|
|
events.onReady();
|
|
|
|
expect(console.log).toBeCalledWith("Ready");
|
|
});
|
|
});
|