vylbot-app/tests/client/client.test.ts

153 lines
4.5 KiB
TypeScript
Raw Permalink Normal View History

v3.0 (#145) * Change rules.txt to rules.json (#31) * Migrate to yarn * Add role configs to config template * Install packges and setup typescript * Migrate entry point * Migrate about command * Migrate ban command * Migrate clear command * Migrate kick command * Migrate mute command * Migrate poll command * Migrate bunny command * Update required roles checker * Migrate role command * Migrate unmute command * Migrate warn command * Migrate eval command * Migrate help command * Migrate rules command * Migrate events to typescript * Update about command to use the PublicEmbed class * Update ErrorMessage to ChannelNotFound * Update messageDelete event to ignore bots * Feature/74 merge vylbot core (#80) * Merge VylBot-Core * Update commands to new system * Fix issue where events would not load * Feature/12 create tests (#102) * 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> * Update rules with blog website and event spoilers rule" (#106) Signed-off-by: Ethan Lane <ethan@vylpes.com> * Containerise bot (#107) * Add moderator names to audit reason (#108) * Feature/48 database (#114) * Add database and default values * Add ability to save a setting to the database * Get commands and events to use database * Setup and config command * Update commands to check roles per server * Different rules per server Signed-off-by: Ethan Lane <ethan@vylpes.com> * Different prefix per server Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add verification system Signed-off-by: Ethan Lane <ethan@vylpes.com> * Disabled commands per server * Add devmode for default prefix * Update embeds * Fix broken tests * Feature/66 add different commands per server (#122) * Add ability for server exclusive commands * Add MankBot server-exclusive commands * Add lobby entity to database * Add documentation * Add setup command for lobby (#123) * Update bot to discord.js v13 (#125) * Update bot to discord.js v13 * Remove debug code * 110 commandshelp about command errors which causes command to not run (#126) * Change onMessage to onMessageCreate * Fix help command * Add override for bot owner and server owner (#135) * Change help command so exclusive commands can only be seen for the server they're assigned to (#136) * Change parsing to not crash if invalid (#142) * 137 role command cannot read properties of undefined (#141) * Fix issue with bot crashing * Fix server prefix not showing * Add easy way to configure role command * Move help text to its own directory * Make role config command to use role id * Get lobby command to use IDs instead of names (#144) Co-authored-by: Vylpes <getgravitysoftware@gmail.com>
2022-04-24 14:46:37 +01:00
import { mock } from "jest-mock-extended";
const connectionMock = mock<Connection>();
const qbuilderMock = mock<SelectQueryBuilder<any>>();
let repositoryMock = mock<Repository<any>>();
let settingMock = mock<Setting>();
jest.mock('typeorm', () => {
qbuilderMock.where.mockReturnThis();
qbuilderMock.select.mockReturnThis();
repositoryMock.createQueryBuilder.mockReturnValue(qbuilderMock);
repositoryMock.findOne.mockImplementation(async () => {
return settingMock;
});
connectionMock.getRepository.mockReturnValue(repositoryMock);
return {
getConnection: () => connectionMock,
createConnection: () => connectionMock,
BaseEntity: class Mock {},
ObjectType: () => {},
Entity: () => {},
InputType: () => {},
Index: () => {},
PrimaryColumn: () => {},
Column: () => {},
CreateDateColumn: () => {},
UpdateDateColumn: () => {},
OneToMany: () => {},
ManyToOne: () => {},
}
});
jest.mock("discord.js");
jest.mock("dotenv");
jest.mock("../../src/client/events");
jest.mock("../../src/client/util");
jest.mock("../../src/constants/DefaultValues");
import { CoreClient } from "../../src/client/client";
import { Client } from "discord.js";
import * as dotenv from "dotenv";
import { Events } from "../../src/client/events";
import { Util } from "../../src/client/util";
import { Command } from "../../src/type/command";
import { Event } from "../../src/type/event";
import DefaultValues from "../../src/constants/DefaultValues";
import { Connection, Repository, SelectQueryBuilder } from "typeorm";
import Setting from "../../src/entity/Setting";
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
})
describe('Constructor', () => {
test('Expect Successful Initialisation', () => {
const coreClient = new CoreClient();
expect(coreClient).toBeInstanceOf(Client);
expect(dotenv.config).toBeCalledTimes(1);
expect(Events).toBeCalledTimes(1);
expect(Util).toBeCalledTimes(1);
expect(DefaultValues.useDevPrefix).toBe(false);
});
test('Given devmode parameter is true, Expect devmode prefix to be true', () => {
const coreClient = new CoreClient(true);
expect(coreClient).toBeInstanceOf(Client);
expect(dotenv.config).toBeCalledTimes(1);
expect(Events).toBeCalledTimes(1);
expect(Util).toBeCalledTimes(1);
expect(DefaultValues.useDevPrefix).toBe(true);
});
});
describe('Start', () => {
test('Given Env Is Valid, Expect Successful Start', async () => {
process.env = {
BOT_TOKEN: "TOKEN",
};
const coreClient = new CoreClient();
await coreClient.start();
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
});
test('Given BOT_TOKEN Is Null, Expect Failure', async () => {
process.env = {};
const consoleError = jest.fn();
console.error = consoleError;
const coreClient = new CoreClient();
await coreClient.start();
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
expect(coreClient.on).not.toBeCalled();
expect(coreClient.login).not.toBeCalled();
});
test('Given BOT_TOKEN Is Empty, Expect Failure', async () => {
process.env = {
BOT_TOKEN: '',
}
const consoleError = jest.fn();
console.error = consoleError;
const coreClient = new CoreClient();
await coreClient.start();
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
expect(coreClient.on).not.toBeCalled();
expect(coreClient.login).not.toBeCalled();
});
});
describe('RegisterCommand', () => {
test('Expect command added to register', () => {
const cmd = mock<Command>();
const client = new CoreClient();
client.RegisterCommand("test", cmd);
expect(client.commandItems.length).toBe(1);
expect(client.commandItems[0].Name).toBe("test");
expect(client.commandItems[0].Command).toBe(cmd);
});
});
describe('RegisterEvent', () => {
test('Expect event added to register', () => {
const evt = mock<Event>();
const client = new CoreClient();
client.RegisterEvent(evt);
expect(client.eventItems.length).toBe(1);
expect(client.eventItems[0].Event).toBe(evt);
});
});