[fix] disallow text node children of View

Catch text nodes that are not the only child of View. Fixes a case
missing from 0ad6ab948b.
This commit is contained in:
Nicolas Gallagher
2017-09-18 13:28:51 -07:00
parent c03cfdf8bd
commit 5cb09b1a9e
3 changed files with 19 additions and 9 deletions
+14 -3
View File
@@ -13,9 +13,20 @@ describe('components/View', () => {
}); });
describe('prop "children"', () => { describe('prop "children"', () => {
test('text node throws error', () => { test('text node throws error (single)', () => {
const children = 'hello'; const render = () => shallow(<View>'hello'</View>);
const render = () => shallow(<View>{children}</View>); expect(render).toThrow();
});
test('text node throws error (array)', () => {
const render = () =>
shallow(
<View>
<View />
'hello'
<View />
</View>
);
expect(render).toThrow(); expect(render).toThrow();
}); });
+3 -4
View File
@@ -51,10 +51,9 @@ class View extends Component {
} = this.props; } = this.props;
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
invariant( React.Children.toArray(this.props.children).forEach(item => {
typeof this.props.children !== 'string', invariant(typeof item !== 'string', 'A text node cannot be a child of a <View>');
'A text node cannot be a child of a <View>' });
);
} }
const { isInAParentText } = this.context; const { isInAParentText } = this.context;
@@ -2,7 +2,7 @@
import normalizeNativeEvent from '..'; import normalizeNativeEvent from '..';
const normalizeEvent = (nativeEvent) => { const normalizeEvent = nativeEvent => {
const result = normalizeNativeEvent(nativeEvent); const result = normalizeNativeEvent(nativeEvent);
result.timestamp = 1496876171255; result.timestamp = 1496876171255;
if (result.changedTouches && result.changedTouches[0]) { if (result.changedTouches && result.changedTouches[0]) {
@@ -12,7 +12,7 @@ const normalizeEvent = (nativeEvent) => {
result.touches[0].timestamp = 1496876171255; result.touches[0].timestamp = 1496876171255;
} }
return result; return result;
} };
describe('modules/normalizeNativeEvent', () => { describe('modules/normalizeNativeEvent', () => {
describe('mouse events', () => { describe('mouse events', () => {