Skip to content

Commit 51cd245

Browse files
added a case when only one user is found and a suited test case
1 parent 1a1db21 commit 51cd245

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

src/controllers/mentionEachUser.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ export async function mentionEachUser(
1919
dev?: DevFlag;
2020
},
2121
env: env,
22-
ctx: ExecutionContext
22+
ctx: ExecutionContext,
23+
mockUserData?: UserArray[] // Optional parametr to pass user data directly for testing
2324
) {
24-
const getMembersInServerResponse = await getMembersInServer(env);
25+
const getMembersInServerResponse = mockUserData || await getMembersInServer(env);
2526
const roleId = transformedArgument.roleToBeTaggedObj.value;
2627
const msgToBeSent = transformedArgument?.displayMessageObj?.value; // Get custom message
2728
const dev = transformedArgument?.dev?.value || false;
@@ -33,11 +34,15 @@ export async function mentionEachUser(
3334

3435
// Use the custom message if provided, otherwise construct the default message
3536
let responseMessage;
37+
const roleTag = `<@&${roleId}>`;
38+
const userList = usersWithMatchingRole.join(", ");
39+
3640
if (usersWithMatchingRole.length === 0) {
37-
responseMessage = msgToBeSent || "Sorry no user found under this role."; // Use custom message if available
41+
responseMessage =
42+
msgToBeSent || `Sorry no user found under ${roleTag} role.`; // Use custom message if available
43+
} else if (usersWithMatchingRole.length === 1) {
44+
responseMessage = `The user with ${roleTag} role is: ${userList}`; // Handle singular user case
3845
} else {
39-
const roleTag = `<@&${roleId}>`;
40-
const userList = usersWithMatchingRole.join(", ");
4146
responseMessage = `The users with ${roleTag} roles are: ${userList}`;
4247
}
4348

tests/unit/handlers/mentionEachUser.test.ts

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ describe("Test mention each function", () => {
2525
DISCORD_GUILD_ID: "123",
2626
DISCORD_TOKEN: "abc",
2727
};
28+
const roleId = "123456789";
2829
const response = mentionEachUser(
2930
{
3031
...onlyRoleToBeTagged,
32+
roleToBeTaggedObj: {
33+
name: "roleToBeTagged",
34+
type: 3,
35+
value: roleId,
36+
},
3137
dev: {
3238
name: "dev",
3339
type: 4,
@@ -42,7 +48,7 @@ describe("Test mention each function", () => {
4248
(res) => res.json()
4349
);
4450
expect(textMessage.data.content).toBe(
45-
"Sorry no user found under this role."
51+
`Sorry no user found under <@&${roleId}> role.`
4652
);
4753
});
4854

@@ -52,14 +58,25 @@ describe("Test mention each function", () => {
5258
DISCORD_GUILD_ID: "123",
5359
DISCORD_TOKEN: "abc",
5460
};
55-
56-
const response = mentionEachUser(onlyRoleToBeTagged, env, ctx);
61+
const roleId = "123456789";
62+
const response = mentionEachUser(
63+
{
64+
...onlyRoleToBeTagged,
65+
roleToBeTaggedObj: {
66+
name: "roleToBeTagged",
67+
type: 3,
68+
value: roleId,
69+
},
70+
},
71+
env,
72+
ctx
73+
);
5774
expect(response).toBeInstanceOf(Promise);
5875
const textMessage: { data: { content: string } } = await response.then(
5976
(res) => res.json()
6077
);
6178
expect(textMessage.data.content).toBe(
62-
"Sorry no user found under this role."
79+
`Sorry no user found under <@&${roleId}> role.`
6380
);
6481
});
6582

@@ -162,4 +179,43 @@ describe("Test mention each function", () => {
162179
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
163180
expect(response).toBe("Sorry no user found under this role.");
164181
});
182+
183+
it("should return message when only one user found", async () => {
184+
const env = {
185+
BOT_PUBLIC_KEY: "xyz",
186+
DISCORD_GUILD_ID: "123",
187+
DISCORD_TOKEN: "abc",
188+
};
189+
190+
const roleId = "123456789";
191+
const userId = "256859044593598464";
192+
193+
const mockUserData = [
194+
{
195+
roles: ["123456789"],
196+
user: { id: userId },
197+
},
198+
];
199+
200+
const onlyRoleToBeTagged = {
201+
roleToBeTaggedObj: {
202+
name: "roleToBeTagged",
203+
type: 3,
204+
value: roleId,
205+
},
206+
channelId: 12345,
207+
};
208+
209+
// Call mentionEachUser and pass in the mock user data
210+
const response = await mentionEachUser(onlyRoleToBeTagged, env, ctx, mockUserData);
211+
212+
// Extract the message content from the response
213+
const textMessage: { data: { content: string } } = await response.json();
214+
215+
// Construct the expected response based on the function logic
216+
const expectedResponse = `The user with <@&${roleId}> role is: <@${userId}>`;
217+
218+
// Assert that the response message content matches the expected output
219+
expect(textMessage.data.content).toBe(expectedResponse);
220+
});
165221
});

0 commit comments

Comments
 (0)