Skip to content

fix: immediate setState in useEffect for unstable_validateStringsRenderedWithinText #1612

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
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
46 changes: 46 additions & 0 deletions src/__tests__/render-string-validation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,49 @@ test('should throw when rendering string in a View in a Text', () => {
`${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.`,
);
});

const UseEffectComponent = () => {
const [showText, setShowText] = React.useState(false);

React.useEffect(() => {
setShowText(true);
}, []);

if (!showText) {
return <Text>Text is hidden</Text>;
}

return (
<View>
<Text>Text is visible</Text>
</View>
);
};

test('should render immediate setState in useEffect properly', async () => {
render(<UseEffectComponent />, { unstable_validateStringsRenderedWithinText: true });

expect(await screen.findByText('Text is visible')).toBeTruthy();
});

const InvalidUseEffectComponent = () => {
const [showText, setShowText] = React.useState(false);

React.useEffect(() => {
setShowText(true);
}, []);

if (!showText) {
return <Text>Text is hidden</Text>;
}

return <View>Text is visible</View>;
};

test('should throw properly for immediate setState in useEffect', () => {
expect(() =>
render(<InvalidUseEffectComponent />, { unstable_validateStringsRenderedWithinText: true }),
).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "Text is visible" string within a <View> component.`,
);
});
12 changes: 7 additions & 5 deletions src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import debugShallow from './helpers/debug-shallow';
import { configureHostComponentNamesIfNeeded } from './helpers/host-component-names';
import { validateStringsRenderedWithinText } from './helpers/string-validation';
import { renderWithAct } from './render-act';
import { setRenderResult, screen } from './screen';
import { setRenderResult } from './screen';
import { getQueriesForElement } from './within';

export interface RenderOptions {
Expand Down Expand Up @@ -64,11 +64,12 @@ function renderWithStringValidation<T>(
component: React.ReactElement<T>,
options: Omit<RenderOptions, 'unstable_validateStringsRenderedWithinText'> = {},
) {
let renderer: ReactTestRenderer;
const { wrapper: Wrapper, ...testRendererOptions } = options ?? {};

const handleRender: React.ProfilerProps['onRender'] = (_, phase) => {
if (phase === 'update') {
validateStringsRenderedWithinText(screen.toJSON());
const handleRender: React.ProfilerOnRenderCallback = (_, phase) => {
if (renderer && phase === 'update') {
validateStringsRenderedWithinText(renderer.toJSON());
}
};

Expand All @@ -78,7 +79,8 @@ function renderWithStringValidation<T>(
</Profiler>
);

const renderer = renderWithAct(wrap(component), testRendererOptions);
renderer = renderWithAct(wrap(component), testRendererOptions);

validateStringsRenderedWithinText(renderer.toJSON());

return buildRenderResult(renderer, wrap);
Expand Down