Skip to content

fix: update ErrorDialog logic and tests #10111

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 7 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions site/src/components/Dialogs/DeleteDialog/DeleteDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { render } from "testHelpers/renderHelpers";
import { DeleteDialog } from "./DeleteDialog";
import { act } from "react-dom/test-utils";

const inputTestId = "delete-dialog-name-confirmation";

async function fillInputField(inputElement: HTMLElement, text: string) {
// 2023-10-06 - There's something wonky with MUI's ConfirmDialog that causes
// its state to update after a typing event gets fired, and React Testing
// Library isn't able to catch it, making React DOM freak out because an
// "unexpected" state change happened. It won't fail the test, but it makes
// the console look really scary because it'll spit out a big warning message.
// Tried everything under the sun to catch the state changes the proper way,
// but the only way to get around it for now might be to manually make React
// DOM aware of the changes

// eslint-disable-next-line testing-library/no-unnecessary-act -- have to make sure state updates don't slip through cracks
return act(() => userEvent.type(inputElement, text));
Copy link
Member Author

@Parkreiner Parkreiner Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, this issue existed with the previous version of the component, too. I just don't know if this is a recent issue or not – the error message is really big and scary looking. Hard to miss, even with the tests still passing.

Going to see if it's worth opening an issue with MUI and/or RTL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the first reasonable use case for act I've seen in my career :)

}

describe("DeleteDialog", () => {
it("disables confirm button when the text field is empty", () => {
Expand All @@ -14,6 +31,7 @@ describe("DeleteDialog", () => {
name="MyTemplate"
/>,
);

const confirmButton = screen.getByRole("button", { name: "Delete" });
expect(confirmButton).toBeDisabled();
});
Expand All @@ -28,8 +46,10 @@ describe("DeleteDialog", () => {
name="MyTemplate"
/>,
);
const textField = screen.getByTestId("delete-dialog-name-confirmation");
await userEvent.type(textField, "MyTemplateWrong");

const textField = screen.getByTestId(inputTestId);
await fillInputField(textField, "MyTemplateButWrong");

const confirmButton = screen.getByRole("button", { name: "Delete" });
expect(confirmButton).toBeDisabled();
});
Expand All @@ -44,8 +64,10 @@ describe("DeleteDialog", () => {
name="MyTemplate"
/>,
);
const textField = screen.getByTestId("delete-dialog-name-confirmation");
await userEvent.type(textField, "MyTemplate");

const textField = screen.getByTestId(inputTestId);
await fillInputField(textField, "MyTemplate");

const confirmButton = screen.getByRole("button", { name: "Delete" });
expect(confirmButton).not.toBeDisabled();
});
Expand Down
124 changes: 67 additions & 57 deletions site/src/components/Dialogs/DeleteDialog/DeleteDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import makeStyles from "@mui/styles/makeStyles";
import {
type FC,
type FormEvent,
type PropsWithChildren,
useId,
useState,
} from "react";

import { useTheme } from "@emotion/react";
import TextField from "@mui/material/TextField";
import { ChangeEvent, useState, PropsWithChildren, FC } from "react";
import { ConfirmDialog } from "../ConfirmDialog/ConfirmDialog";

export interface DeleteDialogProps {
Expand All @@ -22,51 +29,23 @@ export const DeleteDialog: FC<PropsWithChildren<DeleteDialogProps>> = ({
name,
confirmLoading,
}) => {
const styles = useStyles();
const [nameValue, setNameValue] = useState("");
const confirmed = name === nameValue;
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
setNameValue(event.target.value);
};
const hasError = nameValue.length > 0 && !confirmed;
const hookId = useId();
const theme = useTheme();

const content = (
<>
<p>Deleting this {entity} is irreversible!</p>
{Boolean(info) && <p className={styles.warning}>{info}</p>}
<p>Are you sure you want to proceed?</p>
<p>
Type &ldquo;<strong>{name}</strong>&rdquo; below to confirm.
</p>
const [userConfirmationText, setUserConfirmationText] = useState("");
const [isFocused, setIsFocused] = useState(false);

<form
onSubmit={(e) => {
e.preventDefault();
if (confirmed) {
onConfirm();
}
}}
>
<TextField
fullWidth
autoFocus
className={styles.textField}
name="confirmation"
autoComplete="off"
id="confirmation"
placeholder={name}
value={nameValue}
onChange={handleChange}
label={`Name of the ${entity} to delete`}
error={hasError}
helperText={
hasError && `${nameValue} does not match the name of this ${entity}`
}
inputProps={{ ["data-testid"]: "delete-dialog-name-confirmation" }}
/>
</form>
</>
);
const deletionConfirmed = name === userConfirmationText;
const onSubmit = (event: FormEvent) => {
event.preventDefault();
if (deletionConfirmed) {
onConfirm();
}
};

const hasError = !deletionConfirmed && userConfirmationText.length > 0;
const displayErrorMessage = hasError && !isFocused;
const inputColor = hasError ? "error" : "primary";

return (
<ConfirmDialog
Expand All @@ -76,19 +55,50 @@ export const DeleteDialog: FC<PropsWithChildren<DeleteDialogProps>> = ({
title={`Delete ${entity}`}
onConfirm={onConfirm}
onClose={onCancel}
description={content}
confirmLoading={confirmLoading}
disabled={!confirmed}
disabled={!deletionConfirmed}
description={
<>
<p>Deleting this {entity} is irreversible!</p>

{Boolean(info) && (
<p css={{ color: theme.palette.warning.light }}>{info}</p>
)}

<p>Are you sure you want to proceed?</p>

<p>
Type &ldquo;<strong>{name}</strong>&rdquo; below to confirm.
</p>

<form onSubmit={onSubmit}>
<TextField
fullWidth
autoFocus
sx={{ marginTop: theme.spacing(3) }}
name="confirmation"
autoComplete="off"
id={`${hookId}-confirm`}
placeholder={name}
value={userConfirmationText}
onChange={(event) => setUserConfirmationText(event.target.value)}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
label={`Name of the ${entity} to delete`}
color={inputColor}
error={displayErrorMessage}
helperText={
displayErrorMessage &&
`${userConfirmationText} does not match the name of this ${entity}`
}
InputProps={{ color: inputColor }}
inputProps={{
"data-testid": "delete-dialog-name-confirmation",
}}
/>
</form>
</>
}
/>
);
};

const useStyles = makeStyles((theme) => ({
warning: {
color: theme.palette.warning.light,
},

textField: {
marginTop: theme.spacing(3),
},
}));