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

View file

@ -20,53 +20,22 @@ test('LoadCommand_GivenSuccessfulExection_ExpectSuccessfulResult', () => {
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const util = new Util();
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: {
user: user,
nick: "NOT API SUPPORT",
roles: [],
joined_at: "2015-04-26T06:26:56.936000+00:00",
deaf: false,
mute: false
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
type: 0,
author: user,
},
new TextChannel(guild, {
client: client,
guild: guild,
id: "channel-id",
}),
);
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
const result = util.loadCommand("name", [ "first" ], message);
expect(result.valid).toBeTruthy();
client.destroy();
});
test('LoadCommand_GivenMemberIsNull_ExpectFailedResult', () => {
@ -80,38 +49,16 @@ test('LoadCommand_GivenMemberIsNull_ExpectFailedResult', () => {
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const util = new Util();
const message = {
member: null
} as unknown as 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(),
});
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 util = new Util();
const result = util.loadCommand("name", [ "first" ], message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Member is not part of message");
client.destroy();
});
test('LoadCommand_GivenFolderDoesNotExist_ExpectFailedResult', () => {
@ -125,54 +72,23 @@ test('LoadCommand_GivenFolderDoesNotExist_ExpectFailedResult', () => {
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(false);
const util = new Util();
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: {
user: user,
nick: "NOT API SUPPORT",
roles: [],
joined_at: "2015-04-26T06:26:56.936000+00:00",
deaf: false,
mute: false
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
type: 0,
author: user,
},
new TextChannel(guild, {
client: client,
guild: guild,
id: "channel-id",
}),
);
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
const result = util.loadCommand("name", [ "first" ], message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command folder does not exist");
client.destroy();
});
test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => {
@ -187,82 +103,23 @@ test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => {
fs.existsSync = jest.fn().mockReturnValueOnce(true)
.mockReturnValue(false);
const util = new Util();
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",
const message = {
member: {
user: user,
nick: "NOT API SUPPORT",
roles: [],
joined_at: "2015-04-26T06:26:56.936000+00:00",
deaf: false,
mute: false
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
type: 0,
author: user,
},
new TextChannel(guild, {
client: client,
guild: guild,
id: "channel-id",
}),
);
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
const result = util.loadCommand("name", [ "first" ], message);
expect(result.valid).toBeFalsy();
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', () => {
@ -276,7 +133,7 @@ test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
let message = {
const message = {
member: {
roles: {
cache: {
@ -293,4 +150,54 @@ test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
expect(result.valid).toBeFalsy();
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");
});