From 08cd087cada28bf05165b87b6e31a97c6ac598b4 Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Wed, 10 Jan 2024 10:17:15 -0500 Subject: [PATCH] Convert ReactPureComponent-test to createRoot (#27917) Convert ReactPureComponent-test to createRoot --- .../src/__tests__/ReactPureComponent-test.js | 63 ++++++++++++++----- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/packages/react/src/__tests__/ReactPureComponent-test.js b/packages/react/src/__tests__/ReactPureComponent-test.js index 0c085fa063..e55a601364 100644 --- a/packages/react/src/__tests__/ReactPureComponent-test.js +++ b/packages/react/src/__tests__/ReactPureComponent-test.js @@ -9,16 +9,20 @@ 'use strict'; +let act; + let React; -let ReactDOM; +let ReactDOMClient; describe('ReactPureComponent', () => { beforeEach(() => { + act = require('internal-test-utils').act; + React = require('react'); - ReactDOM = require('react-dom'); + ReactDOMClient = require('react-dom/client'); }); - it('should render', () => { + it('should render', async () => { let renders = 0; class Component extends React.PureComponent { constructor() { @@ -32,36 +36,47 @@ describe('ReactPureComponent', () => { } const container = document.createElement('div'); + const root = ReactDOMClient.createRoot(container); let text; - let component; + const componentRef = React.createRef(); text = ['porcini']; - component = ReactDOM.render(, container); + await act(() => { + root.render(); + }); expect(container.textContent).toBe('porcini'); expect(renders).toBe(1); text = ['morel']; - component = ReactDOM.render(, container); + await act(() => { + root.render(); + }); expect(container.textContent).toBe('morel'); expect(renders).toBe(2); text[0] = 'portobello'; - component = ReactDOM.render(, container); + await act(() => { + root.render(); + }); expect(container.textContent).toBe('morel'); expect(renders).toBe(2); // Setting state without changing it doesn't cause a rerender. - component.setState({type: 'mushrooms'}); + await act(() => { + componentRef.current.setState({type: 'mushrooms'}); + }); expect(container.textContent).toBe('morel'); expect(renders).toBe(2); // But changing state does. - component.setState({type: 'portobello mushrooms'}); + await act(() => { + componentRef.current.setState({type: 'portobello mushrooms'}); + }); expect(container.textContent).toBe('portobello'); expect(renders).toBe(3); }); - it('can override shouldComponentUpdate', () => { + it('can override shouldComponentUpdate', async () => { let renders = 0; class Component extends React.PureComponent { render() { @@ -74,17 +89,24 @@ describe('ReactPureComponent', () => { } const container = document.createElement('div'); - expect(() => ReactDOM.render(, container)).toErrorDev( + const root = ReactDOMClient.createRoot(container); + await expect(async () => { + await act(() => { + root.render(); + }); + }).toErrorDev( 'Warning: ' + 'Component has a method called shouldComponentUpdate(). ' + 'shouldComponentUpdate should not be used when extending React.PureComponent. ' + 'Please extend React.Component if shouldComponentUpdate is used.', ); - ReactDOM.render(, container); + await act(() => { + root.render(); + }); expect(renders).toBe(2); }); - it('extends React.Component', () => { + it('extends React.Component', async () => { let renders = 0; class Component extends React.PureComponent { render() { @@ -94,11 +116,14 @@ describe('ReactPureComponent', () => { return
; } } - ReactDOM.render(, document.createElement('div')); + const root = ReactDOMClient.createRoot(document.createElement('div')); + await act(() => { + root.render(); + }); expect(renders).toBe(1); }); - it('should warn when shouldComponentUpdate is defined on React.PureComponent', () => { + it('should warn when shouldComponentUpdate is defined on React.PureComponent', async () => { class PureComponent extends React.PureComponent { shouldComponentUpdate() { return true; @@ -107,8 +132,12 @@ describe('ReactPureComponent', () => { return
; } } - const container = document.createElement('div'); - expect(() => ReactDOM.render(, container)).toErrorDev( + const root = ReactDOMClient.createRoot(document.createElement('div')); + await expect(async () => { + await act(() => { + root.render(); + }); + }).toErrorDev( 'Warning: ' + 'PureComponent has a method called shouldComponentUpdate(). ' + 'shouldComponentUpdate should not be used when extending React.PureComponent. ' +