diff --git a/packages/mui-joy/src/MenuItem/MenuItem.test.tsx b/packages/mui-joy/src/MenuItem/MenuItem.test.tsx index a81d2248c07f35..e84712d09d1c52 100644 --- a/packages/mui-joy/src/MenuItem/MenuItem.test.tsx +++ b/packages/mui-joy/src/MenuItem/MenuItem.test.tsx @@ -97,7 +97,7 @@ describe('Joy ', () => { const handler = spy(); render(); - await act(async () => fireEvent[eventName](screen.getByRole('menuitem'))); + fireEvent[eventName](screen.getByRole('menuitem')); expect(handler.callCount).to.equal(1); }); @@ -125,15 +125,15 @@ describe('Joy ', () => { 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(); }); diff --git a/packages/mui-material/src/ButtonBase/ButtonBase.test.js b/packages/mui-material/src/ButtonBase/ButtonBase.test.js index 21d28575b8dc98..569b3d9b4c488e 100644 --- a/packages/mui-material/src/ButtonBase/ButtonBase.test.js +++ b/packages/mui-material/src/ButtonBase/ButtonBase.test.js @@ -202,47 +202,47 @@ describe('', () => { 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); }); }); @@ -359,7 +359,7 @@ describe('', () => { 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( @@ -802,7 +802,7 @@ describe('', () => { }); describe('event: focus', () => { - it('when disabled should be called onFocus', () => { + it('when disabled should be called onFocus', async () => { const onFocusSpy = spy(); const { getByRole } = render( @@ -810,14 +810,14 @@ describe('', () => { , ); - 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(); @@ -829,7 +829,7 @@ describe('', () => { expect(button).not.to.have.class(classes.focusVisible); - act(() => { + await act(async () => { button.focus(); }); @@ -942,9 +942,7 @@ describe('', () => { await ripple.startFocus(button); - act(() => { - fireEvent.keyDown(button, { key: 'Enter' }); - }); + fireEvent.keyDown(button, { key: 'Enter' }); expect(container.querySelectorAll('.ripple-visible')).to.have.lengthOf(1); @@ -1007,7 +1005,7 @@ describe('', () => { }); 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( @@ -1017,11 +1015,12 @@ describe('', () => { ); const button = getByRole('button'); - act(() => { + await act(async () => { button.focus(); - fireEvent.keyDown(button, { - key: ' ', - }); + }); + + fireEvent.keyDown(button, { + key: ' ', }); expect(onClickSpy.callCount).to.equal(0); @@ -1029,7 +1028,7 @@ describe('', () => { 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( @@ -1038,18 +1037,19 @@ describe('', () => { ); 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( ', () => { ); 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( @@ -1086,11 +1087,12 @@ describe('', () => { ); 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); @@ -1131,7 +1133,7 @@ describe('', () => { 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( @@ -1140,16 +1142,17 @@ describe('', () => { ); 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( @@ -1159,11 +1162,12 @@ describe('', () => { ); 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); @@ -1181,7 +1185,7 @@ describe('', () => { } }); - it('should be able to focus visible the button', () => { + it('should be able to focus visible the button', async () => { /** * @type {React.RefObject} */ @@ -1195,7 +1199,7 @@ describe('', () => { // @ts-ignore expect(typeof buttonActionsRef.current.focusVisible).to.equal('function'); - act(() => { + await act(async () => { // @ts-ignore buttonActionsRef.current.focusVisible(); }); diff --git a/packages/mui-material/src/ListItemButton/ListItemButton.test.js b/packages/mui-material/src/ListItemButton/ListItemButton.test.js index 1a58df4cfc118c..d1ddb64aa6bc21 100644 --- a/packages/mui-material/src/ListItemButton/ListItemButton.test.js +++ b/packages/mui-material/src/ListItemButton/ListItemButton.test.js @@ -62,14 +62,15 @@ describe('', () => { } }); - it('should merge the class names', () => { + it('should merge the class names', async () => { const { getByRole } = render( , ); 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(); }); diff --git a/packages/mui-material/src/MenuItem/MenuItem.test.js b/packages/mui-material/src/MenuItem/MenuItem.test.js index 234fc6db0c36e8..145338a1add652 100644 --- a/packages/mui-material/src/MenuItem/MenuItem.test.js +++ b/packages/mui-material/src/MenuItem/MenuItem.test.js @@ -91,11 +91,11 @@ describe('', () => { 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); diff --git a/packages/mui-material/src/Popper/Popper.test.js b/packages/mui-material/src/Popper/Popper.test.js index 9a61fe3130fe2b..326b95b2df39ec 100644 --- a/packages/mui-material/src/Popper/Popper.test.js +++ b/packages/mui-material/src/Popper/Popper.test.js @@ -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'; @@ -114,9 +114,7 @@ describe('', () => { ); 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'); }); diff --git a/packages/mui-material/src/Select/Select.test.js b/packages/mui-material/src/Select/Select.test.js index 86ffb06d56bd7c..d425e6e82b40c0 100644 --- a/packages/mui-material/src/Select/Select.test.js +++ b/packages/mui-material/src/Select/Select.test.js @@ -116,14 +116,15 @@ describe('', () => { }); [' ', 'ArrowUp', 'ArrowDown', 'Enter'].forEach((key) => { - it(`should open menu when pressed ${key} key on select`, () => { + it(`should open menu when pressed ${key} key on select`, async () => { render( , ); const trigger = screen.getByRole('combobox'); - act(() => { + await act(async () => { trigger.focus(); }); @@ -164,7 +165,7 @@ describe(' @@ -172,11 +173,8 @@ describe(', ); const button = getByRole('combobox'); - act(() => { + await act(async () => { button.focus(); - }); - - act(() => { button.blur(); }); @@ -184,7 +182,7 @@ describe('', () => { , ); - act(() => { + await act(async () => { getByTestId('backdrop').click(); }); @@ -243,7 +241,7 @@ describe(' @@ -253,7 +251,7 @@ describe(', ); fireEvent.mouseDown(getByRole('combobox')); - act(() => { + await act(async () => { getAllByRole('option')[1].click(); }); @@ -262,7 +260,7 @@ describe('', () => { ); fireEvent.mouseDown(getByRole('combobox')); - act(() => { + await act(async () => { getAllByRole('option')[1].click(); }); expect(eventLog).to.deep.equal(['CHANGE_EVENT', 'CLOSE_EVENT']); }); - it('should not be called if selected element has the current value (value did not change)', () => { + it('should not be called if selected element has the current value (value did not change)', async () => { const onChangeHandler = spy(); const { getAllByRole, getByRole } = render( ', () => { , ); fireEvent.mouseDown(getByRole('combobox')); - act(() => { + await act(async () => { getAllByRole('option')[1].click(); }); @@ -479,10 +477,10 @@ describe('); - act(() => { + await act(async () => { getByRole('listbox').focus(); }); @@ -529,11 +527,9 @@ describe('', () => { const options = getAllByRole('option'); expect(options[2]).to.have.attribute('tabindex', '0'); - act(() => { - fireEvent.keyDown(options[2], { key: 'ArrowDown' }); - fireEvent.keyDown(options[3], { key: 'ArrowDown' }); - fireEvent.keyDown(options[5], { key: 'Enter' }); - }); + fireEvent.keyDown(options[2], { key: 'ArrowDown' }); + fireEvent.keyDown(options[3], { key: 'ArrowDown' }); + fireEvent.keyDown(options[5], { key: 'Enter' }); expect(options[5]).to.have.attribute('aria-selected', 'true'); }); @@ -582,11 +576,9 @@ describe('', () => { const options = getAllByRole('option'); expect(options[1]).to.have.attribute('tabindex', '0'); - act(() => { - fireEvent.keyDown(options[1], { key: 'ArrowDown' }); - fireEvent.keyDown(options[2], { key: 'ArrowDown' }); - fireEvent.keyDown(options[4], { key: 'Enter' }); - }); + fireEvent.keyDown(options[1], { key: 'ArrowDown' }); + fireEvent.keyDown(options[2], { key: 'ArrowDown' }); + fireEvent.keyDown(options[4], { key: 'Enter' }); expect(options[4]).to.have.attribute('aria-selected', 'true'); }); @@ -690,11 +680,9 @@ describe('', () => { }); describe('prop: readOnly', () => { - it('should not trigger any event with readOnly', () => { + it('should not trigger any event with readOnly', async () => { render( ', () => { , ); const trigger = screen.getByRole('combobox'); - act(() => { + await act(async () => { trigger.focus(); }); @@ -940,7 +928,7 @@ describe('', () => { } const { container, getByRole } = render(); const openSelect = container.querySelector('#open-select'); - act(() => { + await act(async () => { openSelect.focus(); }); fireEvent.click(openSelect); @@ -975,7 +963,7 @@ describe('', () => { fireEvent.mouseDown(getByRole('combobox')); expect(getByRole('listbox')).not.to.equal(null); - act(() => { + await act(async () => { getByRole('option').click(); }); // react-transition-group uses one extra commit for exit to completely remove @@ -1199,7 +1187,7 @@ describe('', () => { expect(onChange.callCount).to.equal(1); expect(onChange.firstCall.returnValue).to.deep.equal({ name: 'age', value: [30] }); - act(() => { + await act(async () => { options[0].click(); }); @@ -1332,11 +1320,11 @@ describe(' is still used. - it('should be able focus the trigger imperatively', () => { + it('should be able focus the trigger imperatively', async () => { const ref = React.createRef(); const { getByRole } = render(', () => { }); describe('form submission', () => { - it('includes Select value in formData only if the `name` attribute is provided', function test() { + it('includes Select value in formData only if the `name` attribute is provided', async function test() { if (/jsdom/.test(window.navigator.userAgent)) { // FormData is not available in JSDOM this.skip(); @@ -1602,7 +1590,7 @@ describe('