Skip to content

feat: have user type name of thing to delete for extra safety #4080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 20, 2022
Prev Previous commit
Next Next commit
Fix the worst of the UsersPage test bugs
  • Loading branch information
presleyp committed Sep 15, 2022
commit 06e6e658015e693ec1e348faddff3b5f33c6cb07
48 changes: 27 additions & 21 deletions site/src/pages/UsersPage/UsersPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ const { t } = i18n

const suspendUser = async (setupActionSpies: () => void) => {
// Get the first user in the table
const users = await screen.findAllByText(/.*@coder.com/)
const firstUserRow = users[0].closest("tr")
if (!firstUserRow) {
throw new Error("Error on get the first user row")
}
waitFor(async () => {
const users = await screen.findAllByText(/.*@coder.com/)
const firstUserRow = users[0].closest("tr")

// Click on the "more" button to display the "Suspend" option
const moreButton = within(firstUserRow).getByLabelText("more")
if (!firstUserRow) {
throw new Error("Error on get the first user row")
}
// Click on the "more" button to display the "Suspend" option
const moreButton = within(firstUserRow).getByLabelText("more")

fireEvent.click(moreButton)
fireEvent.click(moreButton)
})

const menu = await screen.findByRole("menu")
const suspendButton = within(menu).getByText(UsersTableBodyLanguage.suspendMenuItem)
Expand All @@ -58,21 +60,23 @@ const suspendUser = async (setupActionSpies: () => void) => {

const deleteUser = async (setupActionSpies: () => void) => {
// Get the first user in the table
const users = await screen.findAllByText(/.*@coder.com/)
const firstUserRow = users[0].closest("tr")
if (!firstUserRow) {
throw new Error("Error on get the first user row")
}
waitFor(async () => {
const users = await screen.findAllByText(/.*@coder.com/)
const firstUserRow = users[0].closest("tr")
if (!firstUserRow) {
throw new Error("Error on get the first user row")
}

// Click on the "more" button to display the "Suspend" option
const moreButton = within(firstUserRow).getByLabelText("more")
// Click on the "more" button to display the "Delete" option
const moreButton = within(firstUserRow).getByLabelText("more")

fireEvent.click(moreButton)
fireEvent.click(moreButton)
})

const menu = await screen.findByRole("menu")
const suspendButton = within(menu).getByText(UsersTableBodyLanguage.deleteMenuItem)
const deleteButton = within(menu).getByText(UsersTableBodyLanguage.deleteMenuItem)

fireEvent.click(suspendButton)
fireEvent.click(deleteButton)

// Check if the confirm message is displayed
const confirmDialog = await screen.findByRole("dialog")
Expand All @@ -83,7 +87,9 @@ const deleteUser = async (setupActionSpies: () => void) => {
// Confirm with text input
const labelText = t("deleteDialog.confirmLabel", { ns: "common", entity: "user" })
const textField = screen.getByLabelText(labelText)
await userEvent.type(textField, MockUser.username)
waitFor(async () => {
await userEvent.type(textField, MockUser.username)
})

// Setup spies to check the actions after
setupActionSpies()
Expand Down Expand Up @@ -284,7 +290,7 @@ describe("UsersPage", () => {
jest.spyOn(API, "deleteUser").mockResolvedValueOnce(undefined)
jest
.spyOn(API, "getUsers")
.mockImplementationOnce(() => Promise.resolve([MockUser, MockUser2]))
.mockImplementationOnce(() => Promise.resolve([MockUser2]))
})

// Check if the success message is displayed
Expand All @@ -294,7 +300,7 @@ describe("UsersPage", () => {
expect(API.deleteUser).toBeCalledTimes(1)
expect(API.deleteUser).toBeCalledWith(MockUser.id)

// Check if the users list was reload
// Check if the users list was reloaded
await waitFor(() => expect(API.getUsers).toBeCalledTimes(1))
})
})
Expand Down