@@ -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,36 @@ 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+ try {
59+ await act ( ( ) => {
60+ throw testError ;
61+ } ) ;
62+ // Should not reach here
63+ expect ( true ) . toBe ( false ) ;
64+ } catch ( error ) {
65+ expect ( error ) . toBe ( testError ) ;
66+ }
67+
68+ // Verify the act environment was restored even after error
69+ expect ( getIsReactActEnvironment ( ) ) . toBe ( previousEnvironment ) ;
70+ } ) ;
71+
72+ test ( 'should restore act environment when callback returns non-promise value' , async ( ) => {
73+ const previousEnvironment = getIsReactActEnvironment ( ) ;
74+
75+ // Call act with a callback that returns a non-promise value
76+ // This tests the else branch in withGlobalActEnvironment
77+ const result = await act ( ( ) => {
78+ return 42 ;
79+ } ) ;
80+
81+ expect ( result ) . toBe ( 42 ) ;
82+ // Verify the act environment was restored
83+ expect ( getIsReactActEnvironment ( ) ) . toBe ( previousEnvironment ) ;
84+ } ) ;
0 commit comments