From d1ed4e0822483d0ec0645934e5d546fe8e42f935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aar=C3=B3n=20Garc=C3=ADa=20Herv=C3=A1s?= Date: Tue, 25 Jun 2024 15:24:45 +0200 Subject: [PATCH] Expect React warning in test --- .../src/withStyles/withStyles.test.js | 50 +++++++++++++------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/packages/mui-styles/src/withStyles/withStyles.test.js b/packages/mui-styles/src/withStyles/withStyles.test.js index 7684f4c65a7be4..aab0aa46b2976a 100644 --- a/packages/mui-styles/src/withStyles/withStyles.test.js +++ b/packages/mui-styles/src/withStyles/withStyles.test.js @@ -136,7 +136,16 @@ describe('withStyles', () => { const jssCallbackStub = stub().returns({}); const styles = { root: jssCallbackStub }; const StyledComponent = withStyles(styles)(MyComp); - render(); + const renderCb = () => render(); + + // 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({ @@ -179,24 +188,33 @@ describe('withStyles', () => { const styles = { root: { display: 'flex' } }; const StyledComponent = withStyles(styles, { name: 'MuiFoo' })(MuiFoo); - - const { container } = render( - + render( + - - , - ); + })} + > + + , + ); - 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', () => {