Skip to content

Commit 16b9e49

Browse files
refactor: pass through Test Renderer options (#1594)
1 parent ec032b0 commit 16b9e49

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/render-act.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import act from './act';
44

55
export function renderWithAct(
66
component: React.ReactElement,
7-
options?: TestRendererOptions,
7+
options?: Partial<TestRendererOptions>,
88
): ReactTestRenderer {
99
let renderer: ReactTestRenderer;
1010

1111
// This will be called synchronously.
1212
void act(() => {
13+
// @ts-expect-error TestRenderer.create is not typed correctly
1314
renderer = TestRenderer.create(component, options);
1415
});
1516

16-
// @ts-ignore act is synchronous, so renderer is already initialised here
17+
// @ts-ignore act is synchronous, so renderer is already initialized here
1718
return renderer;
1819
}

src/render.tsx

+14-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getQueriesForElement } from './within';
1515

1616
export interface RenderOptions {
1717
wrapper?: React.ComponentType<any>;
18-
createNodeMock?: (element: React.ReactElement) => any;
18+
createNodeMock?: (element: React.ReactElement) => unknown;
1919
unstable_validateStringsRenderedWithinText?: boolean;
2020
}
2121

@@ -35,38 +35,37 @@ export interface RenderInternalOptions extends RenderOptions {
3535

3636
export function renderInternal<T>(
3737
component: React.ReactElement<T>,
38-
{
38+
options?: RenderInternalOptions,
39+
) {
40+
const {
3941
wrapper: Wrapper,
40-
createNodeMock,
41-
unstable_validateStringsRenderedWithinText,
4242
detectHostComponentNames = true,
43-
}: RenderInternalOptions = {},
44-
) {
43+
unstable_validateStringsRenderedWithinText,
44+
...testRendererOptions
45+
} = options || {};
46+
4547
if (detectHostComponentNames) {
4648
configureHostComponentNamesIfNeeded();
4749
}
4850

4951
if (unstable_validateStringsRenderedWithinText) {
5052
return renderWithStringValidation(component, {
5153
wrapper: Wrapper,
52-
createNodeMock,
54+
...testRendererOptions,
5355
});
5456
}
5557

5658
const wrap = (element: React.ReactElement) => (Wrapper ? <Wrapper>{element}</Wrapper> : element);
57-
58-
const renderer = renderWithAct(wrap(component), createNodeMock ? { createNodeMock } : undefined);
59-
59+
const renderer = renderWithAct(wrap(component), testRendererOptions);
6060
return buildRenderResult(renderer, wrap);
6161
}
6262

6363
function renderWithStringValidation<T>(
6464
component: React.ReactElement<T>,
65-
{
66-
wrapper: Wrapper,
67-
createNodeMock,
68-
}: Omit<RenderOptions, 'unstable_validateStringsRenderedWithinText'> = {},
65+
options: Omit<RenderOptions, 'unstable_validateStringsRenderedWithinText'> = {},
6966
) {
67+
const { wrapper: Wrapper, ...testRendererOptions } = options ?? {};
68+
7069
const handleRender: React.ProfilerProps['onRender'] = (_, phase) => {
7170
if (phase === 'update') {
7271
validateStringsRenderedWithinText(screen.toJSON());
@@ -79,7 +78,7 @@ function renderWithStringValidation<T>(
7978
</Profiler>
8079
);
8180

82-
const renderer = renderWithAct(wrap(component), createNodeMock ? { createNodeMock } : undefined);
81+
const renderer = renderWithAct(wrap(component), testRendererOptions);
8382
validateStringsRenderedWithinText(renderer.toJSON());
8483

8584
return buildRenderResult(renderer, wrap);

typings/index.flow.js

+3
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ declare module '@testing-library/react-native' {
348348
wrapper?: React.ComponentType<any>;
349349
createNodeMock?: (element: React.Element<any>) => any;
350350
unstable_validateStringsRenderedWithinText?: boolean;
351+
unstable_isConcurrent?: boolean;
352+
unstable_strictMode?: boolean;
353+
unstable_concurrentUpdatesByDefault?: boolean;
351354
}
352355

353356
declare export var render: (

website/docs/Render.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ This option allows you to wrap the tested component, passed as the first option
3838
#### `createNodeMock` option
3939

4040
```ts
41-
createNodeMock?: (element: React.Element<any>) => any,
41+
createNodeMock?: (element: React.Element) => unknown,
4242
```
4343

4444
This option allows you to pass `createNodeMock` option to `ReactTestRenderer.create()` method in order to allow for custom mock refs. You can learn more about this option from [React Test Renderer documentation](https://reactjs.org/docs/test-renderer.html#ideas).

0 commit comments

Comments
 (0)