forked from callstack/react-native-testing-library
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringValidation.ts
36 lines (30 loc) · 991 Bytes
/
stringValidation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { ReactTestRendererNode } from 'react-test-renderer';
export const validateStringsRenderedWithinText = (
rendererJSON: ReactTestRendererNode | Array<ReactTestRendererNode> | null
) => {
if (!rendererJSON) return;
if (Array.isArray(rendererJSON)) {
rendererJSON.forEach(validateStringsRenderedWithinTextForNode);
return;
}
return validateStringsRenderedWithinTextForNode(rendererJSON);
};
const validateStringsRenderedWithinTextForNode = (
node: ReactTestRendererNode
) => {
if (typeof node === 'string') {
return;
}
if (node.type !== 'Text') {
node.children?.forEach((child) => {
if (typeof child === 'string') {
throw new Error(
`Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "${child}" string within a <${node.type}> component.`
);
}
});
}
if (node.children) {
node.children.forEach(validateStringsRenderedWithinTextForNode);
}
};