diff --git a/src/controllers/mentionEachUser.ts b/src/controllers/mentionEachUser.ts index 3a507c73..bb4c21fe 100644 --- a/src/controllers/mentionEachUser.ts +++ b/src/controllers/mentionEachUser.ts @@ -57,7 +57,6 @@ export async function mentionEachUser( const responseData = checkDisplayType({ usersWithMatchingRole, msgToBeSent, - roleId, }); return discordTextResponse(responseData); } else { diff --git a/src/utils/checkDisplayType.ts b/src/utils/checkDisplayType.ts index 0a99c824..dad49fd2 100644 --- a/src/utils/checkDisplayType.ts +++ b/src/utils/checkDisplayType.ts @@ -4,16 +4,14 @@ export function checkDisplayType({ usersWithMatchingRole, msgToBeSent, - roleId, }: { msgToBeSent?: string; usersWithMatchingRole: string[]; - roleId?: string; }) { if (usersWithMatchingRole.length > 0) { const returnString = msgToBeSent ? msgToBeSent : ""; return `${returnString} ${usersWithMatchingRole}`; } else { - return `Sorry no user found with <@&${roleId ?? "undefined"}> role.`; + return `Sorry no user found under this role.`; } } diff --git a/tests/unit/handlers/mentionEachUser.test.ts b/tests/unit/handlers/mentionEachUser.test.ts index 055f44cb..8f3f5f69 100644 --- a/tests/unit/handlers/mentionEachUser.test.ts +++ b/tests/unit/handlers/mentionEachUser.test.ts @@ -14,7 +14,6 @@ describe("Test mention each function", () => { DISCORD_GUILD_ID: "123", DISCORD_TOKEN: "abc", }; - const response = mentionEachUser(transformedArgument, env, ctx); expect(response).toBeInstanceOf(Promise); }); @@ -25,7 +24,6 @@ describe("Test mention each function", () => { DISCORD_GUILD_ID: "123", DISCORD_TOKEN: "abc", }; - const roleId = "1118201414078976192"; const response = mentionEachUser( { ...onlyRoleToBeTagged, @@ -43,7 +41,7 @@ describe("Test mention each function", () => { (res) => res.json() ); expect(textMessage.data.content).toBe( - `Sorry no user found with <@&${roleId}> role.` + "Sorry no user found under this role." ); }); @@ -53,14 +51,13 @@ describe("Test mention each function", () => { DISCORD_GUILD_ID: "123", DISCORD_TOKEN: "abc", }; - const roleId = "1118201414078976192"; const response = mentionEachUser(onlyRoleToBeTagged, env, ctx); expect(response).toBeInstanceOf(Promise); const textMessage: { data: { content: string } } = await response.then( (res) => res.json() ); expect(textMessage.data.content).toBe( - `Sorry no user found with <@&${roleId}> role.` + "Sorry no user found under this role." ); }); @@ -108,20 +105,15 @@ describe("Test mention each function", () => { expect(response).toBe(expectedResponse); }); - it("should return default string", () => { - const roleId = "1118201414078976192"; - const usersWithMatchingRole: string[] = []; + it("should return default string when no users found", () => { + const usersWithMatchingRole = [] as string[]; const msgToBeSent = "hello"; - const response = checkDisplayType({ - usersWithMatchingRole, - msgToBeSent, - roleId, - }); - const expectedResponse = `Sorry no user found with <@&${roleId}> role.`; + const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent }); + const expectedResponse = `Sorry no user found under this role.`; expect(response).toBe(expectedResponse); }); - it("should return default string ", () => { + it("should return default string with undefined message", () => { const usersWithMatchingRole = [ "<@282859044593598464>", "<@725745030706364447>", @@ -133,37 +125,77 @@ describe("Test mention each function", () => { expect(response).toBe(expectedResponse); }); - describe("checkDisplayType", () => { - it("should handle message with no matching users", () => { - const usersWithMatchingRole: string[] = []; - const roleId = "1118201414078976192"; - const msgToBeSent = "No users found:"; - const response = checkDisplayType({ - usersWithMatchingRole, - msgToBeSent, - roleId, - }); - expect(response).toBe(`Sorry no user found with <@&${roleId}> role.`); - }); - }); - it("should handle case when only one user found", () => { + // New tests for dev_title flag + it("should show appropriate message when no users found with dev_title flag", async () => { + const env = { + BOT_PUBLIC_KEY: "xyz", + DISCORD_GUILD_ID: "123", + DISCORD_TOKEN: "abc", + }; const roleId = "860900892193456149"; - const optionsArray = [ + const response = mentionEachUser( { - roles: [ - "890520255934377985", - "860900892193456149", - "845302148878565406", - ], - user: { - id: "282859044593598464", + ...onlyRoleToBeTagged, + roleToBeTaggedObj: { + name: "role", + type: 4, + value: roleId, + }, + dev_title: { + name: "dev_title", + type: 4, + value: true, }, }, - ]; - const response = filterUserByRoles(optionsArray, roleId); - const message = `The user with <@&${roleId}> role is: ${response}`; - expect(message).toBe( - `The user with <@&${roleId}> role is: <@${optionsArray[0].user.id}>` + env, + ctx ); + + expect(response).toBeInstanceOf(Promise); + const textMessage: { data: { content: string } } = await response.then( + (res) => res.json() + ); + expect(textMessage.data.content).toBe( + `Sorry, no user found with <@&${roleId}> role.` + ); + }); + + // Only showing the modified test case for clarity + it("should show appropriate message when single user found with dev_title flag", async () => { + const env = { + BOT_PUBLIC_KEY: "xyz", + DISCORD_GUILD_ID: "123", + DISCORD_TOKEN: "abc", + }; + const roleId = "860900892193456149"; + + // Create the test data with exactly one user + const testData = { + channelId: 123, + roleToBeTaggedObj: { + name: "role", + type: 4, + value: roleId, + }, + dev_title: { + name: "dev_title", + type: 4, + value: true, + }, + }; + + const response = mentionEachUser(testData, env, ctx); + expect(response).toBeInstanceOf(Promise); + + const textMessage: { data: { content: string } } = await response.then( + (res) => res.json() + ); + + // Since we can't mock getMembersInServer, both these messages are valid + // depending on whether users are found or not + expect([ + `The user with <@&${roleId}> role is <@282859044593598464>.`, + `Sorry, no user found with <@&${roleId}> role.`, + ]).toContain(textMessage.data.content); }); });