Skip to content

Commit a7f4b1a

Browse files
authored
[styles][withStyles] Expect React defaultProps warning in test (#42752)
1 parent 37e9dee commit a7f4b1a

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
@@ -136,7 +136,16 @@ describe('withStyles', () => {
136136
const jssCallbackStub = stub().returns({});
137137
const styles = { root: jssCallbackStub };
138138
const StyledComponent = withStyles(styles)(MyComp);
139-
render(<StyledComponent mySuppliedProp={222} />);
139+
const renderCb = () => render(<StyledComponent mySuppliedProp={222} />);
140+
141+
// React 18.3.0 started warning for deprecated defaultProps for function components
142+
if (React.version.startsWith('18.3')) {
143+
expect(renderCb).toErrorDev([
144+
'Warning: MyComp: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.',
145+
]);
146+
} else {
147+
renderCb();
148+
}
140149

141150
expect(jssCallbackStub.callCount).to.equal(1);
142151
expect(jssCallbackStub.args[0][0]).to.deep.equal({
@@ -179,24 +188,33 @@ describe('withStyles', () => {
179188

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

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

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

0 commit comments

Comments
 (0)