diff --git a/.eslintrc.js b/.eslintrc.js
index 7c853bec85784d..08aa7d7d987f86 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -276,6 +276,21 @@ module.exports = {
'react/no-unused-prop-types': 'off',
},
},
+ {
+ files: [
+ // matching the pattern of the test runner
+ '*.test.mjs',
+ '*.test.js',
+ '*.test.ts',
+ '*.test.tsx',
+ ],
+ excludedFiles: ['packages/markdown/**/*', 'test/e2e/**/*', 'test/regressions/**/*'],
+ extends: ['plugin:testing-library/react'],
+ rules: {
+ 'testing-library/no-container': 'off',
+ 'testing-library/prefer-screen-queries': 'off',
+ },
+ },
{
files: ['docs/src/modules/components/**/*.js'],
rules: {
diff --git a/package.json b/package.json
index 3a168cb26c50cf..fde932d4e5e83f 100644
--- a/package.json
+++ b/package.json
@@ -154,6 +154,7 @@
"eslint-plugin-react": "^7.34.4",
"eslint-plugin-react-compiler": "0.0.0-experimental-0998c1e-20240625",
"eslint-plugin-react-hooks": "^4.6.2",
+ "eslint-plugin-testing-library": "^6.2.2",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"globby": "^14.0.2",
diff --git a/packages-internal/test-utils/src/mochaHooks.test.js b/packages-internal/test-utils/src/mochaHooks.test.js
index 2628a7a0543d68..eb99a562d19d46 100644
--- a/packages-internal/test-utils/src/mochaHooks.test.js
+++ b/packages-internal/test-utils/src/mochaHooks.test.js
@@ -83,6 +83,7 @@ describe('mochaHooks', () => {
// not wrapped in act()
unsafeSetState(1);
// make sure effects are flushed
+ // eslint-disable-next-line testing-library/no-unnecessary-act
act(() => {});
});
diff --git a/packages/mui-base/src/Button/Button.test.tsx b/packages/mui-base/src/Button/Button.test.tsx
index 95a89a1d7d7d08..5803f9410dac6e 100644
--- a/packages/mui-base/src/Button/Button.test.tsx
+++ b/packages/mui-base/src/Button/Button.test.tsx
@@ -67,6 +67,7 @@ describe('', () => {
const { getByRole } = render();
const button = getByRole('button');
+
act(() => {
button.focus();
});
@@ -83,14 +84,12 @@ describe('', () => {
const button = getByRole('button');
act(() => {
button.focus();
- });
-
- act(() => {
button.click();
- fireEvent.keyDown(button, { key: 'Enter' });
- fireEvent.keyUp(button, { key: ' ' });
});
+ fireEvent.keyDown(button, { key: 'Enter' });
+ fireEvent.keyUp(button, { key: ' ' });
+
expect(handleClick.callCount).to.equal(0);
});
});
@@ -129,14 +128,12 @@ describe('', () => {
const button = getByRole('button');
act(() => {
button.focus();
- });
-
- act(() => {
button.click();
- fireEvent.keyDown(button, { key: 'Enter' });
- fireEvent.keyUp(button, { key: ' ' });
});
+ fireEvent.keyDown(button, { key: 'Enter' });
+ fireEvent.keyUp(button, { key: ' ' });
+
expect(handleClick.callCount).to.equal(0);
});
});
diff --git a/packages/mui-base/src/ClickAwayListener/ClickAwayListener.test.js b/packages/mui-base/src/ClickAwayListener/ClickAwayListener.test.js
index 1cbd6e67d15e50..56093a648394ce 100644
--- a/packages/mui-base/src/ClickAwayListener/ClickAwayListener.test.js
+++ b/packages/mui-base/src/ClickAwayListener/ClickAwayListener.test.js
@@ -25,6 +25,7 @@ describe('', () => {
* React bug: https://github.com/facebook/react/issues/20074
*/
function render(...args) {
+ // eslint-disable-next-line testing-library/render-result-naming-convention
const result = clientRender(...args);
clock.tick(0);
return result;
diff --git a/packages/mui-base/src/Dropdown/Dropdown.test.tsx b/packages/mui-base/src/Dropdown/Dropdown.test.tsx
index dde3e91b3c4bbc..e3cc1b8f6bd31a 100644
--- a/packages/mui-base/src/Dropdown/Dropdown.test.tsx
+++ b/packages/mui-base/src/Dropdown/Dropdown.test.tsx
@@ -22,6 +22,7 @@ describe('', () => {
element: React.ReactElement>,
options?: RenderOptions,
): Promise {
+ // eslint-disable-next-line testing-library/render-result-naming-convention
const rendered = internalRender(element, options);
await flushMicrotasks();
return rendered;
diff --git a/packages/mui-base/src/Menu/Menu.test.tsx b/packages/mui-base/src/Menu/Menu.test.tsx
index 5f6ebbe141c311..7dbec0667f5fb1 100644
--- a/packages/mui-base/src/Menu/Menu.test.tsx
+++ b/packages/mui-base/src/Menu/Menu.test.tsx
@@ -32,6 +32,7 @@ describe('', () => {
element: React.ReactElement>,
options?: RenderOptions,
): Promise {
+ // eslint-disable-next-line testing-library/render-result-naming-convention
const rendered = internalRender(element, options);
await flushMicrotasks();
return rendered;
diff --git a/packages/mui-base/src/MenuButton/MenuButton.test.tsx b/packages/mui-base/src/MenuButton/MenuButton.test.tsx
index ee213658e70dd2..d2a5d48eec3a8a 100644
--- a/packages/mui-base/src/MenuButton/MenuButton.test.tsx
+++ b/packages/mui-base/src/MenuButton/MenuButton.test.tsx
@@ -98,7 +98,7 @@ describe('', () => {
button.focus();
});
- expect(document.activeElement).to.equal(button);
+ expect(button).toHaveFocus();
});
});
diff --git a/packages/mui-base/src/Select/Select.test.tsx b/packages/mui-base/src/Select/Select.test.tsx
index 538706b5cd5dab..ad1c7798cebb0d 100644
--- a/packages/mui-base/src/Select/Select.test.tsx
+++ b/packages/mui-base/src/Select/Select.test.tsx
@@ -9,6 +9,7 @@ import {
MuiRenderResult,
RenderOptions,
flushMicrotasks,
+ within,
} from '@mui/internal-test-utils';
import userEvent from '@testing-library/user-event';
import { Select, SelectListboxSlotProps, selectClasses } from '@mui/base/Select';
@@ -28,6 +29,7 @@ describe('', () => {
element: React.ReactElement>,
options?: RenderOptions,
): Promise {
+ // eslint-disable-next-line testing-library/render-result-naming-convention
const rendered = internalRender(element, options);
await flushMicrotasks();
return rendered;
@@ -918,15 +920,15 @@ describe('', () => {
});
it('renders a zero-width space when there is no selected value nor placeholder and renderValue is not provided', async () => {
- const { getByRole } = await render(
+ await render(
,
);
- const select = getByRole('combobox');
- const zws = select.querySelector('.notranslate');
+ const select = screen.getByRole('combobox');
+ const zws = within(select).getByText(''); // zero-width space
expect(zws).not.to.equal(null);
});
@@ -1160,7 +1162,7 @@ describe('', () => {
);
const input = getAllByRole('textbox')[0];
- expect(document.activeElement).to.equal(input);
+ expect(input).toHaveFocus();
});
it('scrolls to initially highlighted option after opening', async function test() {
@@ -1219,7 +1221,7 @@ describe('', () => {
);
const select = getByRole('combobox');
- expect(document.activeElement).to.equal(select);
+ expect(select).toHaveFocus();
});
});
@@ -1346,7 +1348,7 @@ describe('', () => {
describe('browser autofill', () => {
it('sets value and calls external onChange when browser autofills', async () => {
const onChangeHandler = spy();
- const { container } = await render(
+ await render(
,
);
- const hiddenInput = container.querySelector('[autocomplete="country"]');
+ const hiddenInput = screen.getByRole('textbox', { hidden: true });
expect(hiddenInput).not.to.eq(null);
expect(hiddenInput).to.have.value('germany');
@@ -1372,7 +1374,7 @@ describe('', () => {
it('does not set value when browser autofills invalid value', async () => {
const onChangeHandler = spy();
- const { container } = await render(
+ await render(
@@ -1380,7 +1382,7 @@ describe('', () => {
,
);
- const hiddenInput = container.querySelector('[autocomplete="country"]');
+ const hiddenInput = screen.getByRole('textbox', { hidden: true });
expect(hiddenInput).not.to.eq(null);
expect(hiddenInput).to.have.value('germany');
@@ -1397,7 +1399,7 @@ describe('', () => {
it('clears value and calls external onChange when browser clears autofill', async () => {
const onChangeHandler = spy();
- const { container } = await render(
+ await render(
@@ -1405,7 +1407,7 @@ describe('', () => {
,
);
- const hiddenInput = container.querySelector('[autocomplete="country"]');
+ const hiddenInput = screen.getByRole('textbox', { hidden: true });
expect(hiddenInput).not.to.eq(null);
expect(hiddenInput).to.have.value('germany');
diff --git a/packages/mui-base/src/Snackbar/Snackbar.test.tsx b/packages/mui-base/src/Snackbar/Snackbar.test.tsx
index 30746b3d31ded9..e749259827ebac 100644
--- a/packages/mui-base/src/Snackbar/Snackbar.test.tsx
+++ b/packages/mui-base/src/Snackbar/Snackbar.test.tsx
@@ -18,6 +18,7 @@ describe('', () => {
* React bug: https://github.com/facebook/react/issues/20074
*/
function render(...args: [React.ReactElement]) {
+ // eslint-disable-next-line testing-library/render-result-naming-convention
const result = clientRender(...args);
clock.tick(0);
return result;
diff --git a/packages/mui-base/src/Unstable_NumberInput/NumberInput.test.tsx b/packages/mui-base/src/Unstable_NumberInput/NumberInput.test.tsx
index 5f3b0fc32b4604..56517509de7632 100644
--- a/packages/mui-base/src/Unstable_NumberInput/NumberInput.test.tsx
+++ b/packages/mui-base/src/Unstable_NumberInput/NumberInput.test.tsx
@@ -357,21 +357,21 @@ describe('', () => {
const incrementButton = getByTestId('increment-btn');
const decrementButton = getByTestId('decrement-btn');
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
await userEvent.click(incrementButton);
- expect(document.activeElement).to.equal(input);
+ expect(input).toHaveFocus();
act(() => {
input.blur();
});
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
await userEvent.click(decrementButton);
- expect(document.activeElement).to.equal(input);
+ expect(input).toHaveFocus();
});
});
@@ -554,13 +554,13 @@ describe('', () => {
const { getByRole } = render();
const input = getByRole('textbox') as HTMLInputElement;
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
await userEvent.keyboard('[Tab]');
- expect(document.activeElement).to.equal(input);
+ expect(input).toHaveFocus();
await userEvent.keyboard('[Tab]');
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
});
});
@@ -600,9 +600,7 @@ describe('', () => {
const input = getByRole('textbox') as HTMLInputElement;
const button = getByTestId('button') as HTMLButtonElement;
- act(() => {
- fireEvent.click(button);
- });
+ fireEvent.click(button);
await userEvent.click(input);
expect(input.value).to.equal('20');
diff --git a/packages/mui-base/src/Unstable_Popup/Popup.test.tsx b/packages/mui-base/src/Unstable_Popup/Popup.test.tsx
index 6239c136638c5e..9c6c4fbcb03829 100644
--- a/packages/mui-base/src/Unstable_Popup/Popup.test.tsx
+++ b/packages/mui-base/src/Unstable_Popup/Popup.test.tsx
@@ -52,6 +52,7 @@ describe('', () => {
describeConformanceUnstyled(, () => ({
inheritComponent: 'div',
render: async (...renderArgs) => {
+ // eslint-disable-next-line testing-library/render-result-naming-convention
const result = render(...renderArgs);
await waitForPosition();
diff --git a/packages/mui-base/src/unstable_useNumberInput/useNumberInput.test.tsx b/packages/mui-base/src/unstable_useNumberInput/useNumberInput.test.tsx
index 0b2a628606db49..7bc5b82d0119a5 100644
--- a/packages/mui-base/src/unstable_useNumberInput/useNumberInput.test.tsx
+++ b/packages/mui-base/src/unstable_useNumberInput/useNumberInput.test.tsx
@@ -132,7 +132,7 @@ describe('useNumberInput', () => {
expect(handleChange.callCount).to.equal(0);
await userEvent.keyboard('[Tab]');
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.equal(34);
@@ -180,7 +180,7 @@ describe('useNumberInput', () => {
await userEvent.keyboard('9');
await userEvent.keyboard('[Tab]');
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
expect(handleChange.args[0][1]).to.equal(5);
});
@@ -204,7 +204,7 @@ describe('useNumberInput', () => {
await userEvent.keyboard('-9');
await userEvent.keyboard('[Tab]');
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
expect(handleChange.args[0][1]).to.equal(5);
});
@@ -229,7 +229,7 @@ describe('useNumberInput', () => {
await userEvent.keyboard('4');
await userEvent.keyboard('[Tab]');
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
expect(handleChange.args[0][1]).to.equal(5);
});
@@ -254,7 +254,7 @@ describe('useNumberInput', () => {
expect(input.value).to.equal('');
await userEvent.keyboard('[Tab]');
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.equal(null);
@@ -280,7 +280,7 @@ describe('useNumberInput', () => {
expect(input.value).to.equal('-');
await userEvent.keyboard('[Tab]');
- expect(document.activeElement).to.equal(document.body);
+ expect(document.body).toHaveFocus();
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.equal(null);
diff --git a/packages/mui-base/src/useAutocomplete/useAutocomplete.test.js b/packages/mui-base/src/useAutocomplete/useAutocomplete.test.js
index 72ac62297340d0..ae3b709a2e4d48 100644
--- a/packages/mui-base/src/useAutocomplete/useAutocomplete.test.js
+++ b/packages/mui-base/src/useAutocomplete/useAutocomplete.test.js
@@ -1,6 +1,6 @@
import * as React from 'react';
import { expect } from 'chai';
-import { createRenderer, screen, ErrorBoundary, act, fireEvent } from '@mui/internal-test-utils';
+import { createRenderer, screen, ErrorBoundary, fireEvent } from '@mui/internal-test-utils';
import { spy } from 'sinon';
import { useAutocomplete, createFilterOptions } from '@mui/base/useAutocomplete';
@@ -319,10 +319,8 @@ describe('useAutocomplete', () => {
render();
const input = screen.getByRole('combobox');
- act(() => {
- fireEvent.change(input, { target: { value: 'free' } });
- input.blur();
- });
+ fireEvent.change(input, { target: { value: 'free' } });
+ fireEvent.blur(input);
expect(input.value).to.equal('free');
});
diff --git a/packages/mui-joy/src/Accordion/Accordion.test.tsx b/packages/mui-joy/src/Accordion/Accordion.test.tsx
index aebd710821c6b7..8b877b1b5767a8 100644
--- a/packages/mui-joy/src/Accordion/Accordion.test.tsx
+++ b/packages/mui-joy/src/Accordion/Accordion.test.tsx
@@ -121,13 +121,13 @@ describe('', () => {
});
it('should warn when switching from controlled to uncontrolled', () => {
- const wrapper = render(
+ const { setProps } = render(
Header
,
);
- expect(() => wrapper.setProps({ expanded: undefined })).to.toErrorDev(
+ expect(() => setProps({ expanded: undefined })).to.toErrorDev(
'MUI: A component is changing the controlled expanded state of Accordion to be uncontrolled.',
);
});
diff --git a/packages/mui-joy/src/Autocomplete/Autocomplete.test.tsx b/packages/mui-joy/src/Autocomplete/Autocomplete.test.tsx
index 0faa8ba03dc113..478e19cb023479 100644
--- a/packages/mui-joy/src/Autocomplete/Autocomplete.test.tsx
+++ b/packages/mui-joy/src/Autocomplete/Autocomplete.test.tsx
@@ -102,9 +102,10 @@ describe('Joy ', () => {
act(() => {
input.focus();
- fireEvent.change(document.activeElement!, { target: { value: 'a' } });
});
+ fireEvent.change(document.activeElement!, { target: { value: 'a' } });
+
expect(input.value).to.equal('a');
act(() => {
@@ -344,9 +345,10 @@ describe('Joy ', () => {
);
const textbox = screen.getByRole('combobox');
+ fireEvent.change(textbox, { target: { value: 't' } });
+ fireEvent.keyDown(textbox, { key: 'ArrowDown' });
+
act(() => {
- fireEvent.change(textbox, { target: { value: 't' } });
- fireEvent.keyDown(textbox, { key: 'ArrowDown' });
textbox.blur();
});
diff --git a/packages/mui-joy/src/Checkbox/Checkbox.test.tsx b/packages/mui-joy/src/Checkbox/Checkbox.test.tsx
index f2a5636cd49926..c8e4302e026516 100644
--- a/packages/mui-joy/src/Checkbox/Checkbox.test.tsx
+++ b/packages/mui-joy/src/Checkbox/Checkbox.test.tsx
@@ -90,10 +90,8 @@ describe('', () => {
const { getByRole } = render();
// how a user would trigger it
- act(() => {
- getByRole('checkbox').click();
- fireEvent.change(getByRole('checkbox'), { target: { checked: '' } });
- });
+ fireEvent.click(getByRole('checkbox'));
+ fireEvent.change(getByRole('checkbox'), { target: { checked: '' } });
expect(getByRole('checkbox')).to.have.property('checked', false);
});
diff --git a/packages/mui-joy/src/ListItemButton/ListItemButton.test.tsx b/packages/mui-joy/src/ListItemButton/ListItemButton.test.tsx
index 08f3eb26a2c20d..b2f526165c54ee 100644
--- a/packages/mui-joy/src/ListItemButton/ListItemButton.test.tsx
+++ b/packages/mui-joy/src/ListItemButton/ListItemButton.test.tsx
@@ -65,8 +65,9 @@ describe('Joy ', () => {
const { getByRole } = render();
const button = getByRole('button');
+ fireEvent.keyDown(document.activeElement || document.body, { key: 'Tab' });
+
act(() => {
- fireEvent.keyDown(document.activeElement || document.body, { key: 'Tab' });
button.focus();
});
diff --git a/packages/mui-joy/src/Radio/Radio.test.tsx b/packages/mui-joy/src/Radio/Radio.test.tsx
index fdb63ce9459b5c..0aaa8ba51c34ae 100644
--- a/packages/mui-joy/src/Radio/Radio.test.tsx
+++ b/packages/mui-joy/src/Radio/Radio.test.tsx
@@ -104,8 +104,8 @@ describe('', () => {
// how a user would trigger it
act(() => {
getByRole('radio').click();
- fireEvent.change(getByRole('radio'), { target: { checked: '' } });
});
+ fireEvent.change(getByRole('radio'), { target: { checked: '' } });
expect(getByRole('radio')).to.have.property('checked', false);
});
diff --git a/packages/mui-joy/src/Select/Select.test.tsx b/packages/mui-joy/src/Select/Select.test.tsx
index bfe89587c6bdbf..9364d24586b11f 100644
--- a/packages/mui-joy/src/Select/Select.test.tsx
+++ b/packages/mui-joy/src/Select/Select.test.tsx
@@ -616,17 +616,15 @@ describe('Joy ', () => {
,
);
+
// Fire Click of the avatar
- act(() => {
- fireEvent.click(getByTestId('test-element'));
- });
+ fireEvent.click(getByTestId('test-element'));
expect(getByRole('combobox', { hidden: true })).to.have.attribute('aria-expanded', 'true');
// click again should close
- act(() => {
- fireEvent.click(getByTestId('test-element'));
- });
+ fireEvent.click(getByTestId('test-element'));
+
expect(getByRole('combobox', { hidden: true })).to.have.attribute('aria-expanded', 'false');
});
diff --git a/packages/mui-joy/src/Snackbar/Snackbar.test.tsx b/packages/mui-joy/src/Snackbar/Snackbar.test.tsx
index af55c7aa4e5d98..1095f4c521c4dd 100644
--- a/packages/mui-joy/src/Snackbar/Snackbar.test.tsx
+++ b/packages/mui-joy/src/Snackbar/Snackbar.test.tsx
@@ -20,6 +20,7 @@ describe('Joy ', () => {
* React bug: https://github.com/facebook/react/issues/20074
*/
function render(...args: [React.ReactElement]) {
+ // eslint-disable-next-line testing-library/render-result-naming-convention
const result = clientRender(...args);
clock.tick(0);
return result;
diff --git a/packages/mui-joy/src/Switch/Switch.test.tsx b/packages/mui-joy/src/Switch/Switch.test.tsx
index d14a52403e2db0..c3b03bcc933afd 100644
--- a/packages/mui-joy/src/Switch/Switch.test.tsx
+++ b/packages/mui-joy/src/Switch/Switch.test.tsx
@@ -97,8 +97,8 @@ describe('', () => {
// how a user would trigger it
act(() => {
getByRole('switch').click();
- fireEvent.change(getByRole('switch'), { target: { checked: '' } });
});
+ fireEvent.change(getByRole('switch'), { target: { checked: '' } });
expect(getByRole('switch')).to.have.property('checked', false);
});
@@ -126,8 +126,8 @@ describe('', () => {
// how a user would trigger it
act(() => {
getByRole('switch').click();
- fireEvent.change(getByRole('switch'), { target: { checked: '' } });
});
+ fireEvent.change(getByRole('switch'), { target: { checked: '' } });
expect(getByText('On')).toBeVisible();
});
@@ -142,8 +142,8 @@ describe('', () => {
// how a user would trigger it
act(() => {
getByRole('switch').click();
- fireEvent.change(getByRole('switch'), { target: { checked: '' } });
});
+ fireEvent.change(getByRole('switch'), { target: { checked: '' } });
expect(getByText('On')).toBeVisible();
});
diff --git a/packages/mui-lab/src/TabContext/TabContext.test.js b/packages/mui-lab/src/TabContext/TabContext.test.js
index 664124493a6c70..71515aa4f1ae24 100644
--- a/packages/mui-lab/src/TabContext/TabContext.test.js
+++ b/packages/mui-lab/src/TabContext/TabContext.test.js
@@ -75,6 +75,7 @@ describe('', () => {
);
}
+ // eslint-disable-next-line testing-library/render-result-naming-convention
const markup = ReactDOMServer.renderToStaticMarkup(
diff --git a/packages/mui-material/src/Accordion/Accordion.test.js b/packages/mui-material/src/Accordion/Accordion.test.js
index c1d8f3889c73d8..56ce5c8e989adb 100644
--- a/packages/mui-material/src/Accordion/Accordion.test.js
+++ b/packages/mui-material/src/Accordion/Accordion.test.js
@@ -196,13 +196,13 @@ describe('', () => {
});
it('should warn when switching from controlled to uncontrolled', () => {
- const wrapper = render(
+ const { setProps } = render(
{minimalChildren}
,
);
- expect(() => wrapper.setProps({ expanded: undefined })).to.toErrorDev(
+ expect(() => setProps({ expanded: undefined })).to.toErrorDev(
'MUI: A component is changing the controlled expanded state of Accordion to be uncontrolled.',
);
});
diff --git a/packages/mui-material/src/AccordionSummary/AccordionSummary.test.js b/packages/mui-material/src/AccordionSummary/AccordionSummary.test.js
index a8d1cf1aaa510d..1f9f932426f969 100644
--- a/packages/mui-material/src/AccordionSummary/AccordionSummary.test.js
+++ b/packages/mui-material/src/AccordionSummary/AccordionSummary.test.js
@@ -64,9 +64,9 @@ describe('', () => {
it('should fire onBlur when the button blurs', () => {
const handleBlur = spy();
const { getByRole } = render();
+ const button = getByRole('button');
act(() => {
- const button = getByRole('button');
button.focus();
button.blur();
});
diff --git a/packages/mui-material/src/Autocomplete/Autocomplete.test.js b/packages/mui-material/src/Autocomplete/Autocomplete.test.js
index 7fda0169707e28..51f51d71e5f5e4 100644
--- a/packages/mui-material/src/Autocomplete/Autocomplete.test.js
+++ b/packages/mui-material/src/Autocomplete/Autocomplete.test.js
@@ -7,6 +7,7 @@ import {
fireEvent,
screen,
strictModeDoubleLoggingSuppressed,
+ within,
} from '@mui/internal-test-utils';
import { spy } from 'sinon';
import userEvent from '@testing-library/user-event';
@@ -25,6 +26,7 @@ import Tooltip from '@mui/material/Tooltip';
import describeConformance from '../../test/describeConformance';
function checkHighlightIs(listbox, expected) {
+ // eslint-disable-next-line testing-library/no-node-access
const focused = listbox.querySelector(`.${classes.focused}`);
if (expected) {
@@ -106,7 +108,7 @@ describe('', () => {
}),
);
- it('should be customizable in the theme', () => {
+ it('should be customizable in the theme', async () => {
const theme = createTheme({
components: {
MuiAutocomplete: {
@@ -124,6 +126,7 @@ describe('', () => {
} />
,
);
+ // eslint-disable-next-line testing-library/no-node-access
expect(document.querySelector(`.${classes.paper}`)).to.toHaveComputedStyle({
mixBlendMode: 'darken',
});
@@ -138,13 +141,14 @@ describe('', () => {
act(() => {
input.focus();
- fireEvent.change(document.activeElement, { target: { value: 'a' } });
});
+ fireEvent.change(input, { target: { value: 'a' } });
+
expect(input.value).to.equal('a');
act(() => {
- document.activeElement.blur();
+ input.blur();
});
expect(input.value).to.equal('');
});
@@ -157,7 +161,9 @@ describe('', () => {
renderInput={(params) => }
/>,
);
+ // eslint-disable-next-line testing-library/no-node-access
expect(container.querySelector(`.${classes.root}`)).to.have.class(classes.hasClearIcon);
+ // eslint-disable-next-line testing-library/no-node-access
expect(container.querySelector(`.${classes.root}`)).to.have.class(classes.hasPopupIcon);
});
});
@@ -173,6 +179,7 @@ describe('', () => {
/>,
);
fireEvent.keyDown(screen.getByRole('combobox'), { key: 'ArrowDown' });
+ // eslint-disable-next-line testing-library/no-node-access
expect(document.querySelector(`.${classes.paper}`).textContent).to.equal('Loading…');
});
@@ -186,11 +193,13 @@ describe('', () => {
);
fireEvent.keyDown(screen.getByRole('combobox'), { key: 'ArrowDown' });
+ // eslint-disable-next-line testing-library/no-node-access
expect(document.querySelector(`.${classes.paper}`).textContent).not.to.equal('Loading…');
const listbox = screen.getByRole('listbox');
- const htmlOptions = listbox.querySelectorAll('li');
- expect(htmlOptions[0].innerHTML).to.equal('one');
+ const firstOption = within(listbox).getByRole('option', { name: 'one' });
+
+ expect(firstOption).to.not.equal(null);
});
});
@@ -208,8 +217,8 @@ describe('', () => {
);
checkHighlightIs(getByRole('listbox'), 'one');
- fireEvent.change(document.activeElement, { target: { value: 'oo' } });
- fireEvent.change(document.activeElement, { target: { value: 'o' } });
+ fireEvent.change(getByRole('combobox'), { target: { value: 'oo' } });
+ fireEvent.change(getByRole('combobox'), { target: { value: 'o' } });
checkHighlightIs(getByRole('listbox'), 'one');
});
@@ -226,7 +235,7 @@ describe('', () => {
);
checkHighlightIs(getByRole('listbox'), 'one');
- fireEvent.change(document.activeElement, { target: { value: 'two' } });
+ fireEvent.change(getByRole('combobox'), { target: { value: 'two' } });
checkHighlightIs(getByRole('listbox'), 'two');
});
@@ -481,17 +490,15 @@ describe('', () => {
);
const textbox = screen.getByRole('combobox');
- act(() => {
- fireEvent.change(textbox, { target: { value: 't' } });
- fireEvent.keyDown(textbox, { key: 'ArrowDown' });
- textbox.blur();
- });
+ fireEvent.change(textbox, { target: { value: 't' } });
+ fireEvent.keyDown(textbox, { key: 'ArrowDown' });
+ fireEvent.blur(textbox);
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal(options);
});
- it('should add new value when autoSelect & multiple & freeSolo on blur', () => {
+ it('should add new value when autoSelect & multiple & freeSolo on blur', async () => {
const handleChange = spy();
render(
', () => {
/>,
);
- fireEvent.change(document.activeElement, { target: { value: 'a' } });
+ fireEvent.change(screen.getByRole('combobox'), { target: { value: 'a' } });
act(() => {
- document.activeElement.blur();
+ screen.getByRole('combobox').blur();
});
expect(handleChange.callCount).to.equal(1);
@@ -528,7 +535,7 @@ describe('', () => {
act(() => {
input.focus();
- document.activeElement.blur();
+ input.blur();
input.focus();
});
});
@@ -1056,7 +1063,7 @@ describe('', () => {
it('does not open on clear', () => {
const handleOpen = spy();
const handleChange = spy();
- const { container } = render(
+ render(
', () => {
/>,
);
- const clear = container.querySelector('button');
+ const clear = screen.getByLabelText('Clear');
fireEvent.click(clear);
expect(handleOpen.callCount).to.equal(0);
@@ -1162,7 +1169,7 @@ describe('', () => {
);
const textbox = screen.getByRole('combobox');
fireEvent.change(textbox, { target: { value: 'test' } });
- expect(document.activeElement.value).to.equal('test');
+ expect(textbox.value).to.equal('test');
act(() => {
textbox.blur();
});
@@ -1179,7 +1186,7 @@ describe('', () => {
);
const textbox = screen.getByRole('combobox');
fireEvent.change(textbox, { target: { value: 'test' } });
- expect(document.activeElement.value).to.equal('test');
+ expect(textbox.value).to.equal('test');
act(() => {
textbox.blur();
});
@@ -1198,7 +1205,7 @@ describe('', () => {
);
const textbox = screen.getByRole('combobox');
fireEvent.change(textbox, { target: { value: 'test' } });
- expect(document.activeElement.value).to.equal('test');
+ expect(textbox.value).to.equal('test');
act(() => {
textbox.blur();
});
@@ -1329,7 +1336,7 @@ describe('', () => {
expect(textbox).to.have.attribute('aria-expanded', 'false');
act(() => {
- document.activeElement.blur();
+ textbox.blur();
});
expect(textbox).to.have.attribute('aria-expanded', 'false');
@@ -1560,6 +1567,7 @@ describe('', () => {
// userEvent will fail at releasing MouseLeft if we target the
//