Skip to content

Commit 1060cc0

Browse files
committed
fix: remove flaky warning messages in test
1 parent f6cee9a commit 1060cc0

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

site/src/components/Dialogs/DeleteDialog/DeleteDialog.test.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ import { screen } from "@testing-library/react";
22
import userEvent from "@testing-library/user-event";
33
import { render } from "testHelpers/renderHelpers";
44
import { DeleteDialog } from "./DeleteDialog";
5+
import { act } from "react-dom/test-utils";
6+
7+
const inputTestId = "delete-dialog-name-confirmation";
8+
9+
async function fillInputField(inputElement: HTMLElement, text: string) {
10+
// 2023-10-06 - There's something wonky with MUI's ConfirmDialog that causes
11+
// its state to update after a typing event being fired, and React Testing
12+
// Library isn't able to catch it, making React DOM freak out because an
13+
// "unexpected" state change happened. Tried everything under the sun to catch
14+
// the state changes the proper way, but the only way to get around it for now
15+
// might be to manually make React aware of the changes
16+
17+
// eslint-disable-next-line testing-library/no-unnecessary-act -- have to make sure state updates don't slip through cracks
18+
return act(() => userEvent.type(inputElement, text));
19+
}
520

621
describe("DeleteDialog", () => {
722
it("disables confirm button when the text field is empty", () => {
@@ -14,6 +29,7 @@ describe("DeleteDialog", () => {
1429
name="MyTemplate"
1530
/>,
1631
);
32+
1733
const confirmButton = screen.getByRole("button", { name: "Delete" });
1834
expect(confirmButton).toBeDisabled();
1935
});
@@ -28,8 +44,10 @@ describe("DeleteDialog", () => {
2844
name="MyTemplate"
2945
/>,
3046
);
31-
const textField = screen.getByTestId("delete-dialog-name-confirmation");
32-
await userEvent.type(textField, "MyTemplateWrong");
47+
48+
const textField = screen.getByTestId(inputTestId);
49+
await fillInputField(textField, "MyTemplateButWrong");
50+
3351
const confirmButton = screen.getByRole("button", { name: "Delete" });
3452
expect(confirmButton).toBeDisabled();
3553
});
@@ -44,8 +62,10 @@ describe("DeleteDialog", () => {
4462
name="MyTemplate"
4563
/>,
4664
);
47-
const textField = screen.getByTestId("delete-dialog-name-confirmation");
48-
await userEvent.type(textField, "MyTemplate");
65+
66+
const textField = screen.getByTestId(inputTestId);
67+
await fillInputField(textField, "MyTemplate");
68+
4969
const confirmButton = screen.getByRole("button", { name: "Delete" });
5070
expect(confirmButton).not.toBeDisabled();
5171
});

0 commit comments

Comments
 (0)