Skip to content

Commit

Permalink
[test][material-ui] Remove unnecessary async act calls (#42942)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongarciah authored Jul 18, 2024
1 parent 8f30a53 commit 4523ba2
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 180 deletions.
8 changes: 4 additions & 4 deletions packages/mui-joy/src/MenuItem/MenuItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Joy <MenuItem />', () => {
const handler = spy();
render(<MenuItem {...{ [handlerName]: handler }} />);

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

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

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

await act(async () => fireEvent.keyDown(menuitem));
fireEvent.keyDown(menuitem);

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

await act(async () => fireEvent.keyUp(menuitem));
fireEvent.keyUp(menuitem);

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

act(() => {
await act(async () => {
menuitem.blur();
});

Expand Down
102 changes: 53 additions & 49 deletions packages/mui-material/src/ButtonBase/ButtonBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,47 +202,47 @@ describe('<ButtonBase />', () => {
if (typeof Touch !== 'undefined') {
const touch = new Touch({ identifier: 0, target: button, clientX: 0, clientY: 0 });

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

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

if (canFireDragEvents) {
await act(async () => fireEvent.dragEnd(button));
fireEvent.dragEnd(button);
expect(onDragEnd.callCount).to.equal(1);
}

await act(async () => fireEvent.mouseDown(button));
fireEvent.mouseDown(button);
expect(onMouseDown.callCount).to.equal(1);

await act(async () => fireEvent.mouseUp(button));
fireEvent.mouseUp(button);
expect(onMouseUp.callCount).to.equal(1);

await act(async () => fireEvent.contextMenu(button));
fireEvent.contextMenu(button);
expect(onContextMenu.callCount).to.equal(1);

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

act(() => {
await act(async () => {
button.focus();
});
expect(onFocus.callCount).to.equal(1);

await act(async () => fireEvent.keyDown(button));
fireEvent.keyDown(button);
expect(onKeyDown.callCount).to.equal(1);

await act(async () => fireEvent.keyUp(button));
fireEvent.keyUp(button);
expect(onKeyUp.callCount).to.equal(1);

act(() => {
await act(async () => {
button.blur();
});
expect(onBlur.callCount).to.equal(1);

await act(async () => fireEvent.mouseLeave(button));
fireEvent.mouseLeave(button);
expect(onMouseLeave.callCount).to.equal(1);
});
});
Expand Down Expand Up @@ -359,7 +359,7 @@ describe('<ButtonBase />', () => {
const button = getByRole('button');
await ripple.startTouch(button);

act(() => button.blur());
await act(async () => button.blur());

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

describe('event: focus', () => {
it('when disabled should be called onFocus', () => {
it('when disabled should be called onFocus', async () => {
const onFocusSpy = spy();
const { getByRole } = render(
<ButtonBase component="div" disabled onFocus={onFocusSpy}>
Hello
</ButtonBase>,
);

act(() => {
await act(async () => {
getByRole('button').focus();
});

expect(onFocusSpy.callCount).to.equal(1);
});

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

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

act(() => {
await act(async () => {
button.focus();
});

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

await ripple.startFocus(button);

act(() => {
fireEvent.keyDown(button, { key: 'Enter' });
});
fireEvent.keyDown(button, { key: 'Enter' });

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

Expand Down Expand Up @@ -1007,7 +1005,7 @@ describe('<ButtonBase />', () => {
});

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

act(() => {
await act(async () => {
button.focus();
fireEvent.keyDown(button, {
key: ' ',
});
});

fireEvent.keyDown(button, {
key: ' ',
});

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

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

act(() => {
await act(async () => {
button.focus();
fireEvent.keyUp(button, {
key: ' ',
});
});

fireEvent.keyUp(button, {
key: ' ',
});

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

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

act(() => {
await act(async () => {
button.focus();
fireEvent.keyUp(button, {
key: ' ',
});
});

fireEvent.keyUp(button, {
key: ' ',
});

expect(onClickSpy.callCount).to.equal(0);
});

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

act(() => {
await act(async () => {
button.focus();
fireEvent.keyDown(button, {
key: 'Enter',
});
});

fireEvent.keyDown(button, {
key: 'Enter',
});

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

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

act(() => {
await act(async () => {
button.focus();
fireEvent.keyDown(button, { key: 'Enter' });
});

fireEvent.keyDown(button, { key: 'Enter' });

expect(onClickSpy.calledOnce).to.equal(true);
expect(onClickSpy.firstCall.args[0]).to.have.property('defaultPrevented', true);
});

it('should ignore anchors with href', () => {
it('should ignore anchors with href', async () => {
const onClick = spy();
const onKeyDown = spy();
const { getByText } = render(
Expand All @@ -1159,11 +1162,12 @@ describe('<ButtonBase />', () => {
);
const button = getByText('Hello');

act(() => {
await act(async () => {
button.focus();
fireEvent.keyDown(button, {
key: 'Enter',
});
});

fireEvent.keyDown(button, {
key: 'Enter',
});

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

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

act(() => {
await act(async () => {
// @ts-ignore
buttonActionsRef.current.focusVisible();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ describe('<ListItemButton />', () => {
}
});

it('should merge the class names', () => {
it('should merge the class names', async () => {
const { getByRole } = render(
<ListItemButton focusVisibleClassName="focusVisibleClassName" />,
);
const button = getByRole('button');

act(() => {
fireEvent.keyDown(document.activeElement || document.body, { key: 'Tab' });
fireEvent.keyDown(document.activeElement || document.body, { key: 'Tab' });

await act(async () => {
button.focus();
});

Expand Down
4 changes: 2 additions & 2 deletions packages/mui-material/src/MenuItem/MenuItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ describe('<MenuItem />', () => {

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

await act(async () => fireEvent.keyDown(menuitem));
fireEvent.keyDown(menuitem);

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

await act(async () => fireEvent.keyUp(menuitem));
fireEvent.keyUp(menuitem);

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

Expand Down
6 changes: 2 additions & 4 deletions packages/mui-material/src/Popper/Popper.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { expect } from 'chai';
import { act, createRenderer, fireEvent, screen } from '@mui/internal-test-utils';
import { createRenderer, fireEvent, screen } from '@mui/internal-test-utils';
import { ThemeProvider } from '@mui/system';
import createTheme from '@mui/system/createTheme';
import Grow from '@mui/material/Grow';
Expand Down Expand Up @@ -114,9 +114,7 @@ describe('<Popper />', () => {
);
expect(screen.getByTestId('placement')).to.have.text('bottom');

await act(async () => {
await popperRef.current.setOptions({ placement: 'top' });
});
await popperRef.current.setOptions({ placement: 'top' });

expect(screen.getByTestId('placement')).to.have.text('bottom');
});
Expand Down
Loading

0 comments on commit 4523ba2

Please sign in to comment.