|
1 | 1 | import{fireEvent,screen,waitFor,within}from"@testing-library/react"
|
2 | 2 | importReactfrom"react"
|
3 | 3 | import*asAPIfrom"../../api"
|
| 4 | +import{Role}from"../../api/typesGenerated" |
4 | 5 | import{GlobalSnackbar}from"../../components/GlobalSnackbar/GlobalSnackbar"
|
5 | 6 | import{LanguageasResetPasswordDialogLanguage}from"../../components/ResetPasswordDialog/ResetPasswordDialog"
|
| 7 | +import{LanguageasRoleSelectLanguage}from"../../components/RoleSelect/RoleSelect" |
6 | 8 | import{LanguageasUsersTableLanguage}from"../../components/UsersTable/UsersTable"
|
7 |
| -import{MockUser,MockUser2,render}from"../../testHelpers" |
| 9 | +import{MockAuditorRole,MockUser,MockUser2,render}from"../../testHelpers" |
8 | 10 | import{LanguageasusersXServiceLanguage}from"../../xServices/users/usersXService"
|
9 | 11 | import{LanguageasUsersPageLanguage,UsersPage}from"./UsersPage"
|
10 | 12 |
|
@@ -62,6 +64,34 @@ const resetUserPassword = async (setupActionSpies: () => void) => {
|
62 | 64 | fireEvent.click(confirmButton)
|
63 | 65 | }
|
64 | 66 |
|
| 67 | +constupdateUserRole=async(setupActionSpies:()=>void,role:Role)=>{ |
| 68 | +// Get the first user in the table |
| 69 | +constusers=awaitscreen.findAllByText(/.*@coder.com/) |
| 70 | +constfirstUserRow=users[0].closest("tr") |
| 71 | +if(!firstUserRow){ |
| 72 | +thrownewError("Error on get the first user row") |
| 73 | +} |
| 74 | + |
| 75 | +// Click on the "roles" menu to display the role options |
| 76 | +constrolesLabel=within(firstUserRow).getByLabelText(RoleSelectLanguage.label) |
| 77 | +constrolesMenuTrigger=within(rolesLabel).getByRole("button") |
| 78 | +// For MUI v4, the Select was changed to open on mouseDown instead of click |
| 79 | +// https://github.com/mui-org/material-ui/pull/17978 |
| 80 | +fireEvent.mouseDown(rolesMenuTrigger) |
| 81 | + |
| 82 | +// Setup spies to check the actions after |
| 83 | +setupActionSpies() |
| 84 | + |
| 85 | +// Click on the role option |
| 86 | +constlistBox=screen.getByRole("listbox") |
| 87 | +constauditorOption=within(listBox).getByRole("option",{name:role.display_name}) |
| 88 | +fireEvent.click(auditorOption) |
| 89 | + |
| 90 | +return{ |
| 91 | + rolesMenuTrigger, |
| 92 | +} |
| 93 | +} |
| 94 | + |
65 | 95 | describe("Users Page",()=>{
|
66 | 96 | it("shows users",async()=>{
|
67 | 97 | render(<UsersPage/>)
|
@@ -164,4 +194,55 @@ describe("Users Page", () => {
|
164 | 194 | })
|
165 | 195 | })
|
166 | 196 | })
|
| 197 | + |
| 198 | +describe("Update user role",()=>{ |
| 199 | +describe("when it is success",()=>{ |
| 200 | +it("updates the roles",async()=>{ |
| 201 | +render( |
| 202 | +<> |
| 203 | +<UsersPage/> |
| 204 | +<GlobalSnackbar/> |
| 205 | +</>, |
| 206 | +) |
| 207 | + |
| 208 | +const{ rolesMenuTrigger}=awaitupdateUserRole(()=>{ |
| 209 | +jest.spyOn(API,"updateUserRoles").mockResolvedValueOnce({ |
| 210 | + ...MockUser, |
| 211 | +roles:[...MockUser.roles,MockAuditorRole], |
| 212 | +}) |
| 213 | +},MockAuditorRole) |
| 214 | + |
| 215 | +// Check if the select text was updated with the Auditor role |
| 216 | +awaitwaitFor(()=>expect(rolesMenuTrigger).toHaveTextContent("Admin, Member, Auditor")) |
| 217 | + |
| 218 | +// Check if the API was called correctly |
| 219 | +constcurrentRoles=MockUser.roles.map((r)=>r.name) |
| 220 | +expect(API.updateUserRoles).toBeCalledTimes(1) |
| 221 | +expect(API.updateUserRoles).toBeCalledWith([...currentRoles,MockAuditorRole.name],MockUser.id) |
| 222 | +}) |
| 223 | +}) |
| 224 | + |
| 225 | +describe("when it fails",()=>{ |
| 226 | +it("shows an error message",async()=>{ |
| 227 | +render( |
| 228 | +<> |
| 229 | +<UsersPage/> |
| 230 | +<GlobalSnackbar/> |
| 231 | +</>, |
| 232 | +) |
| 233 | + |
| 234 | +awaitupdateUserRole(()=>{ |
| 235 | +jest.spyOn(API,"updateUserRoles").mockRejectedValueOnce({}) |
| 236 | +},MockAuditorRole) |
| 237 | + |
| 238 | +// Check if the error message is displayed |
| 239 | +awaitscreen.findByText(usersXServiceLanguage.updateUserRolesError) |
| 240 | + |
| 241 | +// Check if the API was called correctly |
| 242 | +constcurrentRoles=MockUser.roles.map((r)=>r.name) |
| 243 | +expect(API.updateUserRoles).toBeCalledTimes(1) |
| 244 | +expect(API.updateUserRoles).toBeCalledWith([...currentRoles,MockAuditorRole.name],MockUser.id) |
| 245 | +}) |
| 246 | +}) |
| 247 | +}) |
167 | 248 | })
|