feat: support string not in Text error

This commit is contained in:
Maciej Jastrzebski
2024-10-15 12:46:18 +02:00
parent 56715a9bbb
commit 26992a1ac3
4 changed files with 13 additions and 22 deletions
@@ -88,7 +88,7 @@ exports[`debug changing component: bananaFresh button message should now be "fre
<Text
testID="bananaFresh"
>
not fresh
fresh
</Text>
<TextInput
placeholder="Add custom freshness"
@@ -367,12 +367,6 @@ exports[`debug: another custom message 1`] = `
</View>"
`;
exports[`debug: shallow 1`] = `
"another custom message
"
`;
exports[`debug: with message 1`] = `
"my custom message
@@ -78,9 +78,9 @@ test('should not throw for texts nested in fragments', () => {
).not.toThrow();
});
test('should not throw if option validateRenderedString is false', () => {
expect(() => render(<View>hello</View>)).not.toThrow();
});
// test('should not throw if option validateRenderedString is false', () => {
// expect(() => render(<View>hello</View>)).not.toThrow();
// });
test(`should throw when one of the children is a text and the parent is not a Text component`, () => {
expect(() =>
+6 -9
View File
@@ -34,6 +34,7 @@ export type TextInstance = {
};
type HostContext = {
elementType: string;
isInsideText: boolean;
};
@@ -130,7 +131,9 @@ const hostConfig = {
_internalHandle: OpaqueHandle,
): TextInstance {
if (!hostContext.isInsideText) {
throw new Error(`Text string "${text}" must be rendered inside <Text> component`);
throw new Error(
`Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "${text}" string within a <${hostContext.elementType}> component.`,
);
}
return {
@@ -207,7 +210,7 @@ const hostConfig = {
* This method happens **in the render phase**. Do not mutate the tree from it.
*/
getRootHostContext(_rootContainer: Container): HostContext | null {
return { isInsideText: false };
return { elementType: 'ROOT', isInsideText: false };
},
/**
@@ -224,14 +227,8 @@ const hostConfig = {
type: Type,
_rootContainer: Container,
): HostContext {
const previousIsInsideText = parentHostContext.isInsideText;
const isInsideText = type === 'Text';
if (previousIsInsideText === isInsideText) {
return parentHostContext;
}
return { isInsideText };
return { elementType: type, isInsideText };
},
/**
+3 -3
View File
@@ -20,15 +20,15 @@ test('throws when rendering string outside of Text', () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
expect(() => render(<View>Hello</View>)).toThrowErrorMatchingInlineSnapshot(
`"Text string "Hello" must be rendered inside <Text> component"`,
`"Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "Hello" string within a <View> component."`,
);
expect(() => render(<Passthrough>Hello</Passthrough>)).toThrowErrorMatchingInlineSnapshot(
`"Text string "Hello" must be rendered inside <Text> component"`,
`"Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "Hello" string within a <ROOT> component."`,
);
expect(() => render(<>Hello</>)).toThrowErrorMatchingInlineSnapshot(
`"Text string "Hello" must be rendered inside <Text> component"`,
`"Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "Hello" string within a <ROOT> component."`,
);
jest.restoreAllMocks();