@@ -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