Poll command tests
This commit is contained in:
parent
ddaed31885
commit
a83de88c9c
2 changed files with 274 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
|||
import { ICommandContext } from "../contracts/ICommandContext";
|
||||
import ICommandReturnContext from "../contracts/ICommandReturnContext";
|
||||
import ErrorEmbed from "../helpers/embeds/ErrorEmbed";
|
||||
import PublicEmbed from "../helpers/embeds/PublicEmbed";
|
||||
import { Command } from "../type/command";
|
||||
|
@ -10,14 +11,18 @@ export default class Poll extends Command {
|
|||
super._category = "General";
|
||||
}
|
||||
|
||||
public override async execute(context: ICommandContext) {
|
||||
public override async execute(context: ICommandContext): Promise<ICommandReturnContext> {
|
||||
const argsJoined = context.args.join(" ");
|
||||
const argsSplit = argsJoined.split(";");
|
||||
|
||||
if (argsSplit.length < 3 || argsSplit.length > 10) {
|
||||
const errorEmbed = new ErrorEmbed(context, "Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options");
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
return;
|
||||
|
||||
return {
|
||||
commandContext: context,
|
||||
embeds: [errorEmbed]
|
||||
};
|
||||
}
|
||||
|
||||
const title = argsSplit[0];
|
||||
|
@ -53,5 +58,10 @@ export default class Poll extends Command {
|
|||
if (context.message.deletable) {
|
||||
await context.message.delete({ reason: "Poll command" });
|
||||
}
|
||||
|
||||
return {
|
||||
commandContext: context,
|
||||
embeds: [embed]
|
||||
};
|
||||
}
|
||||
}
|
262
tests/commands/poll.test.ts
Normal file
262
tests/commands/poll.test.ts
Normal file
|
@ -0,0 +1,262 @@
|
|||
import { Message, MessageEmbed } from "discord.js";
|
||||
import Poll from "../../src/commands/poll";
|
||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
||||
|
||||
describe('Constructor', () => {
|
||||
test('Expect properties to be set', () => {
|
||||
const poll = new Poll();
|
||||
|
||||
expect(poll._category).toBe('General');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Execute', () => {
|
||||
test('Given input is valid, expect poll to be generated', async () => {
|
||||
const returnMessageReact = jest.fn();
|
||||
|
||||
const returnMessage = {
|
||||
react: returnMessageReact
|
||||
} as unknown as Message;
|
||||
|
||||
const messageChannelSend = jest.fn()
|
||||
.mockReturnValue(returnMessage);
|
||||
const messageDelete = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
delete: messageDelete,
|
||||
deletable: true
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'poll',
|
||||
args: ['Test', 'title;', 'one;', 'two'],
|
||||
message: message
|
||||
};
|
||||
|
||||
const poll = new Poll();
|
||||
|
||||
const result = await poll.execute(context);
|
||||
|
||||
expect(messageChannelSend).toBeCalledTimes(1);
|
||||
expect(messageDelete).toBeCalledTimes(1);
|
||||
expect(returnMessageReact).toBeCalledTimes(2);
|
||||
|
||||
expect(result.embeds.length).toBe(1);
|
||||
|
||||
// Embed
|
||||
const embed = result.embeds[0];
|
||||
|
||||
expect(embed.title).toBe('Test title');
|
||||
expect(embed.description).toBe(':one: one\n:two: two');
|
||||
});
|
||||
|
||||
test('Given message is not deletable by client, expect it not to attempt deletion', async () => {
|
||||
const returnMessageReact = jest.fn();
|
||||
|
||||
const returnMessage = {
|
||||
react: returnMessageReact
|
||||
} as unknown as Message;
|
||||
|
||||
const messageChannelSend = jest.fn()
|
||||
.mockReturnValue(returnMessage);
|
||||
const messageDelete = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
delete: messageDelete,
|
||||
deletable: false
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'poll',
|
||||
args: ['Test', 'title;', 'one;', 'two'],
|
||||
message: message
|
||||
};
|
||||
|
||||
const poll = new Poll();
|
||||
|
||||
const result = await poll.execute(context);
|
||||
|
||||
expect(messageChannelSend).toBeCalledTimes(1);
|
||||
expect(messageDelete).not.toBeCalled();
|
||||
expect(returnMessageReact).toBeCalledTimes(2);
|
||||
|
||||
expect(result.embeds.length).toBe(1);
|
||||
|
||||
// Embed
|
||||
const embed = result.embeds[0];
|
||||
|
||||
expect(embed.title).toBe('Test title');
|
||||
expect(embed.description).toBe(':one: one\n:two: two');
|
||||
});
|
||||
|
||||
test('Given no arguments, expect error embed', async () => {
|
||||
const returnMessageReact = jest.fn();
|
||||
|
||||
const returnMessage = {
|
||||
react: returnMessageReact
|
||||
} as unknown as Message;
|
||||
|
||||
const messageChannelSend = jest.fn()
|
||||
.mockReturnValue(returnMessage);
|
||||
const messageDelete = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
delete: messageDelete,
|
||||
deletable: true
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'poll',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const poll = new Poll();
|
||||
|
||||
const result = await poll.execute(context);
|
||||
|
||||
expect(messageChannelSend).toBeCalledTimes(1);
|
||||
expect(messageDelete).not.toBeCalled();
|
||||
expect(returnMessageReact).not.toBeCalled();
|
||||
|
||||
expect(result.embeds.length).toBe(1);
|
||||
|
||||
// Error Embed
|
||||
const errorEmbed = result.embeds[0];
|
||||
|
||||
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
|
||||
});
|
||||
|
||||
test('Given only 1 option, expect error embed', async () => {
|
||||
const returnMessageReact = jest.fn();
|
||||
|
||||
const returnMessage = {
|
||||
react: returnMessageReact
|
||||
} as unknown as Message;
|
||||
|
||||
const messageChannelSend = jest.fn()
|
||||
.mockReturnValue(returnMessage);
|
||||
const messageDelete = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
delete: messageDelete,
|
||||
deletable: true
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'poll',
|
||||
args: ['Test', 'title;', 'one'],
|
||||
message: message
|
||||
};
|
||||
|
||||
const poll = new Poll();
|
||||
|
||||
const result = await poll.execute(context);
|
||||
|
||||
expect(messageChannelSend).toBeCalledTimes(1);
|
||||
expect(messageDelete).not.toBeCalled();
|
||||
expect(returnMessageReact).not.toBeCalled();
|
||||
|
||||
expect(result.embeds.length).toBe(1);
|
||||
|
||||
// Error Embed
|
||||
const errorEmbed = result.embeds[0];
|
||||
|
||||
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
|
||||
});
|
||||
|
||||
test('Given 9 options, expect poll to be generated', async () => {
|
||||
const returnMessageReact = jest.fn();
|
||||
|
||||
const returnMessage = {
|
||||
react: returnMessageReact
|
||||
} as unknown as Message;
|
||||
|
||||
const messageChannelSend = jest.fn()
|
||||
.mockReturnValue(returnMessage);
|
||||
const messageDelete = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
delete: messageDelete,
|
||||
deletable: true
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'poll',
|
||||
args: ['Test', 'title;', 'one;', 'two;', 'three;', 'four;', 'five;', 'six;', 'seven;', 'eight;', 'nine'],
|
||||
message: message
|
||||
};
|
||||
|
||||
const poll = new Poll();
|
||||
|
||||
const result = await poll.execute(context);
|
||||
|
||||
expect(messageChannelSend).toBeCalledTimes(1);
|
||||
expect(messageDelete).toBeCalledTimes(1);
|
||||
expect(returnMessageReact).toBeCalledTimes(9);
|
||||
|
||||
expect(result.embeds.length).toBe(1);
|
||||
|
||||
// Embed
|
||||
const embed = result.embeds[0];
|
||||
|
||||
expect(embed.title).toBe('Test title');
|
||||
expect(embed.description).toBe(':one: one\n:two: two\n:three: three\n:four: four\n:five: five\n:six: six\n:seven: seven\n:eight: eight\n:nine: nine');
|
||||
});
|
||||
|
||||
test('Given 10 options, expect error embed', async () => {
|
||||
const returnMessageReact = jest.fn();
|
||||
|
||||
const returnMessage = {
|
||||
react: returnMessageReact
|
||||
} as unknown as Message;
|
||||
|
||||
const messageChannelSend = jest.fn()
|
||||
.mockReturnValue(returnMessage);
|
||||
const messageDelete = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
delete: messageDelete,
|
||||
deletable: true
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'poll',
|
||||
args: ['Test', 'title;', 'one;', 'two;', 'three;', 'four;', 'five;', 'six;', 'seven;', 'eight;', 'nine;', 'ten'],
|
||||
message: message
|
||||
};
|
||||
|
||||
const poll = new Poll();
|
||||
|
||||
const result = await poll.execute(context);
|
||||
|
||||
expect(messageChannelSend).toBeCalledTimes(1);
|
||||
expect(messageDelete).not.toBeCalled();
|
||||
expect(returnMessageReact).not.toBeCalled();
|
||||
|
||||
expect(result.embeds.length).toBe(1);
|
||||
|
||||
// Error Embed
|
||||
const errorEmbed = result.embeds[0];
|
||||
|
||||
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue