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

[styles][withStyles] Expect React defaultProps warning in test (#42752) #42851

Merged
merged 1 commit into from
Jul 5, 2024
Merged
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
50 changes: 34 additions & 16 deletions packages/mui-styles/src/withStyles/withStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,16 @@ describe('withStyles', () => {
const jssCallbackStub = stub().returns({});
const styles = { root: jssCallbackStub };
const StyledComponent = withStyles(styles)(MyComp);
render(<StyledComponent mySuppliedProp={222} />);
const renderCb = () => render(<StyledComponent mySuppliedProp={222} />);

// React 18.3.0 started warning for deprecated defaultProps for function components
if (React.version.startsWith('18.3')) {
expect(renderCb).toErrorDev([
'Warning: MyComp: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.',
]);
} else {
renderCb();
}

expect(jssCallbackStub.callCount).to.equal(1);
expect(jssCallbackStub.args[0][0]).to.deep.equal({
Expand Down Expand Up @@ -178,24 +187,33 @@ describe('withStyles', () => {

const styles = { root: { display: 'flex' } };
const StyledComponent = withStyles(styles, { name: 'MuiFoo' })(MuiFoo);

const { container } = render(
<ThemeProvider
theme={createTheme({
components: {
MuiFoo: {
defaultProps: {
foo: 'bar',
const renderCb = () =>
render(
<ThemeProvider
theme={createTheme({
components: {
MuiFoo: {
defaultProps: {
foo: 'bar',
},
},
},
},
})}
>
<StyledComponent foo={undefined} />
</ThemeProvider>,
);
})}
>
<StyledComponent foo={undefined} />
</ThemeProvider>,
);

expect(container).to.have.text('bar');
// React 18.3.0 started warning for deprecated defaultProps for function components
if (React.version.startsWith('18.3')) {
expect(renderCb).toErrorDev([
'Warning: MuiFoo: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.',
]);
} else {
renderCb();
}

expect(screen.getByText('bar')).not.to.equal(null);
});

it('should work when depending on a theme', () => {
Expand Down
Loading