Skip to content

Commit 8a2a1ba

Browse files
authored
Spam ban: Fix tests (#755)
Feature works in prod, just test mock was incomplete. Formatting fixed and method casing kept consistent.
1 parent 9132759 commit 8a2a1ba

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

services/spam-ban/__snapshots__/spam-banning.service.test.js.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ exports[`Banning spammer in automod channel Ban user if in the automod channel 2
8181
{
8282
"content": "Successfully banned <@123> for spam.",
8383
"flags": 64,
84-
8584
}
8685
`;
8786

@@ -166,7 +165,6 @@ exports[`Banning spammer in other channels Bans user and deletes their messages
166165

167166
exports[`Banning spammer in other channels Bans user and does not delete their messages if keep message button clicked 1`] = `
168167
{
169-
170168
"deleteMessageSeconds": 0,
171169
"reason": "Account is compromised",
172170
}

services/spam-ban/spam-banning.service.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
const {
32
EmbedBuilder,
43
ButtonBuilder,
@@ -25,7 +24,7 @@ class SpamBanningService {
2524

2625
if (message.channelId !== config.channels.automodBlockChannelId) {
2726
const response =
28-
await SpamBanningService.#AskForDeleteMessages(interaction);
27+
await SpamBanningService.#askForDeleteMessages(interaction);
2928

3029
if (response.result === 'cancel') {
3130
await interaction.editReply({
@@ -81,7 +80,7 @@ class SpamBanningService {
8180
}
8281
}
8382

84-
static async #AskForDeleteMessages(interaction) {
83+
static async #askForDeleteMessages(interaction) {
8584
const keepMessages = new ButtonBuilder()
8685
.setCustomId('keepMessages')
8786
.setLabel("Don't delete messages")
@@ -111,18 +110,19 @@ class SpamBanningService {
111110
});
112111

113112
try {
114-
const buttonInteraction = await response.resource.message.awaitMessageComponent({ time: 60_000 });
115-
113+
const buttonInteraction =
114+
await response.resource.message.awaitMessageComponent({ time: 60_000 });
115+
116116
// Handle the button interaction
117117
if (buttonInteraction.customId === 'cancel') {
118-
await buttonInteraction.update({
119-
content: 'Action has been cancelled.',
120-
components: [],
121-
});
122-
} else {
123-
// Acknowledge the button press but don't change the message yet
124-
await buttonInteraction.deferUpdate();
125-
}
118+
await buttonInteraction.update({
119+
content: 'Action has been cancelled.',
120+
components: [],
121+
});
122+
} else {
123+
// Acknowledge the button press but don't change the message yet
124+
await buttonInteraction.deferUpdate();
125+
}
126126
return { result: buttonInteraction.customId };
127127
} catch {
128128
return { result: 'timeout' };
@@ -143,10 +143,12 @@ class SpamBanningService {
143143
} else {
144144
reply = `Banned <@${message.author.id}> for spam but wasn't able to contact the user as they have left the server.`;
145145
}
146+
146147
await guild.members.ban(message.author.id, {
147148
reason: 'Account is compromised',
148149
deleteMessageSeconds: deleteMessages ? 60 * 60 * 24 * 7 : 0,
149150
});
151+
150152
return reply;
151153
}
152154

services/spam-ban/spam-banning.service.test.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ function createInteractionMock(message, guild) {
2222
reply: jest.fn((arg) => {
2323
replyArg = arg;
2424
return {
25-
awaitMessageComponent: jest.fn(() => {
26-
if (messageComponentReturn === 'timeout') {
27-
return Promise.reject();
28-
}
29-
return Promise.resolve({ customId: messageComponentReturn });
30-
}),
25+
resource: {
26+
message: {
27+
awaitMessageComponent: () => {
28+
if (messageComponentReturn === 'timeout') {
29+
return Promise.reject();
30+
}
31+
return Promise.resolve({
32+
customId: messageComponentReturn,
33+
deferUpdate: () => {},
34+
});
35+
},
36+
},
37+
},
3138
};
3239
}),
3340
guild,
@@ -130,8 +137,7 @@ describe('Banning spammer in automod channel', () => {
130137

131138
it('Ban user if in the automod channel', async () => {
132139
await SpamBanningService.handleInteraction(interactionMock);
133-
await SpamBanningService.handleInteraction(interactionMock);
134-
// expect(interactionMock.guild.members.ban).toHaveBeenCalledTimes(1);
140+
expect(interactionMock.guild.members.ban).toHaveBeenCalledTimes(1);
135141
expect(interactionMock.getBanArg()).toMatchSnapshot();
136142
expect(interactionMock.getReplyArg()).toMatchSnapshot();
137143
});

0 commit comments

Comments
 (0)