Skip to content

Commit

Permalink
Expect React warning in test
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongarciah committed Jun 25, 2024
1 parent 280c2fa commit d1ed4e0
Showing 1 changed file with 34 additions and 16 deletions.
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 @@ -136,7 +136,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 @@ -179,24 +188,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

0 comments on commit d1ed4e0

Please sign in to comment.