Skip to content

Commit

Permalink
made some test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RishiChaubey31 committed Oct 30, 2024
1 parent 19a03b9 commit 47b865d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 46 deletions.
1 change: 0 additions & 1 deletion src/controllers/mentionEachUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export async function mentionEachUser(
const responseData = checkDisplayType({
usersWithMatchingRole,
msgToBeSent,
roleId,
});
return discordTextResponse(responseData);
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/utils/checkDisplayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;
}
}
116 changes: 74 additions & 42 deletions tests/unit/handlers/mentionEachUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -25,7 +24,6 @@ describe("Test mention each function", () => {
DISCORD_GUILD_ID: "123",
DISCORD_TOKEN: "abc",
};
const roleId = "1118201414078976192";
const response = mentionEachUser(
{
...onlyRoleToBeTagged,
Expand All @@ -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."
);
});

Expand All @@ -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."
);
});

Expand Down Expand Up @@ -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>",
Expand All @@ -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);
});
});

0 comments on commit 47b865d

Please sign in to comment.