From aa81b125ac30cd60d37a4bd89ad27332cdc504ca Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 24 Dec 2021 16:20:58 +0000 Subject: [PATCH] Update coverage --- src/client/client.ts | 8 ++++++++ tests/client/client.test.ts | 30 ++++++++++++++++++++++++++++++ tests/client/util.test.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/src/client/client.ts b/src/client/client.ts index f6c992b..c52734c 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -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(); diff --git a/tests/client/client.test.ts b/tests/client/client.test.ts index 712cb41..bf826d1 100644 --- a/tests/client/client.test.ts +++ b/tests/client/client.test.ts @@ -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"); @@ -136,4 +141,29 @@ describe('Start', () => { expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env"); }); +}); + +describe('RegisterCommand', () => { + test('Expect command added to register', () => { + const cmd = mock(); + + 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(); + + const client = new CoreClient(); + client.RegisterEvent(evt); + + expect(client.eventItems.length).toBe(1); + expect(client.eventItems[0].Event).toBe(evt); + }); }); \ No newline at end of file diff --git a/tests/client/util.test.ts b/tests/client/util.test.ts index e5c8462..b1da815 100644 --- a/tests/client/util.test.ts +++ b/tests/client/util.test.ts @@ -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', () => {