Feature/12 create tests #102

Merged
Vylpes merged 25 commits from feature/12-create-tests into develop 2022-01-30 17:03:37 +00:00
3 changed files with 71 additions and 0 deletions
Showing only changes of commit aa81b125ac - Show all commits

View file

@ -15,6 +15,14 @@ export class CoreClient extends Client {
private _events: Events;
private _util: Util;
public get commandItems(): ICommandItem[] {
return this._commandItems;
}
public get eventItems(): IEventItem[] {
return this._eventItems;
}
constructor() {
super();
dotenv.config();

View file

@ -4,6 +4,11 @@ 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 ICommandItem from "../../src/contracts/ICommandItem";
import { mock } from "jest-mock-extended";
import IEventItem from "../../src/contracts/IEventItem";
import { Event } from "../../src/type/event";
jest.mock("discord.js");
jest.mock("dotenv");
@ -137,3 +142,28 @@ describe('Start', () => {
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
});
});
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);
});
});

View file

@ -302,6 +302,39 @@ describe('LoadCommand', () => {
expect(cmd.execute).toBeCalled();
expect(otherCmd.execute).not.toBeCalled();
});
test('Given command is not found in register, expect command not found error', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const commands: ICommandItem[] = [];
const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe('Command not found');
expect(message.reply).toBeCalledWith('Command not found');
});
});
describe('LoadEvents', () => {