Skip to content

Commit 4b46293

Browse files
chore: more tests (#1857)
1 parent e3d8519 commit 4b46293

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/__tests__/act.test.tsx

Lines changed: 30 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,32 @@ 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+
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+
});

src/__tests__/wait-for.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,34 @@ test('waitFor throws if expectation is not a function', async () => {
316316
waitFor('not a function'),
317317
).rejects.toThrowErrorMatchingInlineSnapshot(`"Received \`expectation\` arg must be a function"`);
318318
});
319+
320+
test.each([false, true])(
321+
'waitFor throws clear error when switching from fake timers to real timers (legacyFakeTimers = %s)',
322+
async (legacyFakeTimers) => {
323+
jest.useFakeTimers({ legacyFakeTimers });
324+
325+
const waitForPromise = waitFor(() => {
326+
// Switch to real timers during waitFor - this should trigger an error
327+
jest.useRealTimers();
328+
throw new Error('test');
329+
});
330+
331+
await expect(waitForPromise).rejects.toThrow(
332+
'Changed from using fake timers to real timers while using waitFor',
333+
);
334+
},
335+
);
336+
337+
test('waitFor throws clear error when switching from real timers to fake timers', async () => {
338+
jest.useRealTimers();
339+
340+
const waitForPromise = waitFor(() => {
341+
// Switch to fake timers during waitFor - this should trigger an error
342+
jest.useFakeTimers();
343+
throw new Error('test');
344+
});
345+
346+
await expect(waitForPromise).rejects.toThrow(
347+
'Changed from using real timers to fake timers while using waitFor',
348+
);
349+
});

0 commit comments

Comments
 (0)