WIP: Start of warn command tests
All checks were successful
Test / build (push) Successful in 16s

This commit is contained in:
Ethan Lane 2024-06-28 18:21:28 +01:00
parent 739bd0713d
commit 42ec4a99db
3 changed files with 157 additions and 8 deletions

View file

@ -0,0 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Execute EXPECT user to be warned 1`] = `
[
{
"color": 3166394,
"description": "<@userId> \`userTag\`",
"fields": [
{
"name": "Moderator",
"value": "<@moderatorId>",
},
{
"name": "Reason",
"value": "Test reason",
},
],
"thumbnail": {
"url": "https://google.com/avatar.png",
},
"title": "Member Warned",
},
]
`;
exports[`Execute EXPECT user to be warned 2`] = `
{
"AuditId": Any<String>,
"AuditType": 1,
"Id": Any<String>,
"ModeratorId": "moderatorId",
"Reason": "Test reason",
"ServerId": "guildId",
"UserId": "userId",
"WhenCreated": Any<Date>,
"WhenUpdated": Any<Date>,
}
`;

View file

@ -44,8 +44,8 @@ describe('execute', () => {
const timeoutFunc = jest.fn(); const timeoutFunc = jest.fn();
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined; let dmChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined; let logsChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
const dmChannel = { const dmChannel = {
send: jest.fn().mockImplementation((options: MessageCreateOptions) => { send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
@ -697,8 +697,8 @@ describe('execute', () => {
const timeoutFunc = jest.fn(); const timeoutFunc = jest.fn();
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined; let dmChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined; let logsChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
const dmChannel = { const dmChannel = {
send: jest.fn().mockImplementation((options: MessageCreateOptions) => { send: jest.fn().mockImplementation((options: MessageCreateOptions) => {

View file

@ -1,13 +1,124 @@
import {APIEmbed, APIEmbedField, CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandUserOption, TextChannel} from "discord.js";
import Warn from "../../src/commands/warn";
import SettingsHelper from "../../src/helpers/SettingsHelper";
import EmbedColours from "../../src/constants/EmbedColours";
import Audit from "../../src/database/entities/Audit";
import {AuditType} from "../../src/constants/AuditType";
beforeEach(() => { beforeEach(() => {
process.env = {}; process.env = {};
}); });
describe('Constructor', () => { describe('Constructor', () => {
test.todo('EXPECT values to be set'); test('EXPECT values to be set', () => {
const command = new Warn();
expect(command.CommandBuilder).toBeDefined();
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
expect(commandBuilder.name).toBe("warn");
expect(commandBuilder.description).toBe("Warns a member in the server with an optional reason")
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
expect(commandBuilder.options.length).toBe(2);
const commandBuilderUserOption = commandBuilder.options[0] as SlashCommandUserOption;
expect(commandBuilderUserOption.name).toBe("target");
expect(commandBuilderUserOption.description).toBe("The user");
expect(commandBuilderUserOption.required).toBe(true);
const commandBuilderReasonOption = commandBuilder.options[1] as SlashCommandStringOption;
expect(commandBuilderReasonOption.name).toBe("reason");
expect(commandBuilderReasonOption.description).toBe("The reason");
});
}); });
describe('Execute', () => { describe('Execute', () => {
test.todo("EXPECT user to be warned"); test("EXPECT user to be warned", async () => {
let sentEmbeds: EmbedBuilder[] | undefined;
let savedAudit: Audit | undefined;
// Arrange
const targetUser = {
user: {
id: "userId",
tag: "userTag",
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
},
member: {},
};
const reason = {
value: "Test reason",
};
const logChannel = {
send: jest.fn().mockImplementation((opts: any) => {
sentEmbeds = opts.embeds;
}),
} as unknown as TextChannel;
const interaction = {
reply: jest.fn(),
options: {
get: jest.fn()
.mockReturnValueOnce(targetUser)
.mockReturnValue(reason),
},
guild: {
channels: {
cache: {
find: jest.fn().mockReturnValue(logChannel),
},
},
},
guildId: "guildId",
user: {
id: "moderatorId",
},
} as unknown as CommandInteraction;
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
savedAudit = audit;
});
// Act
const command = new Warn();
await command.execute(interaction);
// Assert
expect(interaction.reply).toHaveBeenCalledTimes(1);
expect(interaction.reply).toHaveBeenCalledWith("Successfully warned user.");
expect(interaction.options.get).toHaveBeenCalledTimes(2);
expect(interaction.options.get).toHaveBeenCalledWith("target");
expect(interaction.options.get).toHaveBeenCalledWith("reason");
expect(interaction.guild!.channels.cache.find).toHaveBeenCalledTimes(1);
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("channels.logs.mod", "guildId");
expect(targetUser.user.avatarURL).toHaveBeenCalledTimes(1);
expect(logChannel.send).toHaveBeenCalledTimes(1);
expect(sentEmbeds).toBeDefined();
expect(sentEmbeds).toMatchSnapshot();
expect(Audit.prototype.Save).toHaveBeenCalledWith(Audit, expect.any(Audit));
expect(savedAudit).toMatchSnapshot({
Id: expect.any(String),
AuditId: expect.any(String),
WhenCreated: expect.any(Date),
WhenUpdated: expect.any(Date)
});
});
test.todo("GIVEN interaction.guild is null, EXPECT nothing to happen"); test.todo("GIVEN interaction.guild is null, EXPECT nothing to happen");