From 451bfe1039d6e8a09c3a5efadc09be9359318a53 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 2 Jan 2022 14:56:08 +0000 Subject: [PATCH] Add help command tests --- jest.config.js | 1 + jest.setup.js | 3 ++ src/commands/help.ts | 14 +------ tests/_mocks/commands/mockCmd.ts | 10 +++++ tests/commands/help.test.ts | 71 ++++++++++++++++++++++++++++++-- 5 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 jest.setup.js create mode 100644 tests/_mocks/commands/mockCmd.ts diff --git a/jest.config.js b/jest.config.js index 5839600..641ea7d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,4 +2,5 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', + setupFiles: ["./jest.setup.js"] }; \ No newline at end of file diff --git a/jest.setup.js b/jest.setup.js new file mode 100644 index 0000000..d583d1a --- /dev/null +++ b/jest.setup.js @@ -0,0 +1,3 @@ +jest.setTimeout(1 * 1000); // 1 second +jest.resetModules(); +jest.resetAllMocks(); \ No newline at end of file diff --git a/src/commands/help.ts b/src/commands/help.ts index 9e40567..aaf8f7e 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -30,7 +30,7 @@ export default class Help extends Command { public SendAll(context: ICommandContext): ICommandReturnContext { const allCommands = this.GetAllCommandData(); - const cateogries = this.DetermineCategories(allCommands); + const cateogries = [...new Set(allCommands.map(x => x.Category!))];; const embed = new PublicEmbed(context, "Commands", ""); @@ -119,16 +119,4 @@ export default class Help extends Command { return data; } - - public DetermineCategories(commands: ICommandData[]): string[] { - const result: string[] = []; - - commands.forEach(cmd => { - if (!result.includes(cmd.Category!)) { - result.push(cmd.Category!); - } - }); - - return result; - } } diff --git a/tests/_mocks/commands/mockCmd.ts b/tests/_mocks/commands/mockCmd.ts new file mode 100644 index 0000000..16f1947 --- /dev/null +++ b/tests/_mocks/commands/mockCmd.ts @@ -0,0 +1,10 @@ +import { Command } from "../../../src/type/command"; + +export default class MockCmd extends Command { + constructor() { + super(); + + super._category = "General"; + super._roles = ["Moderator"]; + } +} \ No newline at end of file diff --git a/tests/commands/help.test.ts b/tests/commands/help.test.ts index 6eab047..8a0fa32 100644 --- a/tests/commands/help.test.ts +++ b/tests/commands/help.test.ts @@ -2,6 +2,8 @@ import Help, { ICommandData } from "../../src/commands/help"; import { Message } from "discord.js"; import { ICommandContext } from "../../src/contracts/ICommandContext"; +const oldCwd = process.cwd(); + describe('Constructor', () => { test('Expect properties to be set', () => { const help = new Help(); @@ -86,13 +88,10 @@ describe('SendAll', () => { help.GetAllCommandData = jest.fn() .mockReturnValue([commandData0, commandData1]); - help.DetermineCategories = jest.fn() - .mockReturnValue(['general']); const result = help.SendAll(context); expect(help.GetAllCommandData).toBeCalled(); - expect(help.DetermineCategories).toBeCalled(); expect(messageChannelSend).toBeCalled(); expect(result.embeds.length).toBe(1); @@ -200,3 +199,69 @@ describe('SendSingle', () => { expect(errorEmbed.description).toBe('Command does not exist'); }); }); + +describe('GetAllCommandData', () => { + test('Expect array of command data to be returned', () => { + process.env = { + FOLDERS_COMMANDS: "commands" + }; + + process.cwd = jest.fn() + .mockReturnValue(`${oldCwd}/tests/_mocks`); + + const help = new Help(); + + const result = help.GetAllCommandData(); + + expect(result.length).toBe(1); + + // Mock Command + const mockCommand = result[0]; + + expect(mockCommand.Exists).toBeTruthy(); + expect(mockCommand.Name).toBe("mockCmd"); + expect(mockCommand.Category).toBe("General"); + + expect(mockCommand.Roles!.length).toBe(1); + expect(mockCommand.Roles![0]).toBe("Moderator"); + }); +}); + +describe('GetCommandData', () => { + test('Given command exists, expect data to be returned', () => { + process.env = { + FOLDERS_COMMANDS: "commands" + }; + + process.cwd = jest.fn() + .mockReturnValue(`${oldCwd}/tests/_mocks`); + + const help = new Help(); + + const result = help.GetCommandData('mockCmd'); + + expect(result.Exists).toBeTruthy(); + expect(result.Name).toBe("mockCmd"); + expect(result.Category).toBe("General"); + + expect(result.Roles!.length).toBe(1); + expect(result.Roles![0]).toBe("Moderator"); + }); + + test('Given command does not exist, expect exists false to be returned', () => { + process.env = { + FOLDERS_COMMANDS: "commands" + }; + + const oldCwd = process.cwd(); + + process.cwd = jest.fn() + .mockReturnValue(`${oldCwd}/tests/_mocks`); + + const help = new Help(); + + const result = help.GetCommandData('none'); + + expect(result.Exists).toBeFalsy(); + }); +});