Compare commits
No commits in common. "6af92ad1c7aaad50395bd1f9cef346be301ebdda" and "c387eb5b02cf55aa4a30c66e2106825fc7efada1" have entirely different histories.
6af92ad1c7
...
c387eb5b02
1 changed files with 7 additions and 330 deletions
|
@ -1,4 +1,4 @@
|
|||
import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, Embed, EmbedBuilder, EmbedField, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, Message, MessageCreateOptions, MessagePayload, SlashCommandBuilder, TextChannel, User } from "discord.js";
|
||||
import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, Embed, EmbedBuilder, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, MessageCreateOptions, SlashCommandBuilder, TextChannel, User } from "discord.js";
|
||||
import { mock } from "jest-mock-extended";
|
||||
import Timeout from "../../src/commands/timeout";
|
||||
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
||||
|
@ -194,19 +194,7 @@ describe('execute', () => {
|
|||
expect(interaction.reply).not.toBeCalled();
|
||||
});
|
||||
|
||||
test('GIVEN interaction.guildId IS NULL, EXPECT nothing to happen', async () => {
|
||||
const command = new Timeout();
|
||||
|
||||
const interaction = {
|
||||
guild: mock<Guild>(),
|
||||
guildId: null,
|
||||
reply: jest.fn(),
|
||||
} as unknown as CommandInteraction;
|
||||
|
||||
await command.execute(interaction);
|
||||
|
||||
expect(interaction.reply).not.toBeCalled();
|
||||
});
|
||||
test.todo('GIVEN interaction.guildId IS NULL, EXPECT nothing to happen');
|
||||
|
||||
// Validation
|
||||
test('GIVEN targetUser IS NULL, EXPECT validation error', async () => {
|
||||
|
@ -404,326 +392,15 @@ describe('execute', () => {
|
|||
});
|
||||
|
||||
// Reason variable
|
||||
test('GIVEN reason IS NULL, EXPECT to be ran with empty string', async () => {
|
||||
const command = new Timeout();
|
||||
test.todo('GIVEN reason IS NOT NULL, EXPECT to be ran with reason set');
|
||||
|
||||
let savedAudit: DeepPartial<Audit> | undefined;
|
||||
|
||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
||||
savedAudit = entity;
|
||||
|
||||
return Promise.resolve();
|
||||
});
|
||||
|
||||
const timeoutFunc = jest.fn();
|
||||
|
||||
const sentEmbeds: EmbedBuilder[] = [];
|
||||
|
||||
const interaction = {
|
||||
reply: jest.fn(),
|
||||
guild: {
|
||||
channels: {
|
||||
cache: {
|
||||
find: jest.fn().mockReturnValue(mock<TextChannel>()),
|
||||
}
|
||||
}
|
||||
},
|
||||
guildId: 'guildId',
|
||||
user: {
|
||||
id: 'moderatorId',
|
||||
},
|
||||
options: {
|
||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
||||
switch (value) {
|
||||
case 'target':
|
||||
return {
|
||||
user: {
|
||||
id: 'userId',
|
||||
tag: 'userTag',
|
||||
createDM: jest.fn().mockReturnValue({
|
||||
send: jest.fn(async (options: MessageCreateOptions): Promise<Message<false>> => {
|
||||
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
||||
|
||||
return mock<Message<false>>();
|
||||
})
|
||||
}) as unknown as DMChannel,
|
||||
} as unknown as User,
|
||||
member: {
|
||||
manageable: true,
|
||||
timeout: timeoutFunc,
|
||||
} as unknown as GuildMember
|
||||
} as CommandInteractionOption;
|
||||
case 'length':
|
||||
return {
|
||||
value: '1m'
|
||||
} as CommandInteractionOption;
|
||||
case 'reason':
|
||||
return {
|
||||
value: undefined,
|
||||
} as CommandInteractionOption;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}),
|
||||
}
|
||||
} as unknown as CommandInteraction;
|
||||
|
||||
|
||||
await command.execute(interaction);
|
||||
|
||||
expect(timeoutFunc).toBeCalledWith(1000 * 60 * 1, "");
|
||||
expect(savedAudit?.Reason).toBe("*none*");
|
||||
|
||||
const dmEmbed = (sentEmbeds[0] as any).data;
|
||||
const dmEmbedReasonField = dmEmbed.fields![0] as EmbedField;
|
||||
|
||||
expect(dmEmbedReasonField.value).toBe("*none*");
|
||||
});
|
||||
test.todo('GIVEN reason IS NULL, EXPECT to be ran with empty string');
|
||||
|
||||
// Log embed
|
||||
test('GIVEN channelName IS NULL, EXPECT execution to return', async () => {
|
||||
const command = new Timeout();
|
||||
test.todo('GIVEN channelName IS NULL, EXPECT execution to return');
|
||||
|
||||
let savedAudit: DeepPartial<Audit> | undefined;
|
||||
|
||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
||||
savedAudit = entity;
|
||||
|
||||
return Promise.resolve();
|
||||
});
|
||||
|
||||
const settingsGet = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue(undefined);
|
||||
|
||||
const timeoutFunc = jest.fn();
|
||||
|
||||
const sentEmbeds: EmbedBuilder[] = [];
|
||||
|
||||
const logChannelSendFunc = jest.fn();
|
||||
|
||||
const interaction = {
|
||||
reply: jest.fn(),
|
||||
guild: {
|
||||
channels: {
|
||||
cache: {
|
||||
find: jest.fn().mockReturnValue({
|
||||
send: logChannelSendFunc,
|
||||
} as unknown as TextChannel),
|
||||
}
|
||||
}
|
||||
},
|
||||
guildId: 'guildId',
|
||||
user: {
|
||||
id: 'moderatorId',
|
||||
},
|
||||
options: {
|
||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
||||
switch (value) {
|
||||
case 'target':
|
||||
return {
|
||||
user: {
|
||||
id: 'userId',
|
||||
tag: 'userTag',
|
||||
createDM: jest.fn().mockReturnValue({
|
||||
send: jest.fn(async (options: MessageCreateOptions): Promise<Message<false>> => {
|
||||
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
||||
|
||||
return mock<Message<false>>();
|
||||
})
|
||||
}) as unknown as DMChannel,
|
||||
} as unknown as User,
|
||||
member: {
|
||||
manageable: true,
|
||||
timeout: timeoutFunc,
|
||||
} as unknown as GuildMember
|
||||
} as CommandInteractionOption;
|
||||
case 'length':
|
||||
return {
|
||||
value: '1m'
|
||||
} as CommandInteractionOption;
|
||||
case 'reason':
|
||||
return {
|
||||
value: 'Test reason',
|
||||
} as CommandInteractionOption;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}),
|
||||
}
|
||||
} as unknown as CommandInteraction;
|
||||
|
||||
|
||||
await command.execute(interaction);
|
||||
|
||||
expect(timeoutFunc).toBeCalled();
|
||||
expect(sentEmbeds.length).toBe(0);
|
||||
expect(logChannelSendFunc).not.toBeCalled();
|
||||
});
|
||||
|
||||
test('GIVEN channel IS NULL, EXPECT embed to not be sent', async () => {
|
||||
const command = new Timeout();
|
||||
|
||||
let savedAudit: DeepPartial<Audit> | undefined;
|
||||
|
||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
||||
savedAudit = entity;
|
||||
|
||||
return Promise.resolve();
|
||||
});
|
||||
|
||||
const settingsGet = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
|
||||
|
||||
const timeoutFunc = jest.fn();
|
||||
|
||||
const sentEmbeds: EmbedBuilder[] = [];
|
||||
|
||||
const interaction = {
|
||||
reply: jest.fn(),
|
||||
guild: {
|
||||
channels: {
|
||||
cache: {
|
||||
find: jest.fn().mockReturnValue(undefined),
|
||||
}
|
||||
}
|
||||
},
|
||||
guildId: 'guildId',
|
||||
user: {
|
||||
id: 'moderatorId',
|
||||
},
|
||||
options: {
|
||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
||||
switch (value) {
|
||||
case 'target':
|
||||
return {
|
||||
user: {
|
||||
id: 'userId',
|
||||
tag: 'userTag',
|
||||
createDM: jest.fn().mockReturnValue({
|
||||
send: jest.fn(async (options: MessageCreateOptions): Promise<Message<false>> => {
|
||||
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
||||
|
||||
return mock<Message<false>>();
|
||||
})
|
||||
}) as unknown as DMChannel,
|
||||
} as unknown as User,
|
||||
member: {
|
||||
manageable: true,
|
||||
timeout: timeoutFunc,
|
||||
} as unknown as GuildMember
|
||||
} as CommandInteractionOption;
|
||||
case 'length':
|
||||
return {
|
||||
value: '1m'
|
||||
} as CommandInteractionOption;
|
||||
case 'reason':
|
||||
return {
|
||||
value: 'Test reason',
|
||||
} as CommandInteractionOption;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}),
|
||||
}
|
||||
} as unknown as CommandInteraction;
|
||||
|
||||
|
||||
await command.execute(interaction);
|
||||
|
||||
expect(timeoutFunc).toBeCalled();
|
||||
expect(sentEmbeds.length).toBeGreaterThan(0);
|
||||
});
|
||||
test.todo('GIVEN channel IS NULL, EXPECT embed to not be sent');
|
||||
|
||||
// DM user
|
||||
test('GIVEN user can NOT be messaged, EXPECT resultEmbed to contain "DM Sent = false"', async () => {
|
||||
let embeds: APIEmbed[] | undefined;
|
||||
|
||||
const command = new Timeout();
|
||||
|
||||
const interactionReply = jest.fn((options: InteractionReplyOptions) => {
|
||||
embeds = options.embeds as APIEmbed[];
|
||||
});
|
||||
|
||||
let savedAudit: DeepPartial<Audit> | undefined;
|
||||
|
||||
const getSetting = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
|
||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
||||
savedAudit = entity;
|
||||
|
||||
return Promise.resolve();
|
||||
});
|
||||
|
||||
const timeoutFunc = jest.fn();
|
||||
|
||||
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||
|
||||
const dmChannel = {
|
||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
||||
dmChannelSentEmbeds = options.embeds;
|
||||
}),
|
||||
} as unknown as DMChannel;
|
||||
|
||||
const userInput = {
|
||||
user: {
|
||||
id: 'userId',
|
||||
tag: 'userTag',
|
||||
createDM: jest.fn().mockRejectedValue(undefined),
|
||||
} as unknown as User,
|
||||
member: {
|
||||
manageable: true,
|
||||
timeout: timeoutFunc,
|
||||
} as unknown as GuildMember,
|
||||
} as CommandInteractionOption;
|
||||
|
||||
const lengthInput = {
|
||||
value: '1s',
|
||||
} as CommandInteractionOption;
|
||||
|
||||
const reasonInput = {
|
||||
value: 'Test reason',
|
||||
} as CommandInteractionOption;
|
||||
|
||||
const logsChannel = {
|
||||
name: 'mod-logs',
|
||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
||||
logsChannelSentEmbeds = options.embeds;
|
||||
}),
|
||||
} as unknown as TextChannel;
|
||||
|
||||
const interaction = {
|
||||
guild: {
|
||||
channels: {
|
||||
cache: {
|
||||
find: jest.fn()
|
||||
.mockReturnValue(logsChannel),
|
||||
}
|
||||
},
|
||||
name: "Test Guild",
|
||||
} as unknown as Guild,
|
||||
guildId: 'guildId',
|
||||
reply: interactionReply,
|
||||
options: {
|
||||
get: jest.fn()
|
||||
.mockReturnValueOnce(userInput)
|
||||
.mockReturnValueOnce(lengthInput)
|
||||
.mockReturnValue(reasonInput),
|
||||
},
|
||||
user: {
|
||||
id: 'moderatorId'
|
||||
}
|
||||
} as unknown as CommandInteraction;
|
||||
|
||||
await command.execute(interaction);
|
||||
|
||||
// EXPECT embeds to be sent
|
||||
expect(embeds).toBeDefined();
|
||||
expect(embeds!.length).toBe(1);
|
||||
|
||||
const resultEmbed = embeds![0] as EmbedBuilder;
|
||||
|
||||
// EXPECT DM field to be configured
|
||||
const resultEmbedDMField = resultEmbed.data.fields![0];
|
||||
|
||||
expect(resultEmbedDMField.name).toBe("DM Sent");
|
||||
expect(resultEmbedDMField.value).toBe("false");
|
||||
});
|
||||
test.todo('GIVEN user can NOT be messaged, EXPECT resultEmbed to contain "DM Sent = false"');
|
||||
});
|
Loading…
Reference in a new issue