From 5cb09b1a9ecee742f013adcda1db89f03d133791 Mon Sep 17 00:00:00 2001 From: Nicolas Gallagher Date: Mon, 18 Sep 2017 13:28:51 -0700 Subject: [PATCH] [fix] disallow text node children of View Catch text nodes that are not the only child of View. Fixes a case missing from 0ad6ab948b4e296a85fa03ca1c837d6d1430e596. --- src/components/View/__tests__/index-test.js | 17 ++++++++++++++--- src/components/View/index.js | 7 +++---- .../__tests__/index-test.js | 4 ++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/components/View/__tests__/index-test.js b/src/components/View/__tests__/index-test.js index ef774148..92252606 100644 --- a/src/components/View/__tests__/index-test.js +++ b/src/components/View/__tests__/index-test.js @@ -13,9 +13,20 @@ describe('components/View', () => { }); describe('prop "children"', () => { - test('text node throws error', () => { - const children = 'hello'; - const render = () => shallow({children}); + test('text node throws error (single)', () => { + const render = () => shallow('hello'); + expect(render).toThrow(); + }); + + test('text node throws error (array)', () => { + const render = () => + shallow( + + + 'hello' + + + ); expect(render).toThrow(); }); diff --git a/src/components/View/index.js b/src/components/View/index.js index a8482fbb..17c0c532 100644 --- a/src/components/View/index.js +++ b/src/components/View/index.js @@ -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 ' - ); + React.Children.toArray(this.props.children).forEach(item => { + invariant(typeof item !== 'string', 'A text node cannot be a child of a '); + }); } const { isInAParentText } = this.context; diff --git a/src/modules/normalizeNativeEvent/__tests__/index-test.js b/src/modules/normalizeNativeEvent/__tests__/index-test.js index de07ff87..4f7aea59 100644 --- a/src/modules/normalizeNativeEvent/__tests__/index-test.js +++ b/src/modules/normalizeNativeEvent/__tests__/index-test.js @@ -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', () => {