@samitouri / QOS-React / commits / e36ee763fa

[tests] assertLog before act in ReactCompositeComponentState (#28758)

Fixes tests blocking https://github.com/facebook/react/pull/28737

Ricky committed Apr 10, 2024 at 10:33 UTC e36ee763faf9f273f162ad3292b36382834bdf30
1 file changed +29 -18
packages/react-dom/src/__tests__/ReactCompositeComponentState-test.js
+29 -18
@@ -152,21 +152,6 @@ describe('ReactCompositeComponent-state', () => {
152 root.render(<TestComponent />);
153 });
154
155 - await act(() => {
156 - root.render(<TestComponent nextColor="green" />);
157 - });
158 -
159 - await act(() => {
160 - testComponentInstance.setFavoriteColor('blue');
161 - });
162 - await act(() => {
163 - testComponentInstance.forceUpdate(
164 - testComponentInstance.peekAtCallback('forceUpdate'),
165 - );
166 - });
167 -
168 - root.unmount();
169 -
155 assertLog([
156 // there is no state when getInitialState() is called
157 'getInitialState undefined',
@@ -198,6 +183,13 @@ describe('ReactCompositeComponent-state', () => {
183 'componentDidUpdate-prevState orange',
184 'setState-yellow yellow',
185 'commit yellow',
186 + ]);
187 +
188 + await act(() => {
189 + root.render(<TestComponent nextColor="green" />);
190 + });
191 +
192 + assertLog([
193 'componentWillReceiveProps-start yellow',
194 // setState({color:'green'}) only enqueues a pending state.
195 'componentWillReceiveProps-end yellow',
@@ -216,6 +208,13 @@ describe('ReactCompositeComponent-state', () => {
208 'componentDidUpdate-prevState yellow',
209 'setState-receiveProps green',
210 'commit green',
211 + ]);
212 +
213 + await act(() => {
214 + testComponentInstance.setFavoriteColor('blue');
215 + });
216 +
217 + assertLog([
218 // setFavoriteColor('blue')
219 'shouldComponentUpdate-currentState green',
220 'shouldComponentUpdate-nextState blue',
@@ -226,6 +225,13 @@ describe('ReactCompositeComponent-state', () => {
225 'componentDidUpdate-prevState green',
226 'setFavoriteColor blue',
227 'commit blue',
228 + ]);
229 + await act(() => {
230 + testComponentInstance.forceUpdate(
231 + testComponentInstance.peekAtCallback('forceUpdate'),
232 + );
233 + });
234 + assertLog([
235 // forceUpdate()
236 'componentWillUpdate-currentState blue',
237 'componentWillUpdate-nextState blue',
@@ -234,7 +240,12 @@ describe('ReactCompositeComponent-state', () => {
240 'componentDidUpdate-prevState blue',
241 'forceUpdate blue',
242 'commit blue',
237 - // unmountComponent()
243 + ]);
244 +
245 + root.unmount();
246 +
247 + assertLog([
248 + // unmount()
249 // state is available within `componentWillUnmount()`
250 'componentWillUnmount blue',
251 ]);
@@ -375,13 +386,13 @@ describe('ReactCompositeComponent-state', () => {
386 await act(() => {
387 root.render(<Parent />);
388 });
389 +
390 + assertLog(['parent render one', 'child render one']);
391 await act(() => {
392 root.render(<Parent />);
393 });
394
395 assertLog([
383 - 'parent render one',
384 - 'child render one',
396 'parent render one',
397 'child componentWillReceiveProps one',
398 'child componentWillReceiveProps done one',