From e36ee763faf9f273f162ad3292b36382834bdf30 Mon Sep 17 00:00:00 2001 From: Ricky Date: Wed, 10 Apr 2024 10:33:51 -0400 Subject: [PATCH] [tests] assertLog before act in ReactCompositeComponentState (#28758) Fixes tests blocking https://github.com/facebook/react/pull/28737 --- .../ReactCompositeComponentState-test.js | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactCompositeComponentState-test.js b/packages/react-dom/src/__tests__/ReactCompositeComponentState-test.js index ecb30f0f1d78e..bd11d1ab6d016 100644 --- a/packages/react-dom/src/__tests__/ReactCompositeComponentState-test.js +++ b/packages/react-dom/src/__tests__/ReactCompositeComponentState-test.js @@ -152,21 +152,6 @@ describe('ReactCompositeComponent-state', () => { root.render(); }); - await act(() => { - root.render(); - }); - - await act(() => { - testComponentInstance.setFavoriteColor('blue'); - }); - await act(() => { - testComponentInstance.forceUpdate( - testComponentInstance.peekAtCallback('forceUpdate'), - ); - }); - - root.unmount(); - assertLog([ // there is no state when getInitialState() is called 'getInitialState undefined', @@ -198,6 +183,13 @@ describe('ReactCompositeComponent-state', () => { 'componentDidUpdate-prevState orange', 'setState-yellow yellow', 'commit yellow', + ]); + + await act(() => { + root.render(); + }); + + assertLog([ 'componentWillReceiveProps-start yellow', // setState({color:'green'}) only enqueues a pending state. 'componentWillReceiveProps-end yellow', @@ -216,6 +208,13 @@ describe('ReactCompositeComponent-state', () => { 'componentDidUpdate-prevState yellow', 'setState-receiveProps green', 'commit green', + ]); + + await act(() => { + testComponentInstance.setFavoriteColor('blue'); + }); + + assertLog([ // setFavoriteColor('blue') 'shouldComponentUpdate-currentState green', 'shouldComponentUpdate-nextState blue', @@ -226,6 +225,13 @@ describe('ReactCompositeComponent-state', () => { 'componentDidUpdate-prevState green', 'setFavoriteColor blue', 'commit blue', + ]); + await act(() => { + testComponentInstance.forceUpdate( + testComponentInstance.peekAtCallback('forceUpdate'), + ); + }); + assertLog([ // forceUpdate() 'componentWillUpdate-currentState blue', 'componentWillUpdate-nextState blue', @@ -234,7 +240,12 @@ describe('ReactCompositeComponent-state', () => { 'componentDidUpdate-prevState blue', 'forceUpdate blue', 'commit blue', - // unmountComponent() + ]); + + root.unmount(); + + assertLog([ + // unmount() // state is available within `componentWillUnmount()` 'componentWillUnmount blue', ]); @@ -375,13 +386,13 @@ describe('ReactCompositeComponent-state', () => { await act(() => { root.render(); }); + + assertLog(['parent render one', 'child render one']); await act(() => { root.render(); }); assertLog([ - 'parent render one', - 'child render one', 'parent render one', 'child componentWillReceiveProps one', 'child componentWillReceiveProps done one',