Skip to content

Commit 4523ba2

Browse files
authored
[test][material-ui] Remove unnecessary async act calls (#42942)
1 parent 8f30a53 commit 4523ba2

File tree

10 files changed

+171
-180
lines changed

10 files changed

+171
-180
lines changed

packages/mui-joy/src/MenuItem/MenuItem.test.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('Joy <MenuItem />', () => {
9797
const handler = spy();
9898
render(<MenuItem {...{ [handlerName]: handler }} />);
9999

100-
await act(async () => fireEvent[eventName](screen.getByRole('menuitem')));
100+
fireEvent[eventName](screen.getByRole('menuitem'));
101101

102102
expect(handler.callCount).to.equal(1);
103103
});
@@ -125,15 +125,15 @@ describe('Joy <MenuItem />', () => {
125125

126126
expect(handleFocus.callCount).to.equal(1);
127127

128-
await act(async () => fireEvent.keyDown(menuitem));
128+
fireEvent.keyDown(menuitem);
129129

130130
expect(handleKeyDown.callCount).to.equal(1);
131131

132-
await act(async () => fireEvent.keyUp(menuitem));
132+
fireEvent.keyUp(menuitem);
133133

134134
expect(handleKeyUp.callCount).to.equal(1);
135135

136-
act(() => {
136+
await act(async () => {
137137
menuitem.blur();
138138
});
139139

packages/mui-material/src/ButtonBase/ButtonBase.test.js

+53-49
Original file line numberDiff line numberDiff line change
@@ -202,47 +202,47 @@ describe('<ButtonBase />', () => {
202202
if (typeof Touch !== 'undefined') {
203203
const touch = new Touch({ identifier: 0, target: button, clientX: 0, clientY: 0 });
204204

205-
await act(async () => fireEvent.touchStart(button, { touches: [touch] }));
205+
fireEvent.touchStart(button, { touches: [touch] });
206206
expect(onTouchStart.callCount).to.equal(1);
207207

208-
await act(async () => fireEvent.touchEnd(button, { touches: [touch] }));
208+
fireEvent.touchEnd(button, { touches: [touch] });
209209
expect(onTouchEnd.callCount).to.equal(1);
210210
}
211211

212212
if (canFireDragEvents) {
213-
await act(async () => fireEvent.dragEnd(button));
213+
fireEvent.dragEnd(button);
214214
expect(onDragEnd.callCount).to.equal(1);
215215
}
216216

217-
await act(async () => fireEvent.mouseDown(button));
217+
fireEvent.mouseDown(button);
218218
expect(onMouseDown.callCount).to.equal(1);
219219

220-
await act(async () => fireEvent.mouseUp(button));
220+
fireEvent.mouseUp(button);
221221
expect(onMouseUp.callCount).to.equal(1);
222222

223-
await act(async () => fireEvent.contextMenu(button));
223+
fireEvent.contextMenu(button);
224224
expect(onContextMenu.callCount).to.equal(1);
225225

226226
await user.click(button);
227227
expect(onClick.callCount).to.equal(1);
228228

229-
act(() => {
229+
await act(async () => {
230230
button.focus();
231231
});
232232
expect(onFocus.callCount).to.equal(1);
233233

234-
await act(async () => fireEvent.keyDown(button));
234+
fireEvent.keyDown(button);
235235
expect(onKeyDown.callCount).to.equal(1);
236236

237-
await act(async () => fireEvent.keyUp(button));
237+
fireEvent.keyUp(button);
238238
expect(onKeyUp.callCount).to.equal(1);
239239

240-
act(() => {
240+
await act(async () => {
241241
button.blur();
242242
});
243243
expect(onBlur.callCount).to.equal(1);
244244

245-
await act(async () => fireEvent.mouseLeave(button));
245+
fireEvent.mouseLeave(button);
246246
expect(onMouseLeave.callCount).to.equal(1);
247247
});
248248
});
@@ -359,7 +359,7 @@ describe('<ButtonBase />', () => {
359359
const button = getByRole('button');
360360
await ripple.startTouch(button);
361361

362-
act(() => button.blur());
362+
await act(async () => button.blur());
363363

364364
expect(button.querySelectorAll('.ripple-visible .child-leaving')).to.have.lengthOf(0);
365365
expect(
@@ -802,22 +802,22 @@ describe('<ButtonBase />', () => {
802802
});
803803

804804
describe('event: focus', () => {
805-
it('when disabled should be called onFocus', () => {
805+
it('when disabled should be called onFocus', async () => {
806806
const onFocusSpy = spy();
807807
const { getByRole } = render(
808808
<ButtonBase component="div" disabled onFocus={onFocusSpy}>
809809
Hello
810810
</ButtonBase>,
811811
);
812812

813-
act(() => {
813+
await act(async () => {
814814
getByRole('button').focus();
815815
});
816816

817817
expect(onFocusSpy.callCount).to.equal(1);
818818
});
819819

820-
it('has a focus-visible polyfill', function test() {
820+
it('has a focus-visible polyfill', async function test() {
821821
if (/jsdom/.test(window.navigator.userAgent)) {
822822
// JSDOM doesn't support :focus-visible
823823
this.skip();
@@ -829,7 +829,7 @@ describe('<ButtonBase />', () => {
829829

830830
expect(button).not.to.have.class(classes.focusVisible);
831831

832-
act(() => {
832+
await act(async () => {
833833
button.focus();
834834
});
835835

@@ -942,9 +942,7 @@ describe('<ButtonBase />', () => {
942942

943943
await ripple.startFocus(button);
944944

945-
act(() => {
946-
fireEvent.keyDown(button, { key: 'Enter' });
947-
});
945+
fireEvent.keyDown(button, { key: 'Enter' });
948946

949947
expect(container.querySelectorAll('.ripple-visible')).to.have.lengthOf(1);
950948

@@ -1007,7 +1005,7 @@ describe('<ButtonBase />', () => {
10071005
});
10081006

10091007
describe('keyboard accessibility for non interactive elements', () => {
1010-
it('does not call onClick when a spacebar is pressed on the element but prevents the default', () => {
1008+
it('does not call onClick when a spacebar is pressed on the element but prevents the default', async () => {
10111009
const onKeyDown = spy();
10121010
const onClickSpy = spy();
10131011
const { getByRole } = render(
@@ -1017,19 +1015,20 @@ describe('<ButtonBase />', () => {
10171015
);
10181016
const button = getByRole('button');
10191017

1020-
act(() => {
1018+
await act(async () => {
10211019
button.focus();
1022-
fireEvent.keyDown(button, {
1023-
key: ' ',
1024-
});
1020+
});
1021+
1022+
fireEvent.keyDown(button, {
1023+
key: ' ',
10251024
});
10261025

10271026
expect(onClickSpy.callCount).to.equal(0);
10281027
expect(onKeyDown.callCount).to.equal(1);
10291028
expect(onKeyDown.firstCall.args[0]).to.have.property('defaultPrevented', true);
10301029
});
10311030

1032-
it('does call onClick when a spacebar is released on the element', () => {
1031+
it('does call onClick when a spacebar is released on the element', async () => {
10331032
const onClickSpy = spy();
10341033
const { getByRole } = render(
10351034
<ButtonBase onClick={onClickSpy} component="div">
@@ -1038,18 +1037,19 @@ describe('<ButtonBase />', () => {
10381037
);
10391038
const button = getByRole('button');
10401039

1041-
act(() => {
1040+
await act(async () => {
10421041
button.focus();
1043-
fireEvent.keyUp(button, {
1044-
key: ' ',
1045-
});
1042+
});
1043+
1044+
fireEvent.keyUp(button, {
1045+
key: ' ',
10461046
});
10471047

10481048
expect(onClickSpy.callCount).to.equal(1);
10491049
expect(onClickSpy.firstCall.args[0]).to.have.property('defaultPrevented', false);
10501050
});
10511051

1052-
it('does not call onClick when a spacebar is released and the default is prevented', () => {
1052+
it('does not call onClick when a spacebar is released and the default is prevented', async () => {
10531053
const onClickSpy = spy();
10541054
const { getByRole } = render(
10551055
<ButtonBase
@@ -1067,17 +1067,18 @@ describe('<ButtonBase />', () => {
10671067
);
10681068
const button = getByRole('button');
10691069

1070-
act(() => {
1070+
await act(async () => {
10711071
button.focus();
1072-
fireEvent.keyUp(button, {
1073-
key: ' ',
1074-
});
1072+
});
1073+
1074+
fireEvent.keyUp(button, {
1075+
key: ' ',
10751076
});
10761077

10771078
expect(onClickSpy.callCount).to.equal(0);
10781079
});
10791080

1080-
it('calls onClick when Enter is pressed on the element', () => {
1081+
it('calls onClick when Enter is pressed on the element', async () => {
10811082
const onClickSpy = spy();
10821083
const { getByRole } = render(
10831084
<ButtonBase onClick={onClickSpy} component="div">
@@ -1086,11 +1087,12 @@ describe('<ButtonBase />', () => {
10861087
);
10871088
const button = getByRole('button');
10881089

1089-
act(() => {
1090+
await act(async () => {
10901091
button.focus();
1091-
fireEvent.keyDown(button, {
1092-
key: 'Enter',
1093-
});
1092+
});
1093+
1094+
fireEvent.keyDown(button, {
1095+
key: 'Enter',
10941096
});
10951097

10961098
expect(onClickSpy.calledOnce).to.equal(true);
@@ -1131,7 +1133,7 @@ describe('<ButtonBase />', () => {
11311133
expect(onClickSpy.callCount).to.equal(0);
11321134
});
11331135

1134-
it('prevents default with an anchor and empty href', () => {
1136+
it('prevents default with an anchor and empty href', async () => {
11351137
const onClickSpy = spy();
11361138
const { getByRole } = render(
11371139
<ButtonBase component="a" onClick={onClickSpy}>
@@ -1140,16 +1142,17 @@ describe('<ButtonBase />', () => {
11401142
);
11411143
const button = getByRole('button');
11421144

1143-
act(() => {
1145+
await act(async () => {
11441146
button.focus();
1145-
fireEvent.keyDown(button, { key: 'Enter' });
11461147
});
11471148

1149+
fireEvent.keyDown(button, { key: 'Enter' });
1150+
11481151
expect(onClickSpy.calledOnce).to.equal(true);
11491152
expect(onClickSpy.firstCall.args[0]).to.have.property('defaultPrevented', true);
11501153
});
11511154

1152-
it('should ignore anchors with href', () => {
1155+
it('should ignore anchors with href', async () => {
11531156
const onClick = spy();
11541157
const onKeyDown = spy();
11551158
const { getByText } = render(
@@ -1159,11 +1162,12 @@ describe('<ButtonBase />', () => {
11591162
);
11601163
const button = getByText('Hello');
11611164

1162-
act(() => {
1165+
await act(async () => {
11631166
button.focus();
1164-
fireEvent.keyDown(button, {
1165-
key: 'Enter',
1166-
});
1167+
});
1168+
1169+
fireEvent.keyDown(button, {
1170+
key: 'Enter',
11671171
});
11681172

11691173
expect(onClick.callCount).to.equal(0);
@@ -1181,7 +1185,7 @@ describe('<ButtonBase />', () => {
11811185
}
11821186
});
11831187

1184-
it('should be able to focus visible the button', () => {
1188+
it('should be able to focus visible the button', async () => {
11851189
/**
11861190
* @type {React.RefObject<import('./ButtonBase').ButtonBaseActions>}
11871191
*/
@@ -1195,7 +1199,7 @@ describe('<ButtonBase />', () => {
11951199
// @ts-ignore
11961200
expect(typeof buttonActionsRef.current.focusVisible).to.equal('function');
11971201

1198-
act(() => {
1202+
await act(async () => {
11991203
// @ts-ignore
12001204
buttonActionsRef.current.focusVisible();
12011205
});

packages/mui-material/src/ListItemButton/ListItemButton.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ describe('<ListItemButton />', () => {
6262
}
6363
});
6464

65-
it('should merge the class names', () => {
65+
it('should merge the class names', async () => {
6666
const { getByRole } = render(
6767
<ListItemButton focusVisibleClassName="focusVisibleClassName" />,
6868
);
6969
const button = getByRole('button');
7070

71-
act(() => {
72-
fireEvent.keyDown(document.activeElement || document.body, { key: 'Tab' });
71+
fireEvent.keyDown(document.activeElement || document.body, { key: 'Tab' });
72+
73+
await act(async () => {
7374
button.focus();
7475
});
7576

packages/mui-material/src/MenuItem/MenuItem.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ describe('<MenuItem />', () => {
9191

9292
expect(handleFocus.callCount).to.equal(1);
9393

94-
await act(async () => fireEvent.keyDown(menuitem));
94+
fireEvent.keyDown(menuitem);
9595

9696
expect(handleKeyDown.callCount).to.equal(1);
9797

98-
await act(async () => fireEvent.keyUp(menuitem));
98+
fireEvent.keyUp(menuitem);
9999

100100
expect(handleKeyUp.callCount).to.equal(1);
101101

packages/mui-material/src/Popper/Popper.test.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { expect } from 'chai';
3-
import { act, createRenderer, fireEvent, screen } from '@mui/internal-test-utils';
3+
import { createRenderer, fireEvent, screen } from '@mui/internal-test-utils';
44
import { ThemeProvider } from '@mui/system';
55
import createTheme from '@mui/system/createTheme';
66
import Grow from '@mui/material/Grow';
@@ -114,9 +114,7 @@ describe('<Popper />', () => {
114114
);
115115
expect(screen.getByTestId('placement')).to.have.text('bottom');
116116

117-
await act(async () => {
118-
await popperRef.current.setOptions({ placement: 'top' });
119-
});
117+
await popperRef.current.setOptions({ placement: 'top' });
120118

121119
expect(screen.getByTestId('placement')).to.have.text('bottom');
122120
});

0 commit comments

Comments
 (0)