Skip to content

detectHostComponentNames: render test tree inside act to avoid timer leaks #1371

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 4 commits into from
Mar 23, 2023
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
7 changes: 3 additions & 4 deletions src/helpers/host-component-names.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import * as React from 'react';
import { Text, TextInput, View } from 'react-native';
import TestRenderer from 'react-test-renderer';
import { configureInternal, getConfig, HostComponentNames } from '../config';
import { renderWithAct } from '../render-act';
import { getQueriesForElement } from '../within';

const userConfigErrorMessage = `There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly.
Expand Down Expand Up @@ -29,13 +29,12 @@ export function configureHostComponentNamesIfNeeded() {

function detectHostComponentNames(): HostComponentNames {
try {
const renderer = TestRenderer.create(
const renderer = renderWithAct(
<View>
<Text testID="text">Hello</Text>
<TextInput testID="textInput" />
</View>
);

const { getByTestId } = getQueriesForElement(renderer.root);
const textHostName = getByTestId('text').type;
const textInputHostName = getByTestId('textInput').type;
Expand Down
19 changes: 19 additions & 0 deletions src/render-act.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import TestRenderer from 'react-test-renderer';
import type {
ReactTestRenderer,
TestRendererOptions,
} from 'react-test-renderer';

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

TestRenderer.act(() => {
renderer = TestRenderer.create(component, options);
});

// @ts-ignore act is synchronous, so renderer is already initialised here
return renderer;
}
30 changes: 6 additions & 24 deletions src/render.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import TestRenderer from 'react-test-renderer';
import type { ReactTestInstance, ReactTestRenderer } from 'react-test-renderer';
import * as React from 'react';
import { Profiler } from 'react';
import act from './act';
import { addToCleanupQueue } from './cleanup';
import debugShallow from './helpers/debugShallow';
import debugDeep, { DebugOptions } from './helpers/debugDeep';
import { getQueriesForElement } from './within';
import { setRenderResult, screen } from './screen';
import { validateStringsRenderedWithinText } from './helpers/stringValidation';
import { getConfig } from './config';
import { getHostChildren } from './helpers/component-tree';
import debugDeep, { DebugOptions } from './helpers/debugDeep';
import debugShallow from './helpers/debugShallow';
import { configureHostComponentNamesIfNeeded } from './helpers/host-component-names';
import { validateStringsRenderedWithinText } from './helpers/stringValidation';
import { renderWithAct } from './render-act';
import { setRenderResult, screen } from './screen';
import { getQueriesForElement } from './within';

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

type TestRendererOptions = {
createNodeMock: (element: React.ReactElement) => any;
};

export type RenderResult = ReturnType<typeof render>;

/**
Expand Down Expand Up @@ -129,20 +125,6 @@ function buildRenderResult(
return result;
}

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

act(() => {
renderer = TestRenderer.create(component, options);
});

// @ts-ignore act is sync, so renderer is always initialised here
return renderer;
}

function updateWithAct(
renderer: ReactTestRenderer,
wrap: (innerElement: React.ReactElement) => React.ReactElement
Expand Down