Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88643c6

Browse files
committed
added test
1 parent 7a322f4 commit 88643c6

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

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

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { GlobalSnackbar } from "../../components/GlobalSnackbar/GlobalSnackbar"
66
import { Language as ResetPasswordDialogLanguage } from "../../components/ResetPasswordDialog/ResetPasswordDialog"
77
import { Language as RoleSelectLanguage } from "../../components/RoleSelect/RoleSelect"
88
import { Language as UsersTableLanguage } from "../../components/UsersTable/UsersTable"
9-
import { MockAuditorRole, MockUser, MockUser2, render } from "../../testHelpers/renderHelpers"
9+
import { MockAuditorRole, MockUser, MockUser2, render, SuspendedMockUser } from "../../testHelpers/renderHelpers"
1010
import { server } from "../../testHelpers/server"
1111
import { permissionsToCheck } from "../../xServices/auth/authXService"
1212
import { Language as usersXServiceLanguage } from "../../xServices/users/usersXService"
@@ -40,6 +40,35 @@ const suspendUser = async (setupActionSpies: () => void) => {
4040
fireEvent.click(confirmButton)
4141
}
4242

43+
const activateUser = async (setupActionSpies: () => void) => {
44+
// Get the first user in the table
45+
const users = await screen.findAllByText(/.*@coder.com/)
46+
const firstUserRow = users[2].closest("tr")
47+
if (!firstUserRow) {
48+
throw new Error("Error on get the first user row")
49+
}
50+
51+
// Click on the "more" button to display the "Activate" option
52+
const moreButton = within(firstUserRow).getByLabelText("more")
53+
fireEvent.click(moreButton)
54+
const menu = screen.getByRole("menu")
55+
const activateButton = within(menu).getByText(UsersTableLanguage.activateMenuItem)
56+
fireEvent.click(activateButton)
57+
58+
// Check if the confirm message is displayed
59+
const confirmDialog = screen.getByRole("dialog")
60+
expect(confirmDialog).toHaveTextContent(
61+
`${UsersPageLanguage.activateDialogMessagePrefix} ${SuspendedMockUser.username}?`,
62+
)
63+
64+
// Setup spies to check the actions after
65+
setupActionSpies()
66+
67+
// Click on the "Confirm" button
68+
const confirmButton = within(confirmDialog).getByText(UsersPageLanguage.activateDialogAction)
69+
fireEvent.click(confirmButton)
70+
}
71+
4372
const resetUserPassword = async (setupActionSpies: () => void) => {
4473
// Get the first user in the table
4574
const users = await screen.findAllByText(/.*@coder.com/)
@@ -99,7 +128,7 @@ describe("Users Page", () => {
99128
it("shows users", async () => {
100129
render(<UsersPage />)
101130
const users = await screen.findAllByText(/.*@coder.com/)
102-
expect(users.length).toEqual(2)
131+
expect(users.length).toEqual(3)
103132
})
104133

105134
it("shows 'Create user' button to an authorized user", () => {
@@ -178,6 +207,54 @@ describe("Users Page", () => {
178207
})
179208
})
180209

210+
describe("activate user", () => {
211+
describe("when user is successfully activated", () => {
212+
it("shows a success message and refreshes the page", async () => {
213+
render(
214+
<>
215+
<UsersPage />
216+
<GlobalSnackbar />
217+
</>,
218+
)
219+
220+
await activateUser(() => {
221+
jest.spyOn(API, "activateUser").mockResolvedValueOnce(SuspendedMockUser)
222+
jest
223+
.spyOn(API, "getUsers")
224+
.mockImplementationOnce(() => Promise.resolve([MockUser, MockUser2, SuspendedMockUser]))
225+
})
226+
227+
// Check if the success message is displayed
228+
await screen.findByText(usersXServiceLanguage.activateUserSuccess)
229+
230+
// Check if the API was called correctly
231+
expect(API.activateUser).toBeCalledTimes(1)
232+
expect(API.activateUser).toBeCalledWith(SuspendedMockUser.id)
233+
})
234+
})
235+
describe("when activation fails", () => {
236+
it("shows an error message", async () => {
237+
render(
238+
<>
239+
<UsersPage />
240+
<GlobalSnackbar />
241+
</>,
242+
)
243+
244+
await activateUser(() => {
245+
jest.spyOn(API, "activateUser").mockRejectedValueOnce({})
246+
})
247+
248+
// Check if the error message is displayed
249+
await screen.findByText(usersXServiceLanguage.activateUserError)
250+
251+
// Check if the API was called correctly
252+
expect(API.activateUser).toBeCalledTimes(1)
253+
expect(API.activateUser).toBeCalledWith(SuspendedMockUser.id)
254+
})
255+
})
256+
})
257+
181258
describe("reset user password", () => {
182259
describe("when it is success", () => {
183260
it("shows a success message", async () => {

site/src/testHelpers/entities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ export const MockUser2: TypesGen.User = {
5151
roles: [],
5252
}
5353

54+
export const SuspendedMockUser: TypesGen.User = {
55+
id: "suspended-mock-user",
56+
username: "SuspendedMockUser",
57+
58+
created_at: "",
59+
status: "suspended",
60+
organization_ids: ["fc0774ce-cc9e-48d4-80ae-88f7a4d4a8b0"],
61+
roles: [],
62+
}
63+
5464
export const MockOrganization: TypesGen.Organization = {
5565
id: "test-org",
5666
name: "Test Organization",

site/src/testHelpers/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const handlers = [
3737

3838
// users
3939
rest.get("/api/v2/users", async (req, res, ctx) => {
40-
return res(ctx.status(200), ctx.json([M.MockUser, M.MockUser2]))
40+
return res(ctx.status(200), ctx.json([M.MockUser, M.MockUser2, M.SuspendedMockUser]))
4141
}),
4242
rest.post("/api/v2/users", async (req, res, ctx) => {
4343
return res(ctx.status(200), ctx.json(M.MockUser))

0 commit comments

Comments
 (0)