Skip to content
Prev Previous commit
Next Next commit
Add delete user story
  • Loading branch information
BrunoQuaresma committed Sep 5, 2024
commit a1bb0ddeef10e4418f67d59524f5389a316241bf
54 changes: 54 additions & 0 deletions site/src/pages/UsersPage/UsersPage.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ export const SuspendUserError: Story = {
},
};

export const DeleteUserSuccess: Story = {
play: async ({ canvasElement }) => {
const user = userEvent.setup();
const firstUserRow = canvasElement.querySelector<HTMLElement>("tbody tr");
if (!firstUserRow) {
throw new Error("No user row found");
}
spyOn(API, "deleteUser").mockResolvedValue();

await deleteUser(user, firstUserRow);

await within(document.body).findByText("Successfully deleted the user.");
},
};

export const DeleteUserError: Story = {
play: async ({ canvasElement }) => {
const user = userEvent.setup();
const firstUserRow = canvasElement.querySelector<HTMLElement>("tbody tr");
if (!firstUserRow) {
throw new Error("No user row found");
}
spyOn(API, "deleteUser").mockRejectedValue({});

await deleteUser(user, firstUserRow);

await within(document.body).findByText("Error deleting user.");
},
};

async function suspendUser(
user: ReturnType<typeof userEvent.setup>,
userRow: HTMLElement,
Expand All @@ -128,3 +158,27 @@ async function suspendUser(
const dialog = await within(document.body).findByRole("dialog");
await user.click(within(dialog).getByRole("button", { name: "Suspend" }));
}

async function deleteUser(
user: ReturnType<typeof userEvent.setup>,
userRow: HTMLElement,
) {
// Open "More options" menu
const moreOptionsButton = within(userRow).getByLabelText("More options");
await user.click(moreOptionsButton);

// Click on "Suspend..."
const deleteButton = await within(userRow).findByText("Delete", {
exact: false,
});
await user.click(deleteButton);

// Wait for the dialog
const dialog = await within(document.body).findByRole("dialog");

// Confirm the deletion by typing the user name and clicking on "Delete"
// button in the dialog
const input = within(dialog).getByLabelText("Name of the user to delete");
await user.type(input, MockUsers[0].username);
await user.click(within(dialog).getByRole("button", { name: "Delete" }));
}