Skip to content

Commit e745ace

Browse files
committed
[styles][withStyles] Expect React defaultProps warning in test (#42752)
1 parent 3f2aff4 commit e745ace

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

packages/mui-styles/src/withStyles/withStyles.test.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,16 @@ describe('withStyles', () => {
135135
const jssCallbackStub = stub().returns({});
136136
const styles = { root: jssCallbackStub };
137137
const StyledComponent = withStyles(styles)(MyComp);
138-
render(<StyledComponent mySuppliedProp={222} />);
138+
const renderCb = () => render(<StyledComponent mySuppliedProp={222} />);
139+
140+
// React 18.3.0 started warning for deprecated defaultProps for function components
141+
if (React.version.startsWith('18.3')) {
142+
expect(renderCb).toErrorDev([
143+
'Warning: MyComp: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.',
144+
]);
145+
} else {
146+
renderCb();
147+
}
139148

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

179188
const styles = { root: { display: 'flex' } };
180189
const StyledComponent = withStyles(styles, { name: 'MuiFoo' })(MuiFoo);
181-
182-
const { container } = render(
183-
<ThemeProvider
184-
theme={createTheme({
185-
components: {
186-
MuiFoo: {
187-
defaultProps: {
188-
foo: 'bar',
190+
const renderCb = () =>
191+
render(
192+
<ThemeProvider
193+
theme={createTheme({
194+
components: {
195+
MuiFoo: {
196+
defaultProps: {
197+
foo: 'bar',
198+
},
189199
},
190200
},
191-
},
192-
})}
193-
>
194-
<StyledComponent foo={undefined} />
195-
</ThemeProvider>,
196-
);
201+
})}
202+
>
203+
<StyledComponent foo={undefined} />
204+
</ThemeProvider>,
205+
);
197206

198-
expect(container).to.have.text('bar');
207+
// React 18.3.0 started warning for deprecated defaultProps for function components
208+
if (React.version.startsWith('18.3')) {
209+
expect(renderCb).toErrorDev([
210+
'Warning: MuiFoo: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.',
211+
]);
212+
} else {
213+
renderCb();
214+
}
215+
216+
expect(screen.getByText('bar')).not.to.equal(null);
199217
});
200218

201219
it('should work when depending on a theme', () => {

0 commit comments

Comments
 (0)