2022-03-27 10:12:19 +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");
|
|
|
|
|
2021-12-24 14:55:28 +00:00
|
|
|
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";
|
2022-01-30 17:03:36 +00:00
|
|
|
import { Command } from "../../src/type/command";
|
|
|
|
import { Event } from "../../src/type/event";
|
2022-03-27 10:12:19 +01:00
|
|
|
import DefaultValues from "../../src/constants/DefaultValues";
|
|
|
|
import { Connection, Repository, SelectQueryBuilder } from "typeorm";
|
|
|
|
import Setting from "../../src/entity/Setting";
|
2021-12-24 14:55:28 +00:00
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
jest.resetModules();
|
|
|
|
})
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
describe('Constructor', () => {
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Expect Successful Initialisation', () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
const coreClient = new CoreClient();
|
|
|
|
|
|
|
|
expect(coreClient).toBeInstanceOf(Client);
|
|
|
|
expect(dotenv.config).toBeCalledTimes(1);
|
|
|
|
expect(Events).toBeCalledTimes(1);
|
|
|
|
expect(Util).toBeCalledTimes(1);
|
2022-03-27 10:12:19 +01:00
|
|
|
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);
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Start', () => {
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given Env Is Valid, Expect Successful Start', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
2022-03-27 10:12:19 +01:00
|
|
|
BOT_TOKEN: "TOKEN",
|
|
|
|
};
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const coreClient = new CoreClient();
|
2022-03-27 10:12:19 +01:00
|
|
|
|
|
|
|
await coreClient.start();
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
|
|
|
|
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
|
|
|
|
});
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given BOT_TOKEN Is Null, Expect Failure', async () => {
|
|
|
|
process.env = {};
|
|
|
|
|
|
|
|
const consoleError = jest.fn();
|
|
|
|
|
|
|
|
console.error = consoleError;
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const coreClient = new CoreClient();
|
2022-03-27 10:12:19 +01:00
|
|
|
|
|
|
|
await coreClient.start();
|
|
|
|
|
|
|
|
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
|
|
|
|
expect(coreClient.on).not.toBeCalled();
|
|
|
|
expect(coreClient.login).not.toBeCalled();
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given BOT_TOKEN Is Empty, Expect Failure', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: '',
|
|
|
|
}
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
const consoleError = jest.fn();
|
|
|
|
|
|
|
|
console.error = consoleError;
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const coreClient = new CoreClient();
|
2022-03-27 10:12:19 +01:00
|
|
|
|
|
|
|
await coreClient.start();
|
|
|
|
|
|
|
|
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
|
|
|
|
expect(coreClient.on).not.toBeCalled();
|
|
|
|
expect(coreClient.login).not.toBeCalled();
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
2022-01-30 17:03:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|