diff --git a/src/components/View/__tests__/index-test.js b/src/components/View/__tests__/index-test.js index 4a604364..ef774148 100644 --- a/src/components/View/__tests__/index-test.js +++ b/src/components/View/__tests__/index-test.js @@ -12,10 +12,18 @@ describe('components/View', () => { }); }); - test('prop "children"', () => { - const children = ; - const component = shallow({children}); - expect(component.contains(children)).toEqual(true); + describe('prop "children"', () => { + test('text node throws error', () => { + const children = 'hello'; + const render = () => shallow({children}); + expect(render).toThrow(); + }); + + test('non-text is rendered', () => { + const children = ; + const component = shallow({children}); + expect(component.contains(children)).toEqual(true); + }); }); describe('prop "hitSlop"', () => { diff --git a/src/components/View/index.js b/src/components/View/index.js index eab4a333..a8482fbb 100644 --- a/src/components/View/index.js +++ b/src/components/View/index.js @@ -11,6 +11,7 @@ import applyLayout from '../../modules/applyLayout'; import applyNativeMethods from '../../modules/applyNativeMethods'; import { bool } from 'prop-types'; import createElement from '../../modules/createElement'; +import invariant from 'fbjs/lib/invariant'; import StyleSheet from '../../apis/StyleSheet'; import ViewPropTypes from './ViewPropTypes'; import React, { Component } from 'react'; @@ -49,6 +50,13 @@ class View extends Component { ...otherProps } = this.props; + if (process.env.NODE_ENV !== 'production') { + invariant( + typeof this.props.children !== 'string', + 'A text node cannot be a child of a ' + ); + } + const { isInAParentText } = this.context; otherProps.style = [styles.initial, isInAParentText && styles.inline, style];