Update command names, add describe categories
This commit is contained in:
parent
bb848dcec3
commit
7e623b4b64
3 changed files with 548 additions and 536 deletions
|
@ -10,126 +10,130 @@ jest.mock("dotenv");
|
||||||
jest.mock("../../src/client/events");
|
jest.mock("../../src/client/events");
|
||||||
jest.mock("../../src/client/util");
|
jest.mock("../../src/client/util");
|
||||||
|
|
||||||
test('Constructor_ExpectSuccessfulInitialisation', () => {
|
describe('Constructor', () => {
|
||||||
const coreClient = new CoreClient();
|
test('Constructor_ExpectSuccessfulInitialisation', () => {
|
||||||
|
const coreClient = new CoreClient();
|
||||||
expect(coreClient).toBeInstanceOf(Client);
|
|
||||||
expect(dotenv.config).toBeCalledTimes(1);
|
expect(coreClient).toBeInstanceOf(Client);
|
||||||
expect(Events).toBeCalledTimes(1);
|
expect(dotenv.config).toBeCalledTimes(1);
|
||||||
expect(Util).toBeCalledTimes(1);
|
expect(Events).toBeCalledTimes(1);
|
||||||
|
expect(Util).toBeCalledTimes(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Start_GivenEnvIsValid_ExpectSuccessfulStart', () => {
|
describe('Start', () => {
|
||||||
process.env = {
|
test('Given Env Is Valid, Expect Successful Start', () => {
|
||||||
BOT_TOKEN: 'TOKEN',
|
process.env = {
|
||||||
BOT_PREFIX: '!',
|
BOT_TOKEN: 'TOKEN',
|
||||||
FOLDERS_COMMANDS: 'commands',
|
BOT_PREFIX: '!',
|
||||||
FOLDERS_EVENTS: 'events',
|
FOLDERS_COMMANDS: 'commands',
|
||||||
}
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(() => coreClient.start()).not.toThrow();
|
const coreClient = new CoreClient();
|
||||||
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
|
|
||||||
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
|
expect(() => coreClient.start()).not.toThrow();
|
||||||
});
|
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
|
||||||
|
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
|
||||||
test('Start_GivenBotTokenIsNull_ExpectFailure', () => {
|
});
|
||||||
process.env = {
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
|
test('Given BOT_TOKEN Is Null, Expect Failure', () => {
|
||||||
});
|
process.env = {
|
||||||
|
BOT_PREFIX: '!',
|
||||||
test('Start_GivenBotTokenIsEmpty_ExpectFailure', () => {
|
FOLDERS_COMMANDS: 'commands',
|
||||||
process.env = {
|
FOLDERS_EVENTS: 'events',
|
||||||
BOT_TOKEN: '',
|
}
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
|
const coreClient = new CoreClient();
|
||||||
});
|
|
||||||
|
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
|
||||||
test('Start_GivenBotPrefixIsNull_ExpectFailure', () => {
|
});
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
|
test('Given BOT_TOKEN Is Empty, Expect Failure', () => {
|
||||||
});
|
process.env = {
|
||||||
|
BOT_TOKEN: '',
|
||||||
test('Start_GivenBotPrefixIsEmpty_ExpectFailure', () => {
|
BOT_PREFIX: '!',
|
||||||
process.env = {
|
FOLDERS_COMMANDS: 'commands',
|
||||||
BOT_TOKEN: 'TOKEN',
|
FOLDERS_EVENTS: 'events',
|
||||||
BOT_PREFIX: '',
|
}
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
|
const coreClient = new CoreClient();
|
||||||
});
|
|
||||||
|
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
|
||||||
test('Start_GivenFoldersCommandsIsNull_ExpectFailure', () => {
|
});
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
|
test('Given BOT_PREFIX Is Null, Expect Failure', () => {
|
||||||
});
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
test('Start_GivenFoldersCommandsIsEmpty_ExpectFailure', () => {
|
FOLDERS_COMMANDS: 'commands',
|
||||||
process.env = {
|
FOLDERS_EVENTS: 'events',
|
||||||
BOT_TOKEN: 'TOKEN',
|
}
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: '',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
|
const coreClient = new CoreClient();
|
||||||
});
|
|
||||||
|
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
|
||||||
test('Start_GivenFoldersEventsIsNull_ExpectFailure', () => {
|
});
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
}
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
|
test('Given BOT_PREFIX Is Empty, Expect Failure', () => {
|
||||||
});
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
test('Start_GivenFoldersCommandsIsEmpty_ExpectFailure', () => {
|
BOT_PREFIX: '',
|
||||||
process.env = {
|
FOLDERS_COMMANDS: 'commands',
|
||||||
BOT_TOKEN: 'TOKEN',
|
FOLDERS_EVENTS: 'events',
|
||||||
BOT_PREFIX: '!',
|
}
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: '',
|
|
||||||
}
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
|
const coreClient = new CoreClient();
|
||||||
|
|
||||||
|
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given FOLDERS_COMMANDS Is Null, Expect Failure', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
const coreClient = new CoreClient();
|
||||||
|
|
||||||
|
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given FOLDERS_COMMANDS Is Empty, Expect Failure', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: '',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
const coreClient = new CoreClient();
|
||||||
|
|
||||||
|
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given FOLDERS_EVENTS Is Null, Expect Failure', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
}
|
||||||
|
|
||||||
|
const coreClient = new CoreClient();
|
||||||
|
|
||||||
|
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given FOLDERS_EVENTS Is Empty, Expect Failure', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: '',
|
||||||
|
}
|
||||||
|
|
||||||
|
const coreClient = new CoreClient();
|
||||||
|
|
||||||
|
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
|
||||||
|
});
|
||||||
});
|
});
|
|
@ -9,173 +9,177 @@ beforeEach(() => {
|
||||||
Util.prototype.loadCommand = jest.fn();
|
Util.prototype.loadCommand = jest.fn();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('OnMessage_GivenMessageIsValid_ExpectMessageSent', () => {
|
describe('OnMessage', () => {
|
||||||
process.env = {
|
test('Given Message Is Valid Expect Message Sent', () => {
|
||||||
BOT_TOKEN: 'TOKEN',
|
process.env = {
|
||||||
BOT_PREFIX: '!',
|
BOT_TOKEN: 'TOKEN',
|
||||||
FOLDERS_COMMANDS: 'commands',
|
BOT_PREFIX: '!',
|
||||||
FOLDERS_EVENTS: 'events',
|
FOLDERS_COMMANDS: 'commands',
|
||||||
};
|
FOLDERS_EVENTS: 'events',
|
||||||
|
};
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
const message = {
|
|
||||||
guild: {},
|
const message = {
|
||||||
author: {
|
guild: {},
|
||||||
bot: false,
|
author: {
|
||||||
},
|
bot: false,
|
||||||
content: "!test first",
|
},
|
||||||
} as unknown as Message;
|
content: "!test first",
|
||||||
|
} as unknown as Message;
|
||||||
const events = new Events();
|
|
||||||
|
const events = new Events();
|
||||||
const result = events.onMessage(message);
|
|
||||||
|
const result = events.onMessage(message);
|
||||||
expect(result.valid).toBeTruthy();
|
|
||||||
|
expect(result.valid).toBeTruthy();
|
||||||
expect(result.context?.prefix).toBe('!');
|
|
||||||
expect(result.context?.name).toBe('test');
|
expect(result.context?.prefix).toBe('!');
|
||||||
expect(result.context?.args.length).toBe(1);
|
expect(result.context?.name).toBe('test');
|
||||||
expect(result.context?.args[0]).toBe('first');
|
expect(result.context?.args.length).toBe(1);
|
||||||
expect(result.context?.message).toBe(message);
|
expect(result.context?.args[0]).toBe('first');
|
||||||
|
expect(result.context?.message).toBe(message);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given Guild Is Null, Expect Failed Result', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: null,
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
},
|
||||||
|
content: "!test first",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const events = new Events();
|
||||||
|
|
||||||
|
const result = events.onMessage(message);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Message was not sent in a guild, ignoring.");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given Author Is A Bot, Expect Failed Result', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {},
|
||||||
|
author: {
|
||||||
|
bot: true,
|
||||||
|
},
|
||||||
|
content: "!test first",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const events = new Events();
|
||||||
|
|
||||||
|
const result = events.onMessage(message);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Message was sent by a bot, ignoring.");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given Message Content Was Not A Command, Expect Failed Result', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
},
|
||||||
|
content: "This is a standard message",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const events = new Events();
|
||||||
|
|
||||||
|
const result = events.onMessage(message);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Message was not a command, ignoring.");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given Message Had No Command Name, Expect Failed Result', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
},
|
||||||
|
content: "!",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const events = new Events();
|
||||||
|
|
||||||
|
const result = events.onMessage(message);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Command name was not found");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given Command Failed To Execute, Expect Failed Result', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: false, message: "Command failed" });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
},
|
||||||
|
content: "!test first",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const events = new Events();
|
||||||
|
|
||||||
|
const result = events.onMessage(message);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Command failed");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('OnMessage_GivenGuildIsNull_ExpectFailedResult', () => {
|
describe('OnReady', () => {
|
||||||
process.env = {
|
test('Expect Console Log', () => {
|
||||||
BOT_TOKEN: 'TOKEN',
|
console.log = jest.fn();
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
const events = new Events();
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
events.onReady();
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
expect(console.log).toBeCalledWith("Ready");
|
||||||
|
});
|
||||||
const message = {
|
|
||||||
guild: null,
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "!test first",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = events.onMessage(message);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Message was not sent in a guild, ignoring.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('OnMessage_GivenAuthorIsBot_ExpectFailedResult', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: true,
|
|
||||||
},
|
|
||||||
content: "!test first",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = events.onMessage(message);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Message was sent by a bot, ignoring.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('OnMessage_GivenMessageContentsWasNotACommand_ExpectFailedResult', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "This is a standard message",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = events.onMessage(message);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Message was not a command, ignoring.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('OnMessage_GivenMessageHadNoCommandName_ExpectFailedResult', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "!",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = events.onMessage(message);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Command name was not found");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('OnMessage_GivenCommandFailedToExecute_ExpectFailedResult', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: false, message: "Command failed" });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "!test first",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = events.onMessage(message);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Command failed");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('OnReady_ExpectConsoleLog', () => {
|
|
||||||
console.log = jest.fn();
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
events.onReady();
|
|
||||||
|
|
||||||
expect(console.log).toBeCalledWith("Ready");
|
|
||||||
});
|
});
|
|
@ -9,100 +9,18 @@ beforeEach(() => {
|
||||||
fs.existsSync = jest.fn();
|
fs.existsSync = jest.fn();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('LoadCommand_GivenSuccessfulExection_ExpectSuccessfulResult', () => {
|
describe('LoadCommand', () => {
|
||||||
process.env = {
|
test('Given Successful Exection, Expect Successful Result', () => {
|
||||||
BOT_TOKEN: 'TOKEN',
|
process.env = {
|
||||||
BOT_PREFIX: '!',
|
BOT_TOKEN: 'TOKEN',
|
||||||
FOLDERS_COMMANDS: 'commands',
|
BOT_PREFIX: '!',
|
||||||
FOLDERS_EVENTS: 'events',
|
FOLDERS_COMMANDS: 'commands',
|
||||||
}
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
||||||
|
fs.existsSync = jest.fn().mockReturnValue(true);
|
||||||
|
|
||||||
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).toBeTruthy();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('LoadCommand_GivenMemberIsNull_ExpectFailedResult', () => {
|
|
||||||
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: null
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
const result = util.loadCommand("normal", [ "first" ], message);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Member is not part of message");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('LoadCommand_GivenFolderDoesNotExist_ExpectFailedResult', () => {
|
|
||||||
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('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => {
|
|
||||||
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 = {
|
const message = {
|
||||||
member: {
|
member: {
|
||||||
roles: {
|
roles: {
|
||||||
|
@ -113,175 +31,261 @@ test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => {
|
||||||
},
|
},
|
||||||
reply: jest.fn(),
|
reply: jest.fn(),
|
||||||
} as unknown as Message;
|
} 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");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('LoadCommand_GivenUserDoesHaveRole_ExpectSuccessfulResult', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
const util = new Util();
|
||||||
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("roles", [ "first" ], message);
|
|
||||||
|
|
||||||
expect(result.valid).toBeTruthy();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
const result = util.loadCommand("normal", [ "first" ], message);
|
||||||
fs.existsSync = jest.fn().mockReturnValue(true);
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(false),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
const result = util.loadCommand("roles", [ "first" ], message);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("You require the `Moderator` role to run this command");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('LoadCommand_GivenCommandCategoryIsNull_ExpectSuccessfulResultStill', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
expect(result.valid).toBeTruthy();
|
||||||
fs.existsSync = jest.fn().mockReturnValue(true);
|
});
|
||||||
|
|
||||||
const message = {
|
test('Given Member Is Null, Expect Failed Result', () => {
|
||||||
member: {
|
process.env = {
|
||||||
roles: {
|
BOT_TOKEN: 'TOKEN',
|
||||||
cache: {
|
BOT_PREFIX: '!',
|
||||||
find: jest.fn().mockReturnValue(true),
|
FOLDERS_COMMANDS: 'commands',
|
||||||
}
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
||||||
|
fs.existsSync = jest.fn().mockReturnValue(true);
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: null
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
|
||||||
|
const result = util.loadCommand("normal", [ "first" ], message);
|
||||||
|
|
||||||
|
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(),
|
||||||
reply: jest.fn(),
|
} as unknown as Message;
|
||||||
} as unknown as Message;
|
|
||||||
|
const util = new Util();
|
||||||
const util = new Util();
|
|
||||||
|
const result = util.loadCommand("normal", [ "first" ], message);
|
||||||
const result = util.loadCommand("noCategory", [ "first" ], message);
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
expect(result.valid).toBeTruthy();
|
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");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given User Does Have Role, 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("roles", [ "first" ], message);
|
||||||
|
|
||||||
|
expect(result.valid).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given User Does Not Have Role, 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(true);
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(false),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
|
||||||
|
const result = util.loadCommand("roles", [ "first" ], message);
|
||||||
|
|
||||||
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('LoadEvents_GivenEventsAreLoaded_ExpectSuccessfulResult', () => {
|
describe('LoadEvents', () => {
|
||||||
process.env = {
|
test('Given Events Are Loaded, Expect Successful Result', () => {
|
||||||
BOT_TOKEN: 'TOKEN',
|
process.env = {
|
||||||
BOT_PREFIX: '!',
|
BOT_TOKEN: 'TOKEN',
|
||||||
FOLDERS_COMMANDS: 'commands',
|
BOT_PREFIX: '!',
|
||||||
FOLDERS_EVENTS: 'events',
|
FOLDERS_COMMANDS: 'commands',
|
||||||
}
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
|
||||||
fs.existsSync = jest.fn().mockReturnValue(true);
|
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
||||||
fs.readdirSync = jest.fn().mockReturnValue(["normal.ts"]);
|
fs.existsSync = jest.fn().mockReturnValue(true);
|
||||||
|
fs.readdirSync = jest.fn().mockReturnValue(["normal.ts"]);
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
const client = {
|
||||||
} as unknown as Client;
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
const util = new Util();
|
|
||||||
|
const util = new Util();
|
||||||
const result = util.loadEvents(client);
|
|
||||||
|
const result = util.loadEvents(client);
|
||||||
const clientOn = jest.spyOn(client, 'on');
|
|
||||||
|
const clientOn = jest.spyOn(client, 'on');
|
||||||
expect(result.valid).toBeTruthy();
|
|
||||||
expect(clientOn).toBeCalledTimes(13);
|
expect(result.valid).toBeTruthy();
|
||||||
});
|
expect(clientOn).toBeCalledTimes(13);
|
||||||
|
});
|
||||||
test('LoadEvents_GivenNoEventsFound_ExpectSuccessfulResultStill', () => {
|
|
||||||
process.env = {
|
test('Given No Events Found, Expect Successful Result', () => {
|
||||||
BOT_TOKEN: 'TOKEN',
|
process.env = {
|
||||||
BOT_PREFIX: '!',
|
BOT_TOKEN: 'TOKEN',
|
||||||
FOLDERS_COMMANDS: 'commands',
|
BOT_PREFIX: '!',
|
||||||
FOLDERS_EVENTS: 'events',
|
FOLDERS_COMMANDS: 'commands',
|
||||||
}
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
|
||||||
fs.existsSync = jest.fn().mockReturnValue(true);
|
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
||||||
fs.readdirSync = jest.fn().mockReturnValue(["normal"]);
|
fs.existsSync = jest.fn().mockReturnValue(true);
|
||||||
|
fs.readdirSync = jest.fn().mockReturnValue(["normal"]);
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
const client = {
|
||||||
} as unknown as Client;
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
const util = new Util();
|
|
||||||
|
const util = new Util();
|
||||||
const result = util.loadEvents(client);
|
|
||||||
|
const result = util.loadEvents(client);
|
||||||
const clientOn = jest.spyOn(client, 'on');
|
|
||||||
|
const clientOn = jest.spyOn(client, 'on');
|
||||||
expect(result.valid).toBeTruthy();
|
|
||||||
expect(clientOn).toBeCalledTimes(0);
|
expect(result.valid).toBeTruthy();
|
||||||
});
|
expect(clientOn).toBeCalledTimes(0);
|
||||||
|
});
|
||||||
test('LoadEvents_GivenEventFolderDoesNotExist_FailedResult', () => {
|
|
||||||
process.env = {
|
test('Given Event Folder Does Not Exist, Expect Failed Result', () => {
|
||||||
BOT_TOKEN: 'TOKEN',
|
process.env = {
|
||||||
BOT_PREFIX: '!',
|
BOT_TOKEN: 'TOKEN',
|
||||||
FOLDERS_COMMANDS: 'commands',
|
BOT_PREFIX: '!',
|
||||||
FOLDERS_EVENTS: 'events',
|
FOLDERS_COMMANDS: 'commands',
|
||||||
}
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
|
||||||
fs.existsSync = jest.fn().mockReturnValue(false);
|
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
||||||
fs.readdirSync = jest.fn().mockReturnValue(["normal.ts"]);
|
fs.existsSync = jest.fn().mockReturnValue(false);
|
||||||
|
fs.readdirSync = jest.fn().mockReturnValue(["normal.ts"]);
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
const client = {
|
||||||
} as unknown as Client;
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
const util = new Util();
|
|
||||||
|
const util = new Util();
|
||||||
const result = util.loadEvents(client);
|
|
||||||
|
const result = util.loadEvents(client);
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Event folder does not exist");
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Event folder does not exist");
|
||||||
|
});
|
||||||
});
|
});
|
Reference in a new issue