Skip to content

Commit 1a16513

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

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/controllers/mentionEachUser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export async function mentionEachUser(
2222
ctx: ExecutionContext,
2323
mockUserData?: UserArray[] // Optional parametr to pass user data directly for testing
2424
) {
25-
const getMembersInServerResponse = mockUserData || await getMembersInServer(env);
25+
const getMembersInServerResponse =
26+
mockUserData || (await getMembersInServer(env));
2627
const roleId = transformedArgument.roleToBeTaggedObj.value;
2728
const msgToBeSent = transformedArgument?.displayMessageObj?.value; // Get custom message
2829
const dev = transformedArgument?.dev?.value || false;
@@ -41,7 +42,7 @@ export async function mentionEachUser(
4142
responseMessage =
4243
msgToBeSent || `Sorry no user found under ${roleTag} role.`; // Use custom message if available
4344
} else if (usersWithMatchingRole.length === 1) {
44-
responseMessage = `The user with ${roleTag} role is: ${userList}`; // Handle singular user case
45+
responseMessage = `The user with ${roleTag} role is: ${userList}`; // Handle singular user case
4546
} else {
4647
responseMessage = `The users with ${roleTag} roles are: ${userList}`;
4748
}

tests/unit/handlers/mentionEachUser.test.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,17 @@ describe("Test mention each function", () => {
186186
DISCORD_GUILD_ID: "123",
187187
DISCORD_TOKEN: "abc",
188188
};
189-
190-
const roleId = "123456789";
191-
const userId = "256859044593598464";
192-
189+
190+
const roleId = "123456789";
191+
const userId = "256859044593598464";
192+
193193
const mockUserData = [
194194
{
195-
roles: ["123456789"],
195+
roles: ["123456789"],
196196
user: { id: userId },
197197
},
198198
];
199-
199+
200200
const onlyRoleToBeTagged = {
201201
roleToBeTaggedObj: {
202202
name: "roleToBeTagged",
@@ -205,17 +205,22 @@ describe("Test mention each function", () => {
205205
},
206206
channelId: 12345,
207207
};
208-
208+
209209
// Call mentionEachUser and pass in the mock user data
210-
const response = await mentionEachUser(onlyRoleToBeTagged, env, ctx, mockUserData);
211-
210+
const response = await mentionEachUser(
211+
onlyRoleToBeTagged,
212+
env,
213+
ctx,
214+
mockUserData
215+
);
216+
212217
// Extract the message content from the response
213218
const textMessage: { data: { content: string } } = await response.json();
214-
219+
215220
// Construct the expected response based on the function logic
216221
const expectedResponse = `The user with <@&${roleId}> role is: <@${userId}>`;
217-
222+
218223
// Assert that the response message content matches the expected output
219224
expect(textMessage.data.content).toBe(expectedResponse);
220-
});
225+
});
221226
});

0 commit comments

Comments
 (0)