Skip to content

Commit 59586b3

Browse files
committed
feat: flag for enabling concurrent rendering
1 parent c56b223 commit 59586b3

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

jest-setup.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { resetToDefaults } from './src/pure';
1+
import { resetToDefaults, configure } from './src/pure';
22
import './src/matchers/extend-expect';
33

44
beforeEach(() => {
55
resetToDefaults();
6+
configure({ legacyRoot: false });
67
});

src/__tests__/config.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { getConfig, configure, resetToDefaults, configureInternal } from '../config';
22

3+
beforeEach(() => {
4+
resetToDefaults();
5+
});
6+
37
test('getConfig() returns existing configuration', () => {
48
expect(getConfig().asyncUtilTimeout).toEqual(1000);
59
expect(getConfig().defaultIncludeHiddenElements).toEqual(false);
@@ -12,6 +16,7 @@ test('configure() overrides existing config values', () => {
1216
asyncUtilTimeout: 5000,
1317
defaultDebugOptions: { message: 'debug message' },
1418
defaultIncludeHiddenElements: false,
19+
legacyRoot: true,
1520
});
1621
});
1722

src/config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export type Config = {
1313

1414
/** Default options for `debug` helper. */
1515
defaultDebugOptions?: Partial<DebugOptions>;
16+
17+
/**
18+
* Only works if used with React 18.
19+
* Set to `true` if you want to force synchronous rendering.
20+
* Otherwise `render` will default to concurrent React if available.
21+
*/
22+
legacyRoot: boolean;
1623
};
1724

1825
export type ConfigAliasOptions = {
@@ -37,6 +44,7 @@ export type InternalConfig = Config & {
3744
const defaultConfig: InternalConfig = {
3845
asyncUtilTimeout: 1000,
3946
defaultIncludeHiddenElements: false,
47+
legacyRoot: true,
4048
};
4149

4250
let config = { ...defaultConfig };

src/render.tsx

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { ReactTestInstance, ReactTestRenderer } from 'react-test-renderer';
1+
import type {
2+
ReactTestInstance,
3+
ReactTestRenderer,
4+
TestRendererOptions,
5+
} from 'react-test-renderer';
26
import * as React from 'react';
37
import { Profiler } from 'react';
48
import act from './act';
@@ -14,7 +18,19 @@ import { setRenderResult } from './screen';
1418
import { getQueriesForElement } from './within';
1519

1620
export interface RenderOptions {
21+
/**
22+
* Pass a React Component as the wrapper option to have it rendered around the inner element. This is most useful for creating
23+
* reusable custom render functions for common data providers.
24+
*/
1725
wrapper?: React.ComponentType<any>;
26+
27+
/**
28+
* Only works if used with React 18.
29+
* Set to `true` if you want to force synchronous rendering.
30+
* Otherwise `render` will default to concurrent React if available.
31+
*/
32+
legacyRoot?: boolean | undefined;
33+
1834
createNodeMock?: (element: React.ReactElement) => unknown;
1935
unstable_validateStringsRenderedWithinText?: boolean;
2036
}
@@ -39,11 +55,18 @@ export function renderInternal<T>(
3955
) {
4056
const {
4157
wrapper: Wrapper,
58+
legacyRoot,
4259
detectHostComponentNames = true,
4360
unstable_validateStringsRenderedWithinText,
44-
...testRendererOptions
61+
...rest
4562
} = options || {};
4663

64+
const testRendererOptions: TestRendererOptions = {
65+
// @ts-expect-error incomplete typing on RTR package
66+
unstable_isConcurrent: !(legacyRoot ?? getConfig().legacyRoot),
67+
...rest,
68+
};
69+
4770
if (detectHostComponentNames) {
4871
configureHostComponentNamesIfNeeded();
4972
}

0 commit comments

Comments
 (0)