-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathrender-string-validation.test.tsx
204 lines (171 loc) · 5.49 KB
/
render-string-validation.test.tsx
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as React from 'react';
import { Pressable, Text, View } from 'react-native';
import { fireEvent, render, screen } from '..';
// eslint-disable-next-line no-console
const originalConsoleError = console.error;
const VALIDATION_ERROR =
'Invariant Violation: Text strings must be rendered within a <Text> component';
const PROFILER_ERROR = 'The above error occurred in the <Profiler> component';
beforeEach(() => {
// eslint-disable-next-line no-console
console.error = (errorMessage: string) => {
if (!errorMessage.includes(PROFILER_ERROR)) {
originalConsoleError(errorMessage);
}
};
});
afterEach(() => {
// eslint-disable-next-line no-console
console.error = originalConsoleError;
});
test('should throw when rendering a string outside a text component', () => {
expect(() =>
render(<View>hello</View>, {
unstable_validateStringsRenderedWithinText: true,
}),
).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.`,
);
});
test('should throw an error when rerendering with text outside of Text component', () => {
render(<View />, {
unstable_validateStringsRenderedWithinText: true,
});
expect(() => screen.rerender(<View>hello</View>)).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.`,
);
});
const InvalidTextAfterPress = () => {
const [showText, setShowText] = React.useState(false);
if (!showText) {
return (
<Pressable onPress={() => setShowText(true)}>
<Text>Show text</Text>
</Pressable>
);
}
return <View>text rendered outside text component</View>;
};
test('should throw an error when strings are rendered outside Text', () => {
render(<InvalidTextAfterPress />, {
unstable_validateStringsRenderedWithinText: true,
});
expect(() => fireEvent.press(screen.getByText('Show text'))).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "text rendered outside text component" string within a <View> component.`,
);
});
test('should not throw for texts nested in fragments', () => {
expect(() =>
render(
<Text>
<>hello</>
</Text>,
{ unstable_validateStringsRenderedWithinText: true },
),
).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(() =>
render(
<View>
<Text>hello</Text>
hello
</View>,
{ unstable_validateStringsRenderedWithinText: true },
),
).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.`,
);
});
test(`should throw when a string is rendered within a fragment rendered outside a Text`, () => {
expect(() =>
render(
<View>
<>hello</>
</View>,
{ unstable_validateStringsRenderedWithinText: true },
),
).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.`,
);
});
test('should throw if a number is rendered outside a text', () => {
expect(() =>
render(<View>0</View>, { unstable_validateStringsRenderedWithinText: true }),
).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "0" string within a <View> component.`,
);
});
const Trans = ({ i18nKey }: { i18nKey: string }) => <>{i18nKey}</>;
test('should throw with components returning string value not rendered in Text', () => {
expect(() =>
render(
<View>
<Trans i18nKey="hello" />
</View>,
{ unstable_validateStringsRenderedWithinText: true },
),
).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "hello" string within a <View> component.`,
);
});
test('should not throw with components returning string value rendered in Text', () => {
expect(() =>
render(
<Text>
<Trans i18nKey="hello" />
</Text>,
{ unstable_validateStringsRenderedWithinText: true },
),
).not.toThrow();
});
test('should throw when rendering string in a View in a Text', () => {
expect(() =>
render(
<Text>
<View>hello</View>
</Text>,
{ unstable_validateStringsRenderedWithinText: true },
),
).toThrow(
`${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.`,
);
});