Add tests
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Ethan Lane 2023-05-27 14:52:01 +01:00
parent 177f7a3072
commit 6af92ad1c7

View file

@ -194,7 +194,19 @@ describe('execute', () => {
expect(interaction.reply).not.toBeCalled();
});
test.todo('GIVEN interaction.guildId IS NULL, EXPECT nothing to happen');
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();
});
// Validation
test('GIVEN targetUser IS NULL, EXPECT validation error', async () => {
@ -469,10 +481,249 @@ describe('execute', () => {
});
// Log embed
test.todo('GIVEN channelName IS NULL, EXPECT execution to return');
test('GIVEN channelName IS NULL, EXPECT execution to return', async () => {
const command = new Timeout();
test.todo('GIVEN channel IS NULL, EXPECT embed to not be sent');
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);
});
// DM user
test.todo('GIVEN user can NOT be messaged, EXPECT resultEmbed to contain "DM Sent = false"');
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");
});
});