Skip to content

Commit 06ed368

Browse files
committed
fake timers
1 parent 0441c84 commit 06ed368

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* eslint-disable jest/no-standalone-expect */
2+
import * as React from 'react';
3+
import { View } from 'react-native';
4+
import TestRenderer, { type ReactTestRenderer } from 'react-test-renderer';
5+
6+
import { configure, renderAsync, screen, within } from '..';
7+
8+
const isReact19 = React.version.startsWith('19.');
9+
const testGateReact19 = isReact19 ? test : test.skip;
10+
11+
jest.useFakeTimers();
12+
13+
configure({
14+
asyncUtilTimeout: 5000,
15+
});
16+
17+
function wait(delay: number) {
18+
return new Promise<void>((resolve) =>
19+
setTimeout(() => {
20+
resolve();
21+
}, delay),
22+
);
23+
}
24+
25+
function Suspending<T>({ promise }: { promise: Promise<T> }) {
26+
React.use(promise);
27+
return <View testID="view" />;
28+
}
29+
30+
testGateReact19('renderAsync supports components which can suspend', async () => {
31+
await renderAsync(
32+
<View>
33+
<React.Suspense fallback={<View testID="fallback" />}>
34+
<Suspending promise={wait(100)} />
35+
</React.Suspense>
36+
</View>,
37+
);
38+
39+
expect(screen.getByTestId('fallback')).toBeOnTheScreen();
40+
expect(await screen.findByTestId('view')).toBeOnTheScreen();
41+
});
42+
43+
testGateReact19('react test renderer supports components which can suspend', async () => {
44+
let renderer: ReactTestRenderer;
45+
46+
// eslint-disable-next-line require-await
47+
await React.act(async () => {
48+
renderer = TestRenderer.create(
49+
<View>
50+
<React.Suspense fallback={<View testID="fallback" />}>
51+
<Suspending promise={wait(100)} />
52+
</React.Suspense>
53+
</View>,
54+
);
55+
});
56+
57+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
58+
const view = within(renderer!.root);
59+
60+
expect(view.getByTestId('fallback')).toBeDefined();
61+
expect(await view.findByTestId('view')).toBeDefined();
62+
});

src/__tests__/react-suspense.test.tsx renamed to src/__tests__/render-async.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable jest/no-standalone-expect */
12
import * as React from 'react';
23
import { View } from 'react-native';
34
import TestRenderer, { type ReactTestRenderer } from 'react-test-renderer';
@@ -24,7 +25,7 @@ function Suspending<T>({ promise }: { promise: Promise<T> }) {
2425
return <View testID="view" />;
2526
}
2627

27-
testGateReact19('render supports components which can suspend', async () => {
28+
testGateReact19('renderAsync supports components which can suspend', async () => {
2829
await renderAsync(
2930
<View>
3031
<React.Suspense fallback={<View testID="fallback" />}>

0 commit comments

Comments
 (0)