Skip to content

Commit d04c6f2

Browse files
committed
TS tests
1 parent ec6e294 commit d04c6f2

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,32 @@ const activateUser = async (setupActionSpies: () => void) => {
118118
fireEvent.click(confirmButton)
119119
}
120120

121+
const markUserDormant = async (setupActionSpies: () => void) => {
122+
const moreButtons = await screen.findAllByLabelText("more")
123+
const markAsDormantMoreButton = moreButtons[1]
124+
fireEvent.click(markAsDormantMoreButton)
125+
126+
const menu = screen.getByRole("menu")
127+
const text = t("markUserDormantMenuItem", { ns: "usersPage" })
128+
const markAsDormant = within(menu).getByText(text)
129+
fireEvent.click(markAsDormant)
130+
131+
// Check if the confirm message is displayed
132+
const confirmDialog = screen.getByRole("dialog")
133+
expect(confirmDialog).toHaveTextContent(
134+
`Do you want to mark the user TestUser2 as dormant?`,
135+
)
136+
137+
// Setup spies to check the actions after
138+
setupActionSpies()
139+
140+
// Click on the "Confirm" button
141+
const confirmButton = within(confirmDialog).getByText(
142+
UsersPageLanguage.markUserDormantDialogAction,
143+
)
144+
fireEvent.click(confirmButton)
145+
}
146+
121147
const resetUserPassword = async (setupActionSpies: () => void) => {
122148
const moreButtons = await screen.findAllByLabelText("more")
123149
const firstMoreButton = moreButtons[0]
@@ -345,6 +371,50 @@ describe("UsersPage", () => {
345371
})
346372
})
347373

374+
describe("mark user as dormant", () => {
375+
describe("when user is successfully marked as dormant", () => {
376+
it("shows a success message and refreshes the page", async () => {
377+
renderPage()
378+
379+
await markUserDormant(() => {
380+
jest.spyOn(API, "markUserDormant").mockResolvedValueOnce({
381+
...MockUser2,
382+
status: "dormant",
383+
})
384+
jest.spyOn(API, "getUsers").mockImplementationOnce(() =>
385+
Promise.resolve({
386+
users: [MockUser, MockUser2],
387+
count: 2,
388+
}),
389+
)
390+
})
391+
392+
// Check if the success message is displayed
393+
await screen.findByText(usersXServiceLanguage.markUserDormantSuccess)
394+
395+
// Check if the API was called correctly
396+
expect(API.markUserDormant).toBeCalledTimes(1)
397+
expect(API.markUserDormant).toBeCalledWith(MockUser2.id)
398+
})
399+
})
400+
describe("when can't mark user as dormant", () => {
401+
it("shows an error message", async () => {
402+
renderPage()
403+
404+
await markUserDormant(() => {
405+
jest.spyOn(API, "markUserDormant").mockRejectedValueOnce({})
406+
})
407+
408+
// Check if the error message is displayed
409+
await screen.findByText(usersXServiceLanguage.markUserDormantError)
410+
411+
// Check if the API was called correctly
412+
expect(API.markUserDormant).toBeCalledTimes(1)
413+
expect(API.markUserDormant).toBeCalledWith(MockUser2.id)
414+
})
415+
})
416+
})
417+
348418
describe("reset user password", () => {
349419
describe("when it is success", () => {
350420
it("shows a success message", async () => {

site/src/testHelpers/entities.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,19 @@ export const SuspendedMockUser: TypesGen.User = {
314314
login_type: "password",
315315
}
316316

317+
export const DormantMockUser: TypesGen.User = {
318+
id: "dormant-mock-user",
319+
username: "DormantMockUser",
320+
email: "iamdormant@coder.com",
321+
created_at: "",
322+
status: "dormant",
323+
organization_ids: [MockOrganization.id],
324+
roles: [],
325+
avatar_url: "",
326+
last_seen_at: "",
327+
login_type: "password",
328+
}
329+
317330
export const MockProvisioner: TypesGen.ProvisionerDaemon = {
318331
created_at: "",
319332
id: "test-provisioner",

0 commit comments

Comments
 (0)