mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
feat: support string not in Text error
This commit is contained in:
@@ -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(() =>
|
||||
|
||||
@@ -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 };
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user