@@ -2,6 +2,21 @@ import { screen } from "@testing-library/react";
2
2
import userEvent from "@testing-library/user-event" ;
3
3
import { render } from "testHelpers/renderHelpers" ;
4
4
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
+ }
5
20
6
21
describe ( "DeleteDialog" , ( ) => {
7
22
it ( "disables confirm button when the text field is empty" , ( ) => {
@@ -14,6 +29,7 @@ describe("DeleteDialog", () => {
14
29
name = "MyTemplate"
15
30
/> ,
16
31
) ;
32
+
17
33
const confirmButton = screen . getByRole ( "button" , { name : "Delete" } ) ;
18
34
expect ( confirmButton ) . toBeDisabled ( ) ;
19
35
} ) ;
@@ -28,8 +44,10 @@ describe("DeleteDialog", () => {
28
44
name = "MyTemplate"
29
45
/> ,
30
46
) ;
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
+
33
51
const confirmButton = screen . getByRole ( "button" , { name : "Delete" } ) ;
34
52
expect ( confirmButton ) . toBeDisabled ( ) ;
35
53
} ) ;
@@ -44,8 +62,10 @@ describe("DeleteDialog", () => {
44
62
name = "MyTemplate"
45
63
/> ,
46
64
) ;
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
+
49
69
const confirmButton = screen . getByRole ( "button" , { name : "Delete" } ) ;
50
70
expect ( confirmButton ) . not . toBeDisabled ( ) ;
51
71
} ) ;
0 commit comments