@@ -2,6 +2,7 @@ import * as React from 'react';
22import { Text } from 'react-native' ;
33
44import { act , fireEvent , render , screen } from '..' ;
5+ import { getIsReactActEnvironment } from '../act' ;
56
67type UseEffectProps = { callback ( ) : void } ;
78const UseEffect = ( { callback } : UseEffectProps ) => {
@@ -48,3 +49,32 @@ test('should be able to await act', async () => {
4849test ( 'should be able to await act when promise rejects' , async ( ) => {
4950 await expect ( act ( ( ) => Promise . reject ( 'error' ) ) ) . rejects . toBe ( 'error' ) ;
5051} ) ;
52+
53+ test ( 'should restore act environment when callback throws synchronously' , async ( ) => {
54+ const previousEnvironment = getIsReactActEnvironment ( ) ;
55+
56+ const testError = new Error ( 'Synchronous error in act' ) ;
57+
58+ await expect (
59+ act ( ( ) => {
60+ throw testError ;
61+ } ) ,
62+ ) . rejects . toBe ( testError ) ;
63+
64+ // Verify the act environment was restored even after error
65+ expect ( getIsReactActEnvironment ( ) ) . toBe ( previousEnvironment ) ;
66+ } ) ;
67+
68+ test ( 'should restore act environment when callback returns non-promise value' , async ( ) => {
69+ const previousEnvironment = getIsReactActEnvironment ( ) ;
70+
71+ // Call act with a callback that returns a non-promise value
72+ // This tests the else branch in withGlobalActEnvironment
73+ const result = await act ( ( ) => {
74+ return 42 ;
75+ } ) ;
76+
77+ expect ( result ) . toBe ( 42 ) ;
78+ // Verify the act environment was restored
79+ expect ( getIsReactActEnvironment ( ) ) . toBe ( previousEnvironment ) ;
80+ } ) ;
0 commit comments