Feature/12 create tests #102
2 changed files with 168 additions and 164 deletions
|
@ -1,7 +1,9 @@
|
|||
import { Events } from "../../src/client/events";
|
||||
|
||||
import { Message, Client, TextChannel, Guild, SnowflakeUtil, DMChannel } from "discord.js";
|
||||
import { Util } from "../../src/client/util";
|
||||
import ICommandItem from "../../src/contracts/ICommandItem";
|
||||
import { Command } from "../../src/type/command";
|
||||
import { mock } from "jest-mock-extended";
|
||||
|
||||
jest.mock("../../src/client/util");
|
||||
|
||||
|
@ -28,9 +30,18 @@ describe('OnMessage', () => {
|
|||
content: "!test first",
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const events = new Events();
|
||||
|
||||
const result = await events.onMessage(message);
|
||||
const result = await events.onMessage(message, commands);
|
||||
|
||||
expect(result.valid).toBeTruthy();
|
||||
|
||||
|
@ -59,9 +70,18 @@ describe('OnMessage', () => {
|
|||
content: "!test first",
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const events = new Events();
|
||||
|
||||
const result = await events.onMessage(message);
|
||||
const result = await events.onMessage(message, commands);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Message was not sent in a guild, ignoring.");
|
||||
|
@ -85,9 +105,18 @@ describe('OnMessage', () => {
|
|||
content: "!test first",
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const events = new Events();
|
||||
|
||||
const result = await events.onMessage(message);
|
||||
const result = await events.onMessage(message, commands);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Message was sent by a bot, ignoring.");
|
||||
|
@ -111,9 +140,18 @@ describe('OnMessage', () => {
|
|||
content: "This is a standard message",
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const events = new Events();
|
||||
|
||||
const result = await events.onMessage(message);
|
||||
const result = await events.onMessage(message, commands);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Message was not a command, ignoring.");
|
||||
|
@ -137,9 +175,18 @@ describe('OnMessage', () => {
|
|||
content: "!",
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const events = new Events();
|
||||
|
||||
const result = await events.onMessage(message);
|
||||
const result = await events.onMessage(message, commands);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Command name was not found");
|
||||
|
@ -163,9 +210,18 @@ describe('OnMessage', () => {
|
|||
content: "!test first",
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const events = new Events();
|
||||
|
||||
const result = await events.onMessage(message);
|
||||
const result = await events.onMessage(message, commands);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Command failed");
|
||||
|
|
|
@ -2,6 +2,11 @@ import { Util } from "../../src/client/util";
|
|||
|
||||
import { Client, Guild, Message, Role, SnowflakeUtil, TextChannel, User } from "discord.js";
|
||||
import fs from "fs";
|
||||
import { mock } from "jest-mock-extended";
|
||||
import { Command } from "../../src/type/command";
|
||||
import ICommandItem from "../../src/contracts/ICommandItem";
|
||||
import IEventItem from "../../src/contracts/IEventItem";
|
||||
import { Event } from "../../src/type/event";
|
||||
|
||||
jest.mock("fs");
|
||||
|
||||
|
@ -32,11 +37,21 @@ describe('LoadCommand', () => {
|
|||
reply: jest.fn(),
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadCommand("normal", [ "first" ], message);
|
||||
const result = util.loadCommand("test", [ "first" ], message, commands);
|
||||
|
||||
expect(result.valid).toBeTruthy();
|
||||
expect(cmd.execute).toBeCalled();
|
||||
});
|
||||
|
||||
test('Given Member Is Null, Expect Failed Result', () => {
|
||||
|
@ -54,73 +69,22 @@ describe('LoadCommand', () => {
|
|||
member: null
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadCommand("normal", [ "first" ], message);
|
||||
const result = util.loadCommand("test", [ "first" ], message, commands);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Member is not part of message");
|
||||
});
|
||||
|
||||
test('Given Folder Does Not Exist, Expect Failed Result', () => {
|
||||
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(false);
|
||||
|
||||
const message = {
|
||||
member: {
|
||||
roles: {
|
||||
cache: {
|
||||
find: jest.fn().mockReturnValue(true),
|
||||
}
|
||||
},
|
||||
},
|
||||
reply: jest.fn(),
|
||||
} as unknown as Message;
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadCommand("normal", [ "first" ], message);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Command folder does not exist");
|
||||
});
|
||||
|
||||
test('Given File Does Not Exist, Expect Failed Result', () => {
|
||||
process.env = {
|
||||
BOT_TOKEN: 'TOKEN',
|
||||
BOT_PREFIX: '!',
|
||||
FOLDERS_COMMANDS: 'commands',
|
||||
FOLDERS_EVENTS: 'events',
|
||||
}
|
||||
|
||||
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
||||
fs.existsSync = jest.fn().mockReturnValueOnce(true)
|
||||
.mockReturnValue(false);
|
||||
|
||||
const message = {
|
||||
member: {
|
||||
roles: {
|
||||
cache: {
|
||||
find: jest.fn().mockReturnValue(true),
|
||||
}
|
||||
},
|
||||
},
|
||||
reply: jest.fn(),
|
||||
} as unknown as Message;
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadCommand("normal", [ "first" ], message);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("File does not exist");
|
||||
expect(cmd.execute).not.toBeCalled();
|
||||
});
|
||||
|
||||
test('Given User Does Have Role, Expect Successful Result', () => {
|
||||
|
@ -145,11 +109,21 @@ describe('LoadCommand', () => {
|
|||
reply: jest.fn(),
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadCommand("roles", [ "first" ], message);
|
||||
const result = util.loadCommand("test", [ "first" ], message, commands);
|
||||
|
||||
expect(result.valid).toBeTruthy();
|
||||
expect(cmd.execute).toBeCalled();
|
||||
});
|
||||
|
||||
test('Given User Does Not Have Role, Expect Failed Result', () => {
|
||||
|
@ -174,41 +148,23 @@ describe('LoadCommand', () => {
|
|||
reply: jest.fn(),
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
cmd._roles = [ "Moderator" ];
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadCommand("roles", [ "first" ], message);
|
||||
const result = util.loadCommand("test", [ "first" ], message, commands);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("You require the `Moderator` role to run this command");
|
||||
});
|
||||
|
||||
test('Given Command Category Is Null, Expect Successful Result', () => {
|
||||
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 util = new Util();
|
||||
|
||||
const result = util.loadCommand("noCategory", [ "first" ], message);
|
||||
|
||||
expect(result.valid).toBeTruthy();
|
||||
expect(cmd.execute).not.toBeCalled();
|
||||
});
|
||||
|
||||
test('Given command is set to disabled, Expect command to not fire', () => {
|
||||
|
@ -217,7 +173,7 @@ describe('LoadCommand', () => {
|
|||
BOT_PREFIX: '!',
|
||||
FOLDERS_COMMANDS: 'commands',
|
||||
FOLDERS_EVENTS: 'events',
|
||||
COMMANDS_DISABLED: 'normal',
|
||||
COMMANDS_DISABLED: 'test',
|
||||
COMMANDS_DISABLED_MESSAGE: 'disabled',
|
||||
}
|
||||
|
||||
|
@ -237,13 +193,23 @@ describe('LoadCommand', () => {
|
|||
|
||||
const messageReply = jest.spyOn(message, 'reply');
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadCommand("normal", [ "first" ], message);
|
||||
const result = util.loadCommand("test", [ "first" ], message, commands);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Command is disabled");
|
||||
expect(messageReply).toBeCalledWith("disabled");
|
||||
expect(cmd.execute).not.toBeCalled();
|
||||
});
|
||||
|
||||
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', () => {
|
||||
|
@ -252,7 +218,7 @@ describe('LoadCommand', () => {
|
|||
BOT_PREFIX: '!',
|
||||
FOLDERS_COMMANDS: 'commands',
|
||||
FOLDERS_EVENTS: 'events',
|
||||
COMMANDS_DISABLED: 'normal',
|
||||
COMMANDS_DISABLED: 'test',
|
||||
}
|
||||
|
||||
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
||||
|
@ -271,13 +237,23 @@ describe('LoadCommand', () => {
|
|||
|
||||
const messageReply = jest.spyOn(message, 'reply');
|
||||
|
||||
const cmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem ];
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadCommand("normal", [ "first" ], message);
|
||||
const result = util.loadCommand("test", [ "first" ], message, commands);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Command is disabled");
|
||||
expect(messageReply).toBeCalledWith("This command is disabled.");
|
||||
expect(cmd.execute).not.toBeCalled();
|
||||
});
|
||||
|
||||
test('Given a different command is disabled, Expect command to still fire', () => {
|
||||
|
@ -286,7 +262,7 @@ describe('LoadCommand', () => {
|
|||
BOT_PREFIX: '!',
|
||||
FOLDERS_COMMANDS: 'commands',
|
||||
FOLDERS_EVENTS: 'events',
|
||||
COMMANDS_DISABLED: 'anything',
|
||||
COMMANDS_DISABLED: 'other',
|
||||
}
|
||||
|
||||
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
||||
|
@ -303,42 +279,28 @@ describe('LoadCommand', () => {
|
|||
reply: jest.fn(),
|
||||
} as unknown as Message;
|
||||
|
||||
const cmd = mock<Command>();
|
||||
const otherCmd = mock<Command>();
|
||||
|
||||
const commandItem: ICommandItem = {
|
||||
Name: "test",
|
||||
Command: cmd
|
||||
};
|
||||
|
||||
const otherCommandItem: ICommandItem = {
|
||||
Name: "other",
|
||||
Command: otherCmd,
|
||||
}
|
||||
|
||||
const commands: ICommandItem[] = [ commandItem, otherCommandItem ];
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadCommand("normal", [ "first" ], message);
|
||||
const result = util.loadCommand("test", [ "first" ], message, commands);
|
||||
|
||||
expect(result.valid).toBeTruthy();
|
||||
});
|
||||
|
||||
test('Given a different command is disabled with this one, Expect command to not fire', () => {
|
||||
process.env = {
|
||||
BOT_TOKEN: 'TOKEN',
|
||||
BOT_PREFIX: '!',
|
||||
FOLDERS_COMMANDS: 'commands',
|
||||
FOLDERS_EVENTS: 'events',
|
||||
COMMANDS_DISABLED: 'normal,anything,',
|
||||
}
|
||||
|
||||
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 util = new Util();
|
||||
|
||||
const result = util.loadCommand("normal", [ "first" ], message);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Command is disabled");
|
||||
expect(cmd.execute).toBeCalled();
|
||||
expect(otherCmd.execute).not.toBeCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -359,9 +321,17 @@ describe('LoadEvents', () => {
|
|||
on: jest.fn(),
|
||||
} as unknown as Client;
|
||||
|
||||
const evt = mock<Event>();
|
||||
|
||||
const eventItem: IEventItem = {
|
||||
Event: evt
|
||||
};
|
||||
|
||||
const eventItems: IEventItem[] = [ eventItem ];
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadEvents(client);
|
||||
const result = util.loadEvents(client, eventItems);
|
||||
|
||||
const clientOn = jest.spyOn(client, 'on');
|
||||
|
||||
|
@ -385,37 +355,15 @@ describe('LoadEvents', () => {
|
|||
on: jest.fn(),
|
||||
} as unknown as Client;
|
||||
|
||||
const eventItems: IEventItem[] = [];
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadEvents(client);
|
||||
const result = util.loadEvents(client, eventItems);
|
||||
|
||||
const clientOn = jest.spyOn(client, 'on');
|
||||
|
||||
expect(result.valid).toBeTruthy();
|
||||
expect(clientOn).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
test('Given Event Folder Does Not Exist, Expect Failed Result', () => {
|
||||
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(false);
|
||||
fs.readdirSync = jest.fn().mockReturnValue(["normal.ts"]);
|
||||
|
||||
const client = {
|
||||
on: jest.fn(),
|
||||
} as unknown as Client;
|
||||
|
||||
const util = new Util();
|
||||
|
||||
const result = util.loadEvents(client);
|
||||
|
||||
expect(result.valid).toBeFalsy();
|
||||
expect(result.message).toBe("Event folder does not exist");
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue