Fix tests

This commit is contained in:
Ethan Lane 2021-12-24 15:26:32 +00:00
parent 2cc12d91be
commit 1223e35bde
2 changed files with 168 additions and 164 deletions

View file

@ -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");
@ -27,10 +29,19 @@ 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();
@ -58,10 +69,19 @@ 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.");
@ -84,10 +104,19 @@ 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.");
@ -110,10 +139,19 @@ 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.");
@ -136,10 +174,19 @@ 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");
@ -162,10 +209,19 @@ 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");

View file

@ -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");
@ -31,12 +36,22 @@ 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', () => {
@ -53,74 +68,23 @@ describe('LoadCommand', () => {
const message = {
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', () => {
@ -144,12 +108,22 @@ 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', () => {
@ -173,42 +147,24 @@ 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',
}
@ -236,14 +192,24 @@ describe('LoadCommand', () => {
} as unknown as Message;
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");
@ -270,14 +236,24 @@ describe('LoadCommand', () => {
} as unknown as Message;
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");
@ -302,43 +278,29 @@ 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();
});
});
@ -358,10 +320,18 @@ describe('LoadEvents', () => {
const client = {
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');
@ -384,38 +354,16 @@ describe('LoadEvents', () => {
const client = {
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");
});
});