[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

View File

@@ -13,9 +13,20 @@ describe('components/View', () => {
});
describe('prop "children"', () => {
test('text node throws error', () => {
const children = 'hello';
const render = () => shallow(<View>{children}</View>);
test('text node throws error (single)', () => {
const render = () => shallow(<View>'hello'</View>);
expect(render).toThrow();
});
test('text node throws error (array)', () => {
const render = () =>
shallow(
<View>
<View />
'hello'
<View />
</View>
);
expect(render).toThrow();
});

View File

@@ -51,10 +51,9 @@ class View extends Component {
} = this.props;
if (process.env.NODE_ENV !== 'production') {
invariant(
typeof this.props.children !== 'string',
'A text node cannot be a child of a <View>'
);
React.Children.toArray(this.props.children).forEach(item => {
invariant(typeof item !== 'string', 'A text node cannot be a child of a <View>');
});
}
const { isInAParentText } = this.context;

View File

@@ -2,7 +2,7 @@
import normalizeNativeEvent from '..';
const normalizeEvent = (nativeEvent) => {
const normalizeEvent = nativeEvent => {
const result = normalizeNativeEvent(nativeEvent);
result.timestamp = 1496876171255;
if (result.changedTouches && result.changedTouches[0]) {
@@ -12,7 +12,7 @@ const normalizeEvent = (nativeEvent) => {
result.touches[0].timestamp = 1496876171255;
}
return result;
}
};
describe('modules/normalizeNativeEvent', () => {
describe('mouse events', () => {