|
1 |
| -import{screen}from"@testing-library/react" |
| 1 | +import{fireEvent,screen,waitFor,within}from"@testing-library/react" |
2 | 2 | importReactfrom"react"
|
3 |
| -import{render}from"../../testHelpers" |
4 |
| -import{UsersPage}from"./UsersPage" |
| 3 | +import*asAPIfrom"../../api" |
| 4 | +import{GlobalSnackbar}from"../../components/GlobalSnackbar/GlobalSnackbar" |
| 5 | +import{LanguageasUsersTableLanguage}from"../../components/UsersTable/UsersTable" |
| 6 | +import{MockUser,MockUser2,render}from"../../testHelpers" |
| 7 | +import{LanguageasusersXServiceLanguage}from"../../xServices/users/usersXService" |
| 8 | +import{LanguageasUsersPageLanguage,UsersPage}from"./UsersPage" |
| 9 | + |
| 10 | +constsuspendUser=async(setupActionSpies:()=>void)=>{ |
| 11 | +// Get the first user in the table |
| 12 | +constusers=awaitscreen.findAllByText(/.*@coder.com/) |
| 13 | +constfirstUserRow=users[0].closest("tr") |
| 14 | +if(!firstUserRow){ |
| 15 | +thrownewError("Error on get the first user row") |
| 16 | +} |
| 17 | + |
| 18 | +// Click on the "more" button to display the "Suspend" option |
| 19 | +constmoreButton=within(firstUserRow).getByLabelText("more") |
| 20 | +fireEvent.click(moreButton) |
| 21 | +constmenu=screen.getByRole("menu") |
| 22 | +constsuspendButton=within(menu).getByText(UsersTableLanguage.suspendMenuItem) |
| 23 | +fireEvent.click(suspendButton) |
| 24 | + |
| 25 | +// Check if the confirm message is displayed |
| 26 | +constconfirmDialog=screen.getByRole("dialog") |
| 27 | +expect(confirmDialog).toHaveTextContent(`${UsersPageLanguage.suspendDialogMessagePrefix}${MockUser.username}?`) |
| 28 | + |
| 29 | +// Setup spies to check the actions after |
| 30 | +setupActionSpies() |
| 31 | + |
| 32 | +// Click on the "Confirm" button |
| 33 | +constconfirmButton=within(confirmDialog).getByText(UsersPageLanguage.suspendDialogAction) |
| 34 | +fireEvent.click(confirmButton) |
| 35 | +} |
5 | 36 |
|
6 | 37 | describe("Users Page",()=>{
|
7 | 38 | it("shows users",async()=>{
|
8 | 39 | render(<UsersPage/>)
|
9 | 40 | constusers=awaitscreen.findAllByText(/.*@coder.com/)
|
10 | 41 | expect(users.length).toEqual(2)
|
11 | 42 | })
|
| 43 | + |
| 44 | +describe("suspend user",()=>{ |
| 45 | +describe("when it is success",()=>{ |
| 46 | +it("shows a success message and refresh the page",async()=>{ |
| 47 | +render( |
| 48 | +<> |
| 49 | +<UsersPage/> |
| 50 | +<GlobalSnackbar/> |
| 51 | +</>, |
| 52 | +) |
| 53 | + |
| 54 | +awaitsuspendUser(()=>{ |
| 55 | +jest.spyOn(API,"suspendUser").mockResolvedValueOnce(MockUser) |
| 56 | +jest.spyOn(API,"getUsers").mockImplementationOnce(()=>Promise.resolve([MockUser,MockUser2])) |
| 57 | +}) |
| 58 | + |
| 59 | +// Check if the success message is displayed |
| 60 | +awaitscreen.findByText(usersXServiceLanguage.suspendUserSuccess) |
| 61 | + |
| 62 | +// Check if the API was called correctly |
| 63 | +expect(API.suspendUser).toBeCalledTimes(1) |
| 64 | +expect(API.suspendUser).toBeCalledWith(MockUser.id) |
| 65 | + |
| 66 | +// Check if the users list was reload |
| 67 | +awaitwaitFor(()=>expect(API.getUsers).toBeCalledTimes(1)) |
| 68 | +}) |
| 69 | +}) |
| 70 | + |
| 71 | +describe("when it fails",()=>{ |
| 72 | +it("shows an error message",async()=>{ |
| 73 | +render( |
| 74 | +<> |
| 75 | +<UsersPage/> |
| 76 | +<GlobalSnackbar/> |
| 77 | +</>, |
| 78 | +) |
| 79 | + |
| 80 | +awaitsuspendUser(()=>{ |
| 81 | +jest.spyOn(API,"suspendUser").mockRejectedValueOnce({}) |
| 82 | +}) |
| 83 | + |
| 84 | +// Check if the success message is displayed |
| 85 | +awaitscreen.findByText(usersXServiceLanguage.suspendUserError) |
| 86 | + |
| 87 | +// Check if the API was called correctly |
| 88 | +expect(API.suspendUser).toBeCalledTimes(1) |
| 89 | +expect(API.suspendUser).toBeCalledWith(MockUser.id) |
| 90 | +}) |
| 91 | +}) |
| 92 | +}) |
12 | 93 | })
|