Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Cherry-pick React 19-related test PRs #44557

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('<NumberInput />', () => {
}}
/>,
);
}).toErrorDev('Warning: Unknown event handler property `onInputChange`. It will be ignored.');
}).toErrorDev('Unknown event handler property `onInputChange`. It will be ignored.');
});

it('should fire on keyboard input in the textbox instead of onChange', async () => {
Expand Down
58 changes: 38 additions & 20 deletions packages/mui-base/src/useAutocomplete/useAutocomplete.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, screen, ErrorBoundary, act, fireEvent } from '@mui-internal/test-utils';
import {
createRenderer,
screen,
ErrorBoundary,
act,
fireEvent,
reactMajor,
} from '@mui-internal/test-utils';
import { spy } from 'sinon';
import { useAutocomplete, createFilterOptions } from '@mui/base/useAutocomplete';

Expand Down Expand Up @@ -276,28 +283,39 @@ describe('useAutocomplete', () => {
);
}

const muiErrorMessage = 'MUI: Unable to find the input element.';
const aboveErrorUlElementMessage = 'The above error occurred in the <ul> component';
const aboveErrorTestComponentMessage = 'The above error occurred in the <Test> component';
const node16ErrorMessage =
"Error: Uncaught [TypeError: Cannot read properties of null (reading 'removeAttribute')]";
const olderNodeErrorMessage =
"Error: Uncaught [TypeError: Cannot read property 'removeAttribute' of null]";
"TypeError: Cannot read properties of null (reading 'removeAttribute')";
const olderNodeErrorMessage = "TypeError: Cannot read property 'removeAttribute' of null";

const nodeVersion = Number(process.versions.node.split('.')[0]);
const errorMessage = nodeVersion >= 16 ? node16ErrorMessage : olderNodeErrorMessage;

const devErrorMessages = [
errorMessage,
'MUI: Unable to find the input element.',
errorMessage,
// strict effects runs effects twice
React.version.startsWith('18') && 'MUI: Unable to find the input element.',
React.version.startsWith('18') && errorMessage,
'The above error occurred in the <ul> component',
React.version.startsWith('16') && 'The above error occurred in the <ul> component',
'The above error occurred in the <Test> component',
// strict effects runs effects twice
React.version.startsWith('18') && 'The above error occurred in the <Test> component',
React.version.startsWith('16') && 'The above error occurred in the <Test> component',
];
const nodeErrorMessage = nodeVersion >= 16 ? node16ErrorMessage : olderNodeErrorMessage;

const defaultErrorMessages = [muiErrorMessage, nodeErrorMessage, nodeErrorMessage];

const errorMessagesByReactMajor = {
17: [
nodeErrorMessage,
muiErrorMessage,
nodeErrorMessage,
aboveErrorUlElementMessage,
aboveErrorTestComponentMessage,
],
18: [
nodeErrorMessage,
muiErrorMessage,
nodeErrorMessage,
muiErrorMessage,
nodeErrorMessage,
aboveErrorUlElementMessage,
aboveErrorTestComponentMessage,
aboveErrorTestComponentMessage,
],
};

const devErrorMessages = errorMessagesByReactMajor[reactMajor] || defaultErrorMessages;

expect(() => {
render(
Expand Down
43 changes: 24 additions & 19 deletions packages/mui-joy/src/Autocomplete/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
act,
fireEvent,
strictModeDoubleLoggingSuppressed,
reactMajor,
} from '@mui-internal/test-utils';
import Autocomplete, {
autocompleteClasses as classes,
Expand Down Expand Up @@ -1209,19 +1210,19 @@ describe('Joy <Autocomplete />', () => {
const value = 'not a good value';
const options = ['first option', 'second option'];

const errorMessage = 'None of the options match with `"not a good value"`';

let expectedOccurrences = 4;

if (reactMajor === 18) {
expectedOccurrences = 6;
} else if (reactMajor === 17) {
expectedOccurrences = 2;
}

expect(() => {
render(<Autocomplete value={value} options={options} />);
}).toWarnDev([
'None of the options match with `"not a good value"`',
!strictModeDoubleLoggingSuppressed && 'None of the options match with `"not a good value"`',
'None of the options match with `"not a good value"`',
!strictModeDoubleLoggingSuppressed && 'None of the options match with `"not a good value"`',
// React 18 Strict Effects run mount effects twice which lead to a cascading update
React.version.startsWith('18') && 'None of the options match with `"not a good value"`',
React.version.startsWith('18') &&
!strictModeDoubleLoggingSuppressed &&
'None of the options match with `"not a good value"`',
]);
}).toWarnDev(Array(expectedOccurrences).fill(errorMessage));
});

it('warn if groups options are not sorted', () => {
Expand Down Expand Up @@ -2102,10 +2103,10 @@ describe('Joy <Autocomplete />', () => {
);
expect(handleHighlightChange.callCount).to.equal(
// FIXME: highlighted index implementation should be implemented using React not the DOM.
React.version.startsWith('18') ? 2 : 1,
reactMajor >= 18 ? 2 : 1,
);
expect(handleHighlightChange.args[0]).to.deep.equal([undefined, options[0], 'auto']);
if (React.version.startsWith('18')) {
if (reactMajor >= 18) {
expect(handleHighlightChange.args[1]).to.deep.equal([undefined, options[0], 'auto']);
}
});
Expand All @@ -2122,9 +2123,9 @@ describe('Joy <Autocomplete />', () => {

expect(handleHighlightChange.callCount).to.equal(
// FIXME: highlighted index implementation should be implemented using React not the DOM.
React.version.startsWith('18') ? 4 : 3,
reactMajor >= 18 ? 4 : 3,
);
if (React.version.startsWith('18')) {
if (reactMajor >= 18) {
expect(handleHighlightChange.args[2][0]).to.equal(undefined);
expect(handleHighlightChange.args[2][1]).to.equal(null);
expect(handleHighlightChange.args[2][2]).to.equal('auto');
Expand All @@ -2136,7 +2137,7 @@ describe('Joy <Autocomplete />', () => {
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
expect(handleHighlightChange.callCount).to.equal(
// FIXME: highlighted index implementation should be implemented using React not the DOM.
React.version.startsWith('18') ? 5 : 4,
reactMajor >= 18 ? 5 : 4,
);
expect(handleHighlightChange.lastCall.args[0]).not.to.equal(undefined);
expect(handleHighlightChange.lastCall.args[1]).to.equal(options[1]);
Expand All @@ -2153,9 +2154,9 @@ describe('Joy <Autocomplete />', () => {
fireEvent.mouseMove(firstOption);
expect(handleHighlightChange.callCount).to.equal(
// FIXME: highlighted index implementation should be implemented using React not the DOM.
React.version.startsWith('18') ? 4 : 3,
reactMajor >= 18 ? 4 : 3,
);
if (React.version.startsWith('18')) {
if (reactMajor >= 18) {
expect(handleHighlightChange.args[2][0]).to.equal(undefined);
expect(handleHighlightChange.args[2][1]).to.equal(null);
expect(handleHighlightChange.args[2][2]).to.equal('auto');
Expand Down Expand Up @@ -2200,7 +2201,11 @@ describe('Joy <Autocomplete />', () => {
checkHighlightIs(getByRole('listbox'), 'one');
setProps({ options: ['four', 'five'] });
checkHighlightIs(getByRole('listbox'), 'four');
expect(handleHighlightChange).to.deep.equal([null, 'one', 'four']);

const expectedCallHistory =
reactMajor >= 19 ? [null, 'one', 'one', 'four'] : [null, 'one', 'four'];

expect(handleHighlightChange).to.deep.equal(expectedCallHistory);
});
});

Expand Down
16 changes: 11 additions & 5 deletions packages/mui-lab/src/Masonry/Masonry.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { createRenderer } from '@mui-internal/test-utils';
import { createRenderer, reactMajor } from '@mui-internal/test-utils';
import { expect } from 'chai';
import { createTheme } from '@mui/material/styles';
import defaultTheme from '@mui/material/styles/defaultTheme';
Expand Down Expand Up @@ -100,7 +100,8 @@ describe('<Masonry />', () => {
});

it('should throw console error when children are empty', function test() {
if (!/jsdom/.test(window.navigator.userAgent)) {
// React 19 removed prop types support
if (!/jsdom/.test(window.navigator.userAgent) || reactMajor >= 19) {
this.skip();
}
expect(() => render(<Masonry columns={3} spacing={1} />)).toErrorDev(
Expand All @@ -112,9 +113,14 @@ describe('<Masonry />', () => {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
expect(() => render(<Masonry columns={3} spacing={1} />)).toErrorDev(
'Warning: Failed prop type: The prop `children` is marked as required in `ForwardRef(Masonry)`, but its value is `undefined`.',
);

// React 19 removed prop types support
if (reactMajor < 19) {
expect(() => render(<Masonry columns={3} spacing={1} />)).toErrorDev(
'Warning: Failed prop type: The prop `children` is marked as required in `ForwardRef(Masonry)`, but its value is `undefined`.',
);
}

expect(() => render(<Masonry columns={3} spacing={1} />)).not.to.throw(new TypeError());
});
});
Expand Down
9 changes: 7 additions & 2 deletions packages/mui-material/src/Accordion/Accordion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import { spy } from 'sinon';
import { createRenderer, fireEvent } from '@mui-internal/test-utils';
import { createRenderer, fireEvent, reactMajor } from '@mui-internal/test-utils';
import Accordion, { accordionClasses as classes } from '@mui/material/Accordion';
import Paper from '@mui/material/Paper';
import AccordionSummary from '@mui/material/AccordionSummary';
Expand Down Expand Up @@ -154,7 +154,12 @@ describe('<Accordion />', () => {

describe('prop: children', () => {
describe('first child', () => {
beforeEach(() => {
beforeEach(function beforeEachCallback() {
if (reactMajor >= 19) {
// React 19 removed prop types support
this.skip();
}

PropTypes.resetWarningCache();
});

Expand Down
47 changes: 26 additions & 21 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
fireEvent,
screen,
strictModeDoubleLoggingSuppressed,
reactMajor,
} from '@mui-internal/test-utils';
import { spy } from 'sinon';
import userEvent from '@testing-library/user-event';
Expand Down Expand Up @@ -1691,6 +1692,16 @@ describe('<Autocomplete />', () => {
const value = 'not a good value';
const options = ['first option', 'second option'];

const errorMessage = 'None of the options match with `"not a good value"`';

let expectedOccurrences = 4;

if (reactMajor === 18) {
expectedOccurrences = 6;
} else if (reactMajor === 17) {
expectedOccurrences = 2;
}

expect(() => {
render(
<Autocomplete
Expand All @@ -1699,17 +1710,7 @@ describe('<Autocomplete />', () => {
renderInput={(params) => <TextField {...params} />}
/>,
);
}).toWarnDev([
'None of the options match with `"not a good value"`',
!strictModeDoubleLoggingSuppressed && 'None of the options match with `"not a good value"`',
'None of the options match with `"not a good value"`',
!strictModeDoubleLoggingSuppressed && 'None of the options match with `"not a good value"`',
// React 18 Strict Effects run mount effects twice which lead to a cascading update
React.version.startsWith('18') && 'None of the options match with `"not a good value"`',
React.version.startsWith('18') &&
!strictModeDoubleLoggingSuppressed &&
'None of the options match with `"not a good value"`',
]);
}).toWarnDev(Array(expectedOccurrences).fill(errorMessage));
});

it('warn if groups options are not sorted', () => {
Expand Down Expand Up @@ -2748,10 +2749,10 @@ describe('<Autocomplete />', () => {
);
expect(handleHighlightChange.callCount).to.equal(
// FIXME: highlighted index implementation should be implemented using React not the DOM.
React.version.startsWith('18') ? 2 : 1,
reactMajor >= 18 ? 2 : 1,
);
expect(handleHighlightChange.args[0]).to.deep.equal([undefined, options[0], 'auto']);
if (React.version.startsWith('18')) {
if (reactMajor >= 18) {
expect(handleHighlightChange.args[1]).to.deep.equal([undefined, options[0], 'auto']);
}
});
Expand All @@ -2771,10 +2772,10 @@ describe('<Autocomplete />', () => {
);
expect(handleHighlightChange.callCount).to.equal(
// FIXME: highlighted index implementation should be implemented using React not the DOM.
React.version.startsWith('18') ? 2 : 1,
reactMajor >= 18 ? 2 : 1,
);
expect(handleHighlightChange.args[0]).to.deep.equal([undefined, options[0], 'auto']);
if (React.version.startsWith('18')) {
if (reactMajor >= 18) {
expect(handleHighlightChange.args[1]).to.deep.equal([undefined, options[0], 'auto']);
}
});
Expand All @@ -2796,9 +2797,9 @@ describe('<Autocomplete />', () => {

expect(handleHighlightChange.callCount).to.equal(
// FIXME: highlighted index implementation should be implemented using React not the DOM.
React.version.startsWith('18') ? 4 : 3,
reactMajor >= 18 ? 4 : 3,
);
if (React.version.startsWith('18')) {
if (reactMajor >= 18) {
expect(handleHighlightChange.args[2][0]).to.equal(undefined);
expect(handleHighlightChange.args[2][1]).to.equal(null);
expect(handleHighlightChange.args[2][2]).to.equal('auto');
Expand All @@ -2810,7 +2811,7 @@ describe('<Autocomplete />', () => {
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
expect(handleHighlightChange.callCount).to.equal(
// FIXME: highlighted index implementation should be implemented using React not the DOM.
React.version.startsWith('18') ? 5 : 4,
reactMajor >= 18 ? 5 : 4,
);
expect(handleHighlightChange.lastCall.args[0]).not.to.equal(undefined);
expect(handleHighlightChange.lastCall.args[1]).to.equal(options[1]);
Expand All @@ -2832,9 +2833,9 @@ describe('<Autocomplete />', () => {
fireEvent.mouseMove(firstOption);
expect(handleHighlightChange.callCount).to.equal(
// FIXME: highlighted index implementation should be implemented using React not the DOM.
React.version.startsWith('18') ? 4 : 3,
reactMajor >= 18 ? 4 : 3,
);
if (React.version.startsWith('18')) {
if (reactMajor >= 18) {
expect(handleHighlightChange.args[2][0]).to.equal(undefined);
expect(handleHighlightChange.args[2][1]).to.equal(null);
expect(handleHighlightChange.args[2][2]).to.equal('auto');
Expand Down Expand Up @@ -2883,7 +2884,11 @@ describe('<Autocomplete />', () => {
checkHighlightIs(getByRole('listbox'), 'one');
setProps({ options: ['four', 'five'] });
checkHighlightIs(getByRole('listbox'), 'four');
expect(handleHighlightChange).to.deep.equal([null, 'one', 'four']);

const expectedCallHistory =
reactMajor >= 19 ? [null, 'one', 'one', 'four'] : [null, 'one', 'four'];

expect(handleHighlightChange).to.deep.equal(expectedCallHistory);
});
});

Expand Down
Loading