Skip to content

Commit 5153cb1

Browse files
committed
Add test
1 parent 725b651 commit 5153cb1

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

site/src/components/UsersTable/UsersTableBody.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const Language = {
1515
emptyMessage: "No users found",
1616
suspendMenuItem: "Suspend",
1717
deleteMenuItem: "Delete",
18-
listWorkspacesMenuItem: "View Workspaces",
18+
listWorkspacesMenuItem: "View workspaces",
1919
activateMenuItem: "Activate",
2020
resetPasswordMenuItem: "Reset password",
2121
}

site/src/pages/UsersPage/UsersPage.test.tsx

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ const suspendUser = async (setupActionSpies: () => void) => {
5252
fireEvent.click(confirmButton)
5353
}
5454

55+
const deleteUser = async (setupActionSpies: () => void) => {
56+
// Get the first user in the table
57+
const users = await screen.findAllByText(/.*@coder.com/)
58+
const firstUserRow = users[0].closest("tr")
59+
if (!firstUserRow) {
60+
throw new Error("Error on get the first user row")
61+
}
62+
63+
// Click on the "more" button to display the "Suspend" option
64+
const moreButton = within(firstUserRow).getByLabelText("more")
65+
66+
fireEvent.click(moreButton)
67+
68+
const menu = await screen.findByRole("menu")
69+
const suspendButton = within(menu).getByText(UsersTableBodyLanguage.deleteMenuItem)
70+
71+
fireEvent.click(suspendButton)
72+
73+
// Check if the confirm message is displayed
74+
const confirmDialog = await screen.findByRole("dialog")
75+
expect(confirmDialog).toHaveTextContent(
76+
`${UsersPageLanguage.deleteDialogMessagePrefix} ${MockUser.username}?`,
77+
)
78+
79+
// Setup spies to check the actions after
80+
setupActionSpies()
81+
82+
// Click on the "Confirm" button
83+
const confirmButton = within(confirmDialog).getByText(UsersPageLanguage.deleteDialogAction)
84+
fireEvent.click(confirmButton)
85+
}
86+
5587
const activateUser = async (setupActionSpies: () => void) => {
5688
// Get the first user in the table
5789
const users = await screen.findAllByText(/.*@coder.com/)
@@ -229,6 +261,57 @@ describe("UsersPage", () => {
229261
})
230262
})
231263

264+
describe("delete user", () => {
265+
describe("when it is success", () => {
266+
it("shows a success message and refresh the page", async () => {
267+
render(
268+
<>
269+
<UsersPage />
270+
<GlobalSnackbar />
271+
</>,
272+
)
273+
274+
await deleteUser(() => {
275+
jest.spyOn(API, "deleteUser").mockResolvedValueOnce(undefined)
276+
jest
277+
.spyOn(API, "getUsers")
278+
.mockImplementationOnce(() => Promise.resolve([MockUser, MockUser2]))
279+
})
280+
281+
// Check if the success message is displayed
282+
screen.findByText(usersXServiceLanguage.deleteUserSuccess)
283+
284+
// Check if the API was called correctly
285+
expect(API.deleteUser).toBeCalledTimes(1)
286+
expect(API.deleteUser).toBeCalledWith(MockUser.id)
287+
288+
// Check if the users list was reload
289+
await waitFor(() => expect(API.getUsers).toBeCalledTimes(1))
290+
})
291+
})
292+
describe("when it fails", () => {
293+
it("shows an error message", async () => {
294+
render(
295+
<>
296+
<UsersPage />
297+
<GlobalSnackbar />
298+
</>,
299+
)
300+
301+
await deleteUser(() => {
302+
jest.spyOn(API, "deleteUser").mockRejectedValueOnce({})
303+
})
304+
305+
// Check if the error message is displayed
306+
screen.findByText(usersXServiceLanguage.deleteUserError)
307+
308+
// Check if the API was called correctly
309+
expect(API.deleteUser).toBeCalledTimes(1)
310+
expect(API.deleteUser).toBeCalledWith(MockUser.id)
311+
})
312+
})
313+
})
314+
232315
describe("activate user", () => {
233316
describe("when user is successfully activated", () => {
234317
it("shows a success message and refreshes the page", async () => {

0 commit comments

Comments
 (0)