Skip to content

refactor: pass through Test Renderer options #1594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/render-act.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import act from './act';

export function renderWithAct(
component: React.ReactElement,
options?: TestRendererOptions,
options?: Partial<TestRendererOptions>,
): ReactTestRenderer {
let renderer: ReactTestRenderer;

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

// @ts-ignore act is synchronous, so renderer is already initialised here
// @ts-ignore act is synchronous, so renderer is already initialized here
return renderer;
}
29 changes: 14 additions & 15 deletions src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getQueriesForElement } from './within';

export interface RenderOptions {
wrapper?: React.ComponentType<any>;
createNodeMock?: (element: React.ReactElement) => any;
createNodeMock?: (element: React.ReactElement) => unknown;
unstable_validateStringsRenderedWithinText?: boolean;
}

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

export function renderInternal<T>(
component: React.ReactElement<T>,
{
options?: RenderInternalOptions,
) {
const {
wrapper: Wrapper,
createNodeMock,
unstable_validateStringsRenderedWithinText,
detectHostComponentNames = true,
}: RenderInternalOptions = {},
) {
unstable_validateStringsRenderedWithinText,
...testRendererOptions
} = options || {};

if (detectHostComponentNames) {
configureHostComponentNamesIfNeeded();
}

if (unstable_validateStringsRenderedWithinText) {
return renderWithStringValidation(component, {
wrapper: Wrapper,
createNodeMock,
...testRendererOptions,
});
}

const wrap = (element: React.ReactElement) => (Wrapper ? <Wrapper>{element}</Wrapper> : element);

const renderer = renderWithAct(wrap(component), createNodeMock ? { createNodeMock } : undefined);

const renderer = renderWithAct(wrap(component), testRendererOptions);
return buildRenderResult(renderer, wrap);
}

function renderWithStringValidation<T>(
component: React.ReactElement<T>,
{
wrapper: Wrapper,
createNodeMock,
}: Omit<RenderOptions, 'unstable_validateStringsRenderedWithinText'> = {},
options: Omit<RenderOptions, 'unstable_validateStringsRenderedWithinText'> = {},
) {
const { wrapper: Wrapper, ...testRendererOptions } = options ?? {};

const handleRender: React.ProfilerProps['onRender'] = (_, phase) => {
if (phase === 'update') {
validateStringsRenderedWithinText(screen.toJSON());
Expand All @@ -79,7 +78,7 @@ function renderWithStringValidation<T>(
</Profiler>
);

const renderer = renderWithAct(wrap(component), createNodeMock ? { createNodeMock } : undefined);
const renderer = renderWithAct(wrap(component), testRendererOptions);
validateStringsRenderedWithinText(renderer.toJSON());

return buildRenderResult(renderer, wrap);
Expand Down
3 changes: 3 additions & 0 deletions typings/index.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ declare module '@testing-library/react-native' {
wrapper?: React.ComponentType<any>;
createNodeMock?: (element: React.Element<any>) => any;
unstable_validateStringsRenderedWithinText?: boolean;
unstable_isConcurrent?: boolean;
unstable_strictMode?: boolean;
unstable_concurrentUpdatesByDefault?: boolean;
}

declare export var render: (
Expand Down
2 changes: 1 addition & 1 deletion website/docs/Render.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ This option allows you to wrap the tested component, passed as the first option
#### `createNodeMock` option

```ts
createNodeMock?: (element: React.Element<any>) => any,
createNodeMock?: (element: React.Element) => unknown,
```

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).
Expand Down