Skip to content

Commit b16334c

Browse files
committed
.
1 parent 1416ccd commit b16334c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/__tests__/act.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import { Text } from 'react-native';
33

44
import { act, fireEvent, render, screen } from '..';
5+
import { getIsReactActEnvironment } from '../act';
56

67
type UseEffectProps = { callback(): void };
78
const UseEffect = ({ callback }: UseEffectProps) => {
@@ -48,3 +49,36 @@ test('should be able to await act', async () => {
4849
test('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

Comments
 (0)