Util tests, rewrite tests

This commit is contained in:
Ethan Lane 2021-08-20 15:42:46 +01:00
parent 2d2eaadea7
commit 338514ba1e
2 changed files with 128 additions and 329 deletions

View file

@ -15,31 +15,17 @@ test('OnMessage_GivenMessageIsValid_ExpectMessageSent', () => {
BOT_PREFIX: '!', BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands', FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events', FOLDERS_EVENTS: 'events',
} };
Util.prototype.loadCommand = jest.fn().mockImplementation((name: string, args: string[], message: Message) => { Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
return {
valid: true
}
});
let discordClient = new Client(); const message = {
let guild = new Guild(discordClient, { guild: {},
id: SnowflakeUtil.generate(), author: {
}); bot: false,
let message = new Message( },
discordClient,
{
content: "!test first", content: "!test first",
author: { username: "test-user", discriminator: 1234 }, } as unknown as Message;
id: "test",
},
new TextChannel(guild, {
client: discordClient,
guild: guild,
id: "channel-id",
})
);
const events = new Events(); const events = new Events();
@ -52,8 +38,6 @@ test('OnMessage_GivenMessageIsValid_ExpectMessageSent', () => {
expect(result.context?.args.length).toBe(1); expect(result.context?.args.length).toBe(1);
expect(result.context?.args[0]).toBe('first'); expect(result.context?.args[0]).toBe('first');
expect(result.context?.message).toBe(message); expect(result.context?.message).toBe(message);
discordClient.destroy();
}); });
test('OnMessage_GivenGuildIsNull_ExpectFailedResult', () => { test('OnMessage_GivenGuildIsNull_ExpectFailedResult', () => {
@ -64,33 +48,15 @@ test('OnMessage_GivenGuildIsNull_ExpectFailedResult', () => {
FOLDERS_EVENTS: 'events', FOLDERS_EVENTS: 'events',
} }
Util.prototype.loadCommand = jest.fn().mockImplementation((name: string, args: string[], message: Message) => { Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
return {
valid: true
}
});
let client = new Client(); const message = {
let guild = new Guild(client, { guild: null,
id: SnowflakeUtil.generate(), author: {
}); bot: false,
let message = new Message(
client,
{
content: "!test first",
author: {
username: "test-user",
discriminator: 1234
},
id: "test",
}, },
new DMChannel( content: "!test first",
client, } as unknown as Message;
{
id: "channel-id",
}
)
);
const events = new Events(); const events = new Events();
@ -98,8 +64,6 @@ test('OnMessage_GivenGuildIsNull_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was not sent in a guild, ignoring."); expect(result.message).toBe("Message was not sent in a guild, ignoring.");
client.destroy();
}); });
test('OnMessage_GivenAuthorIsBot_ExpectFailedResult', () => { test('OnMessage_GivenAuthorIsBot_ExpectFailedResult', () => {
@ -110,33 +74,15 @@ test('OnMessage_GivenAuthorIsBot_ExpectFailedResult', () => {
FOLDERS_EVENTS: 'events', FOLDERS_EVENTS: 'events',
} }
Util.prototype.loadCommand = jest.fn().mockImplementation((name: string, args: string[], message: Message) => { Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
return {
valid: true
}
});
let client = new Client(); const message = {
let guild = new Guild(client, { guild: {},
id: SnowflakeUtil.generate(), author: {
}); bot: true,
let message = new Message(
client,
{
content: "!test first",
author: {
username: "test-user",
discriminator: 1234,
bot: true,
},
id: "test",
}, },
new TextChannel(guild, { content: "!test first",
client: client, } as unknown as Message;
guild: guild,
id: "channel-id",
})
);
const events = new Events(); const events = new Events();
@ -144,8 +90,6 @@ test('OnMessage_GivenAuthorIsBot_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was sent by a bot, ignoring."); expect(result.message).toBe("Message was sent by a bot, ignoring.");
client.destroy();
}); });
test('OnMessage_GivenMessageContentsWasNotACommand_ExpectFailedResult', () => { test('OnMessage_GivenMessageContentsWasNotACommand_ExpectFailedResult', () => {
@ -156,32 +100,15 @@ test('OnMessage_GivenMessageContentsWasNotACommand_ExpectFailedResult', () => {
FOLDERS_EVENTS: 'events', FOLDERS_EVENTS: 'events',
} }
Util.prototype.loadCommand = jest.fn().mockImplementation((name: string, args: string[], message: Message) => { Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
return {
valid: true
}
});
let client = new Client(); const message = {
let guild = new Guild(client, { guild: {},
id: SnowflakeUtil.generate(), author: {
}); bot: false,
let message = new Message(
client,
{
content: "This is a standard message without a prefix",
author: {
username: "test-user",
discriminator: 1234,
},
id: "test",
}, },
new TextChannel(guild, { content: "This is a standard message",
client: client, } as unknown as Message;
guild: guild,
id: "channel-id",
})
);
const events = new Events(); const events = new Events();
@ -189,8 +116,6 @@ test('OnMessage_GivenMessageContentsWasNotACommand_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Message was not a command, ignoring."); expect(result.message).toBe("Message was not a command, ignoring.");
client.destroy();
}); });
test('OnMessage_GivenMessageHadNoCommandName_ExpectFailedResult', () => { test('OnMessage_GivenMessageHadNoCommandName_ExpectFailedResult', () => {
@ -201,29 +126,15 @@ test('OnMessage_GivenMessageHadNoCommandName_ExpectFailedResult', () => {
FOLDERS_EVENTS: 'events', FOLDERS_EVENTS: 'events',
} }
Util.prototype.loadCommand = jest.fn().mockImplementation((name: string, args: string[], message: Message) => { Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
return {
valid: true
}
});
let discordClient = new Client(); const message = {
let guild = new Guild(discordClient, { guild: {},
id: SnowflakeUtil.generate(), author: {
}); bot: false,
let message = new Message( },
discordClient,
{
content: "!", content: "!",
author: { username: "test-user", discriminator: 1234 }, } as unknown as Message;
id: "test",
},
new TextChannel(guild, {
client: discordClient,
guild: guild,
id: "channel-id",
})
);
const events = new Events(); const events = new Events();
@ -231,8 +142,6 @@ test('OnMessage_GivenMessageHadNoCommandName_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command name was not found"); expect(result.message).toBe("Command name was not found");
discordClient.destroy();
}); });
test('OnMessage_GivenCommandFailedToExecute_ExpectFailedResult', () => { test('OnMessage_GivenCommandFailedToExecute_ExpectFailedResult', () => {
@ -243,30 +152,15 @@ test('OnMessage_GivenCommandFailedToExecute_ExpectFailedResult', () => {
FOLDERS_EVENTS: 'events', FOLDERS_EVENTS: 'events',
} }
Util.prototype.loadCommand = jest.fn().mockImplementation((name: string, args: string[], message: Message) => { Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: false, message: "Command failed" });
return {
valid: false,
message: "Command failed",
}
});
let discordClient = new Client(); const message = {
let guild = new Guild(discordClient, { guild: {},
id: SnowflakeUtil.generate(), author: {
}); bot: false,
let message = new Message( },
discordClient,
{
content: "!test first", content: "!test first",
author: { username: "test-user", discriminator: 1234 }, } as unknown as Message;
id: "test",
},
new TextChannel(guild, {
client: discordClient,
guild: guild,
id: "channel-id",
})
);
const events = new Events(); const events = new Events();
@ -274,8 +168,6 @@ test('OnMessage_GivenCommandFailedToExecute_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command failed"); expect(result.message).toBe("Command failed");
discordClient.destroy();
}); });
test('OnReady_ExpectConsoleLog', () => { test('OnReady_ExpectConsoleLog', () => {

View file

@ -20,53 +20,22 @@ test('LoadCommand_GivenSuccessfulExection_ExpectSuccessfulResult', () => {
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks"); process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true); fs.existsSync = jest.fn().mockReturnValue(true);
const util = new Util(); const message = {
member: {
let client = new Client(); roles: {
let user = new User(client, { cache: {
id: SnowflakeUtil.generate(), find: jest.fn().mockReturnValue(true),
username: "test-user", }
discriminator: 1234,
});
let guild = new Guild(client, {
id: SnowflakeUtil.generate(),
members: [{
user: user,
nick: "Test User",
roles: [],
joined_at: "2015-04-26T06:26:56.936000+00:00",
deaf: false,
mute: false
}]
});
let message = new Message(
client,
{
content: "!test first",
id: "test",
member: {
user: user,
nick: "NOT API SUPPORT",
roles: [],
joined_at: "2015-04-26T06:26:56.936000+00:00",
deaf: false,
mute: false
}, },
type: 0,
author: user,
}, },
new TextChannel(guild, { reply: jest.fn(),
client: client, } as unknown as Message;
guild: guild,
id: "channel-id", const util = new Util();
}),
);
const result = util.loadCommand("name", [ "first" ], message); const result = util.loadCommand("name", [ "first" ], message);
expect(result.valid).toBeTruthy(); expect(result.valid).toBeTruthy();
client.destroy();
}); });
test('LoadCommand_GivenMemberIsNull_ExpectFailedResult', () => { test('LoadCommand_GivenMemberIsNull_ExpectFailedResult', () => {
@ -80,38 +49,16 @@ test('LoadCommand_GivenMemberIsNull_ExpectFailedResult', () => {
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks"); process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true); fs.existsSync = jest.fn().mockReturnValue(true);
const util = new Util(); const message = {
member: null
} as unknown as Message;
let client = new Client(); const util = new Util();
let user = new User(client, {
id: SnowflakeUtil.generate(),
username: "test-user",
discriminator: 1234,
});
let guild = new Guild(client, {
id: SnowflakeUtil.generate(),
});
let message = new Message(
client,
{
content: "!test first",
id: "test",
type: 0,
author: user,
},
new TextChannel(guild, {
client: client,
guild: guild,
id: "channel-id",
}),
);
const result = util.loadCommand("name", [ "first" ], message); const result = util.loadCommand("name", [ "first" ], message);
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Member is not part of message"); expect(result.message).toBe("Member is not part of message");
client.destroy();
}); });
test('LoadCommand_GivenFolderDoesNotExist_ExpectFailedResult', () => { test('LoadCommand_GivenFolderDoesNotExist_ExpectFailedResult', () => {
@ -125,54 +72,23 @@ test('LoadCommand_GivenFolderDoesNotExist_ExpectFailedResult', () => {
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks"); process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(false); fs.existsSync = jest.fn().mockReturnValue(false);
const util = new Util(); const message = {
member: {
let client = new Client(); roles: {
let user = new User(client, { cache: {
id: SnowflakeUtil.generate(), find: jest.fn().mockReturnValue(true),
username: "test-user", }
discriminator: 1234,
});
let guild = new Guild(client, {
id: SnowflakeUtil.generate(),
members: [{
user: user,
nick: "Test User",
roles: [],
joined_at: "2015-04-26T06:26:56.936000+00:00",
deaf: false,
mute: false
}]
});
let message = new Message(
client,
{
content: "!test first",
id: "test",
member: {
user: user,
nick: "NOT API SUPPORT",
roles: [],
joined_at: "2015-04-26T06:26:56.936000+00:00",
deaf: false,
mute: false
}, },
type: 0,
author: user,
}, },
new TextChannel(guild, { reply: jest.fn(),
client: client, } as unknown as Message;
guild: guild,
id: "channel-id", const util = new Util();
}),
);
const result = util.loadCommand("name", [ "first" ], message); const result = util.loadCommand("name", [ "first" ], message);
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command folder does not exist"); expect(result.message).toBe("Command folder does not exist");
client.destroy();
}); });
test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => { test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => {
@ -187,82 +103,23 @@ test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => {
fs.existsSync = jest.fn().mockReturnValueOnce(true) fs.existsSync = jest.fn().mockReturnValueOnce(true)
.mockReturnValue(false); .mockReturnValue(false);
const util = new Util(); const message = {
let client = new Client();
let user = new User(client, {
id: SnowflakeUtil.generate(),
username: "test-user",
discriminator: 1234,
});
let guild = new Guild(client, {
id: SnowflakeUtil.generate(),
members: [{
user: user,
nick: "Test User",
roles: [],
joined_at: "2015-04-26T06:26:56.936000+00:00",
deaf: false,
mute: false
}]
});
let message = new Message(
client,
{
content: "!test first",
id: "test",
member: { member: {
user: user, roles: {
nick: "NOT API SUPPORT", cache: {
roles: [], find: jest.fn().mockReturnValue(true),
joined_at: "2015-04-26T06:26:56.936000+00:00", }
deaf: false, },
mute: false
}, },
type: 0, reply: jest.fn(),
author: user, } as unknown as Message;
},
new TextChannel(guild, { const util = new Util();
client: client,
guild: guild,
id: "channel-id",
}),
);
const result = util.loadCommand("name", [ "first" ], message); const result = util.loadCommand("name", [ "first" ], message);
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("File does not exist"); expect(result.message).toBe("File does not exist");
client.destroy();
});
test('LoadCommand_GivenUserHasRole_ExpectSuccessfulResult', () => {
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);
let message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
}
} as unknown as Message;
const util = new Util();
const result = util.loadCommand("roles", [ "first" ], message);
expect(result.valid).toBeTruthy();
}); });
test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => { test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
@ -276,7 +133,7 @@ test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks"); process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true); fs.existsSync = jest.fn().mockReturnValue(true);
let message = { const message = {
member: { member: {
roles: { roles: {
cache: { cache: {
@ -293,4 +150,54 @@ test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy(); expect(result.valid).toBeFalsy();
expect(result.message).toBe("You require the `Moderator` role to run this command"); expect(result.message).toBe("You require the `Moderator` role to run this command");
});
test('LoadEvents_GivenEventsAreLoaded_ExpectSuccessfulResult', () => {
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);
fs.readdirSync = jest.fn().mockReturnValue(["name.ts"]);
const client = {
on: jest.fn(),
} as unknown as Client;
const util = new Util();
const result = util.loadEvents(client);
const clientOn = jest.spyOn(client, 'on');
expect(result.valid).toBeTruthy();
expect(clientOn).toBeCalledTimes(13);
});
test('LoadEvents_GivenEventFolderDoesNotExist_FailedResult', () => {
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(["name.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");
}); });