From 21e0e3711b6a3e59c6e6052332c2d63c08b54dc3 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Thu, 9 Mar 2017 15:51:10 -0800 Subject: [PATCH 01/11] Use explicit null checks in ReactFiberUpdateQueue Follow-up to #8978 --- .../shared/fiber/ReactFiberUpdateQueue.js | 66 ++++++++++++------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js index a246436fff..03e6c5e5ce 100644 --- a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js +++ b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js @@ -90,7 +90,7 @@ function comparePriority(a: PriorityLevel, b: PriorityLevel): number { // Ensures that a fiber has an update queue, creating a new one if needed. // Returns the new or existing queue. function ensureUpdateQueue(fiber: Fiber): UpdateQueue { - if (fiber.updateQueue) { + if (fiber.updateQueue !== null) { // We already have an update queue. return fiber.updateQueue; } @@ -123,13 +123,15 @@ function cloneUpdateQueue( workInProgress: Fiber, ): UpdateQueue | null { const currentQueue = current.updateQueue; - if (!currentQueue) { + if (currentQueue === null) { // The source fiber does not have an update queue. workInProgress.updateQueue = null; return null; } // If the alternate already has a queue, reuse the previous object. - const altQueue = workInProgress.updateQueue || {}; + const altQueue = workInProgress.updateQueue !== null + ? workInProgress.updateQueue + : {}; altQueue.first = currentQueue.first; altQueue.last = currentQueue.last; @@ -156,8 +158,13 @@ function cloneUpdate(update: Update): Update { }; } -function insertUpdateIntoQueue(queue, update, insertAfter, insertBefore) { - if (insertAfter) { +function insertUpdateIntoQueue( + queue: UpdateQueue, + update: Update, + insertAfter: Update | null, + insertBefore: Update | null, +) { + if (insertAfter !== null) { insertAfter.next = update; } else { // This is the first item in the queue. @@ -165,7 +172,7 @@ function insertUpdateIntoQueue(queue, update, insertAfter, insertBefore) { queue.first = update; } - if (insertBefore) { + if (insertBefore !== null) { update.next = insertBefore; } else { // This is the last item in the queue. @@ -180,7 +187,8 @@ function findInsertionPosition(queue, update): Update | null { let insertAfter = null; let insertBefore = null; if ( - queue.last && comparePriority(queue.last.priorityLevel, priorityLevel) <= 0 + queue.last !== null && + comparePriority(queue.last.priorityLevel, priorityLevel) <= 0 ) { // Fast path for the common case where the update should be inserted at // the end of the queue. @@ -188,7 +196,7 @@ function findInsertionPosition(queue, update): Update | null { } else { insertBefore = queue.first; while ( - insertBefore && + insertBefore !== null && comparePriority(insertBefore.priorityLevel, priorityLevel) <= 0 ) { insertAfter = insertBefore; @@ -229,11 +237,13 @@ function findInsertionPosition(queue, update): Update | null { // If the update is cloned, it returns the cloned update. function insertUpdate(fiber: Fiber, update: Update): Update | null { const queue1 = ensureUpdateQueue(fiber); - const queue2 = fiber.alternate ? ensureUpdateQueue(fiber.alternate) : null; + const queue2 = fiber.alternate !== null + ? ensureUpdateQueue(fiber.alternate) + : null; // Warn if an update is scheduled from inside an updater function. if (__DEV__) { - if (queue1.isProcessing || (queue2 && queue2.isProcessing)) { + if (queue1.isProcessing || (queue2 !== null && queue2.isProcessing)) { warning( false, 'An update (setState, replaceState, or forceUpdate) was scheduled ' + @@ -246,9 +256,11 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null { // Find the insertion position in the first queue. const insertAfter1 = findInsertionPosition(queue1, update); - const insertBefore1 = insertAfter1 ? insertAfter1.next : queue1.first; + const insertBefore1 = insertAfter1 !== null + ? insertAfter1.next + : queue1.first; - if (!queue2) { + if (queue2 === null) { // If there's no alternate queue, there's nothing else to do but insert. insertUpdateIntoQueue(queue1, update, insertAfter1, insertBefore1); return null; @@ -256,7 +268,9 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null { // If there is an alternate queue, find the insertion position. const insertAfter2 = findInsertionPosition(queue2, update); - const insertBefore2 = insertAfter2 ? insertAfter2.next : queue2.first; + const insertBefore2 = insertAfter2 !== null + ? insertAfter2.next + : queue2.first; // Now we can insert into the first queue. This must come after finding both // insertion positions because it mutates the list. @@ -273,10 +287,10 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null { // queue, it also inserted into the alternate. All we need to do is update // the alternate queue's `first` and `last` pointers, in case they // have changed. - if (!insertAfter2) { + if (insertAfter2 === null) { queue2.first = update; } - if (!insertBefore2) { + if (insertBefore2 === null) { queue2.last = null; } } @@ -341,7 +355,7 @@ function addForceUpdate( exports.addForceUpdate = addForceUpdate; function getPendingPriority(queue: UpdateQueue): PriorityLevel { - return queue.first ? queue.first.priorityLevel : NoWork; + return queue.first !== null ? queue.first.priorityLevel : NoWork; } exports.getPendingPriority = getPendingPriority; @@ -351,7 +365,7 @@ function addTopLevelUpdate( callback: Callback | null, priorityLevel: PriorityLevel, ): void { - const isTopLevelUnmount = !!(partialState && partialState.element === null); + const isTopLevelUnmount = partialState.element === null; const update = { priorityLevel, @@ -368,13 +382,15 @@ function addTopLevelUpdate( // Drop all updates that are lower-priority, so that the tree is not // remounted. We need to do this for both queues. const queue1 = fiber.updateQueue; - const queue2 = fiber.alternate && fiber.alternate.updateQueue; + const queue2 = fiber.alternate !== null + ? fiber.alternate.updateQueue + : null; - if (queue1 && update.next) { + if (queue1 !== null && update.next !== null) { update.next = null; queue1.last = update; } - if (queue2 && update2 && update2.next) { + if (queue2 !== null && update2 !== null && update2.next !== null) { update2.next = null; queue2.last = update; } @@ -414,12 +430,14 @@ function beginUpdateQueue( let dontMutatePrevState = true; let callbackList = null; let update = queue.first; - while (update && comparePriority(update.priorityLevel, priorityLevel) <= 0) { + while ( + update !== null && comparePriority(update.priorityLevel, priorityLevel) <= 0 + ) { // Remove each update from the queue right before it is processed. That way // if setState is called from inside an updater function, the new update // will be inserted in the correct position. queue.first = update.next; - if (!queue.first) { + if (queue.first === null) { queue.last = null; } @@ -456,7 +474,7 @@ function beginUpdateQueue( queue.callbackList = callbackList; - if (!queue.first && !callbackList && !queue.hasForceUpdate) { + if (queue.first === null && callbackList === null && !queue.hasForceUpdate) { // The queue is empty and there are no callbacks. We can reset it. workInProgress.updateQueue = null; } @@ -475,7 +493,7 @@ function commitCallbacks( context: mixed, ) { const callbackList = queue.callbackList; - if (!callbackList) { + if (callbackList === null) { return; } for (let i = 0; i < callbackList.length; i++) { From bc30ef9842a5c2719828278aa8aa2e4df9575cda Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Thu, 9 Mar 2017 15:55:07 -0800 Subject: [PATCH 02/11] Work around loose typing for fiber.updateQueue The updateQueue field is currently an any type. Check the fiber's tag so that only UpdateQueues are passed to getPendingPriority. --- src/renderers/shared/fiber/ReactFiberScheduler.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/renderers/shared/fiber/ReactFiberScheduler.js b/src/renderers/shared/fiber/ReactFiberScheduler.js index 290efb7cab..29b74134ec 100644 --- a/src/renderers/shared/fiber/ReactFiberScheduler.js +++ b/src/renderers/shared/fiber/ReactFiberScheduler.js @@ -571,7 +571,13 @@ module.exports = function( // Check for pending update priority. This is usually null so it shouldn't // be a perf issue. const queue = workInProgress.updateQueue; - if (queue !== null) { + const tag = workInProgress.tag; + if ( + queue !== null && + // TODO: Revisit once updateQueue is typed properly to distinguish between + // update payloads for host components and update queues for composites + (tag === ClassComponent || tag === HostRoot) + ) { newPriority = getPendingPriority(queue); } From 9c7cf20dd56a42fe417e6caa264e75058bf92911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Thu, 16 Mar 2017 15:29:06 -0700 Subject: [PATCH 03/11] Ignore event listener extraction on numeric text components (#9194) In ReactDOM we don't dispatch events in the synthetic event system on to text nodes. We back up one to the element before dispatching. In React Native we don't do that. The iOS native side doesn't dispatch on the text nodes but only the parent. The Android native side however does dispatch on text nodes. We already covered this for strings, but current element in Stack can be a number if it is numeric text content. --- .../__tests__/ReactNativeEvents-test.js | 87 +++++++++++++++++++ .../shared/shared/event/EventPluginHub.js | 13 ++- 2 files changed, 93 insertions(+), 7 deletions(-) diff --git a/src/renderers/native/__tests__/ReactNativeEvents-test.js b/src/renderers/native/__tests__/ReactNativeEvents-test.js index c6d0085a59..783f4bab16 100644 --- a/src/renderers/native/__tests__/ReactNativeEvents-test.js +++ b/src/renderers/native/__tests__/ReactNativeEvents-test.js @@ -87,6 +87,93 @@ it('handles events', () => { ]); }); +it('handles events on text nodes', () => { + expect(RCTEventEmitter.register.mock.calls.length).toBe(1); + var EventEmitter = RCTEventEmitter.register.mock.calls[0][0]; + + var Text = createReactNativeComponentClass({ + validAttributes: {foo: true}, + uiViewClassName: 'Text', + }); + + class ContextHack extends React.Component { + static childContextTypes = {isInAParentText: React.PropTypes.bool}; + getChildContext() { + return {isInAParentText: true}; + } + render() { + return this.props.children; + } + } + + var log = []; + ReactNative.render( + + + log.push('string touchend')} + onTouchEndCapture={() => log.push('string touchend capture')} + onTouchStart={() => log.push('string touchstart')} + onTouchStartCapture={() => log.push('string touchstart capture')}> + Text Content + + log.push('number touchend')} + onTouchEndCapture={() => log.push('number touchend capture')} + onTouchStart={() => log.push('number touchstart')} + onTouchStartCapture={() => log.push('number touchstart capture')}> + {123} + + + , + 1, + ); + + expect(UIManager.createView.mock.calls.length).toBe(5); + + // Don't depend on the order of createView() calls. + // Stack creates views outside-in; fiber creates them inside-out. + var innerTagString = UIManager.createView.mock.calls.find( + args => args[3] && args[3].text === 'Text Content', + )[0]; + var innerTagNumber = UIManager.createView.mock.calls.find( + args => args[3] && args[3].text === '123', + )[0]; + + EventEmitter.receiveTouches( + 'topTouchStart', + [{target: innerTagString, identifier: 17}], + [0], + ); + EventEmitter.receiveTouches( + 'topTouchEnd', + [{target: innerTagString, identifier: 17}], + [0], + ); + + EventEmitter.receiveTouches( + 'topTouchStart', + [{target: innerTagNumber, identifier: 18}], + [0], + ); + EventEmitter.receiveTouches( + 'topTouchEnd', + [{target: innerTagNumber, identifier: 18}], + [0], + ); + + expect(log).toEqual([ + 'string touchstart capture', + 'string touchstart', + 'string touchend capture', + 'string touchend', + 'number touchstart capture', + 'number touchstart', + 'number touchend capture', + 'number touchend', + ]); +}); + it('handles when a responder is unmounted while a touch sequence is in progress', () => { var EventEmitter = RCTEventEmitter.register.mock.calls[0][0]; var View = createReactNativeComponentClass({ diff --git a/src/renderers/shared/shared/event/EventPluginHub.js b/src/renderers/shared/shared/event/EventPluginHub.js index 7ed1950708..ac723ff890 100644 --- a/src/renderers/shared/shared/event/EventPluginHub.js +++ b/src/renderers/shared/shared/event/EventPluginHub.js @@ -138,7 +138,10 @@ var EventPluginHub = { return null; } } else { - if (typeof inst._currentElement === 'string') { + const currentElement = inst._currentElement; + if ( + typeof currentElement === 'string' || typeof currentElement === 'number' + ) { // Text node, let it bubble through. return null; } @@ -146,14 +149,10 @@ var EventPluginHub = { // If the instance is already unmounted, we have no listeners. return null; } - const props = inst._currentElement.props; + const props = currentElement.props; listener = props[registrationName]; if ( - shouldPreventMouseEvent( - registrationName, - inst._currentElement.type, - props, - ) + shouldPreventMouseEvent(registrationName, currentElement.type, props) ) { return null; } From 096975afabdb23f0daab11758ff91a5ec5780ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Thu, 16 Mar 2017 16:29:05 -0700 Subject: [PATCH 04/11] Record fiber tests (#9195) --- scripts/fiber/tests-passing.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/fiber/tests-passing.txt b/scripts/fiber/tests-passing.txt index e6ffdc28ab..60e666174e 100644 --- a/scripts/fiber/tests-passing.txt +++ b/scripts/fiber/tests-passing.txt @@ -1436,6 +1436,7 @@ src/renderers/native/__tests__/ReactNativeAttributePayload-test.js src/renderers/native/__tests__/ReactNativeEvents-test.js * handles events +* handles events on text nodes * handles when a responder is unmounted while a touch sequence is in progress src/renderers/native/__tests__/ReactNativeMount-test.js From ad2b6545fee4c24ea2d7fa162fee012909920192 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Thu, 16 Mar 2017 17:12:50 -0700 Subject: [PATCH 05/11] Upgraded Flow dev dependency to 41 for 'implements' support (#9196) --- .flowconfig | 2 +- package.json | 2 +- yarn.lock | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.flowconfig b/.flowconfig index c05e58f597..4c85601032 100644 --- a/.flowconfig +++ b/.flowconfig @@ -36,4 +36,4 @@ suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError [version] -^0.37.0 +^0.41.0 diff --git a/package.json b/package.json index 094946a201..f6f8f5268d 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "eslint-plugin-react-internal": "file:./eslint-rules", "fbjs": "^0.8.9", "fbjs-scripts": "^0.6.0", - "flow-bin": "^0.37.0", + "flow-bin": "^0.41.0", "glob": "^6.0.1", "glob-stream": "^6.1.0", "grunt": "^0.4.5", diff --git a/yarn.lock b/yarn.lock index 26f1c6b89d..a04c53eeee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2232,9 +2232,9 @@ flat-cache@^1.2.1: graceful-fs "^4.1.2" write "^0.2.1" -flow-bin@^0.37.0: - version "0.37.4" - resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.37.4.tgz#3d8da2ef746e80e730d166e09040f4198969b76b" +flow-bin@^0.41.0: + version "0.41.0" + resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.41.0.tgz#8badac9a19da45004997e599bd316518db489b2e" flow-parser@0.40.0: version "0.40.0" From 78761c387f7ab24b0dbbb63e9085b26b808e5330 Mon Sep 17 00:00:00 2001 From: Sasha Aickin Date: Sun, 19 Mar 2017 16:08:55 +0000 Subject: [PATCH 06/11] Add unit tests for props to attribute mapping in SSR. (#9106) * Added a handful of SSR unit tests, ported from a previous pull request. * Fixing linting errors * Fixed a test helper function to properly report errors. * Un-nested the new rendering tests. Updated the fiber test passing/not passing lists. * Edited to comply with the react/jsx-space-before-closing eslint rule, which will soon be merged into master. * Response to code review from @spicyj. Moved tests to separate file, reworked wording of test names, corrected use of canUseDom, and simplified tests against tagName. Thanks for the help, @spicyj! * Converted the unit tests to use async-await style. * Moved async-await babel transform for tests from .babelrc to preprocessor.js. * Response to code review in PR #9089. Thanks, @spicyj! * Fixing some bugs in the SSR unit tests. * Missed deleting some repeated code in the last commit. * Adding unit tests for property to attribute mapping in SSR. * Removing some redundant unit tests. * Oops. I forgot to re-run record-tests after c593dbc; fixing that here. * Reformatting for prettier so that the build will pass. --- scripts/fiber/tests-failing.txt | 41 +++ scripts/fiber/tests-passing-except-dev.txt | 37 +++ scripts/fiber/tests-passing.txt | 72 ++++- .../ReactDOMServerIntegration-test.js | 263 +++++++++++++++++- 4 files changed, 398 insertions(+), 15 deletions(-) diff --git a/scripts/fiber/tests-failing.txt b/scripts/fiber/tests-failing.txt index 325811e749..4a4444a784 100644 --- a/scripts/fiber/tests-failing.txt +++ b/scripts/fiber/tests-failing.txt @@ -6,6 +6,47 @@ src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js * gives source code refs for unknown prop warning for exact elements (ssr) * gives source code refs for unknown prop warning for exact elements in composition (ssr) +src/renderers/dom/shared/__tests__/ReactDOMServerIntegration-test.js +* renders a blank div with client render on top of good server markup +* renders a div with inline styles with client render on top of good server markup +* renders a self-closing tag with client render on top of good server markup +* renders a self-closing tag as a child with client render on top of good server markup +* renders simple numbers with client render on top of good server markup +* renders simple strings with client render on top of good server markup +* renders string prop with true value with client render on top of good server markup +* renders string prop with false value with client render on top of good server markup +* renders boolean prop with true value with client render on top of good server markup +* renders boolean prop with false value with client render on top of good server markup +* renders boolean prop with self value with client render on top of good server markup +* renders boolean prop with "" value with client render on top of good server markup +* renders boolean prop with string value with client render on top of good server markup +* renders boolean prop with array value with client render on top of good server markup +* renders boolean prop with object value with client render on top of good server markup +* renders boolean prop with non-zero number value with client render on top of good server markup +* renders boolean prop with zero value with client render on top of good server markup +* renders download prop with true value with client render on top of good server markup +* renders download prop with false value with client render on top of good server markup +* renders download prop with string value with client render on top of good server markup +* renders download prop with string "true" value with client render on top of good server markup +* renders className prop with string value with client render on top of good server markup +* renders className prop with empty string value with client render on top of good server markup +* renders className prop with true value with client render on top of good server markup +* renders className prop with false value with client render on top of good server markup +* renders htmlFor with string value with client render on top of good server markup +* renders htmlFor with an empty string with client render on top of good server markup +* renders className prop with true value with client render on top of good server markup +* renders className prop with false value with client render on top of good server markup +* renders no ref attribute with client render on top of good server markup +* renders no children attribute with client render on top of good server markup +* renders no key attribute with client render on top of good server markup +* renders no dangerouslySetInnerHTML attribute with client render on top of good server markup +* renders no unknown attributes with client render on top of good server markup +* renders unknown data- attributes with client render on top of good server markup +* renders no unknown attributes for non-standard elements with client render on top of good server markup +* renders unknown attributes for custom elements with client render on top of good server markup +* renders unknown attributes for custom elements using is with client render on top of good server markup +* renders no HTML events with client render on top of good server markup + src/renderers/dom/shared/__tests__/ReactDOMTextComponent-test.js * can reconcile text merged by Node.normalize() alongside other elements * can reconcile text merged by Node.normalize() diff --git a/scripts/fiber/tests-passing-except-dev.txt b/scripts/fiber/tests-passing-except-dev.txt index ea25216a39..98dda4bba0 100644 --- a/scripts/fiber/tests-passing-except-dev.txt +++ b/scripts/fiber/tests-passing-except-dev.txt @@ -10,6 +10,43 @@ src/renderers/dom/shared/__tests__/ReactDOMServerIntegration-test.js * renders a div with inline styles with client render on top of bad server markup * renders a self-closing tag with client render on top of bad server markup * renders a self-closing tag as a child with client render on top of bad server markup +* renders simple numbers with client render on top of bad server markup +* renders simple strings with client render on top of bad server markup +* renders string prop with true value with client render on top of bad server markup +* renders string prop with false value with client render on top of bad server markup +* renders boolean prop with true value with client render on top of bad server markup +* renders boolean prop with false value with client render on top of bad server markup +* renders boolean prop with self value with client render on top of bad server markup +* renders boolean prop with "" value with client render on top of bad server markup +* renders boolean prop with string value with client render on top of bad server markup +* renders boolean prop with array value with client render on top of bad server markup +* renders boolean prop with object value with client render on top of bad server markup +* renders boolean prop with non-zero number value with client render on top of bad server markup +* renders boolean prop with zero value with client render on top of bad server markup +* renders download prop with true value with client render on top of bad server markup +* renders download prop with false value with client render on top of bad server markup +* renders download prop with string value with client render on top of bad server markup +* renders download prop with string "true" value with client render on top of bad server markup +* renders className prop with string value with client render on top of bad server markup +* renders className prop with empty string value with client render on top of bad server markup +* renders className prop with true value with client render on top of bad server markup +* renders className prop with false value with client render on top of bad server markup +* renders htmlFor with string value with client render on top of bad server markup +* renders htmlFor with an empty string with client render on top of bad server markup +* renders className prop with true value with client render on top of bad server markup +* renders className prop with false value with client render on top of bad server markup +* renders no ref attribute with client render on top of bad server markup +* renders no children attribute with client render on top of bad server markup +* renders no key attribute with client render on top of bad server markup +* renders no dangerouslySetInnerHTML attribute with client render on top of bad server markup +* renders no unknown attributes with server string render +* renders no unknown attributes with client render on top of bad server markup +* renders unknown data- attributes with client render on top of bad server markup +* renders no unknown attributes for non-standard elements with server string render +* renders no unknown attributes for non-standard elements with client render on top of bad server markup +* renders unknown attributes for custom elements with client render on top of bad server markup +* renders unknown attributes for custom elements using is with client render on top of bad server markup +* renders no HTML events with client render on top of bad server markup src/renderers/dom/shared/__tests__/ReactMount-test.js * should warn if mounting into dirty rendered markup diff --git a/scripts/fiber/tests-passing.txt b/scripts/fiber/tests-passing.txt index 60e666174e..2ffaa0d693 100644 --- a/scripts/fiber/tests-passing.txt +++ b/scripts/fiber/tests-passing.txt @@ -1064,16 +1064,80 @@ src/renderers/dom/shared/__tests__/ReactDOMSVG-test.js src/renderers/dom/shared/__tests__/ReactDOMServerIntegration-test.js * renders a blank div with server string render * renders a blank div with clean client render -* renders a blank div with client render on top of good server markup * renders a div with inline styles with server string render * renders a div with inline styles with clean client render -* renders a div with inline styles with client render on top of good server markup * renders a self-closing tag with server string render * renders a self-closing tag with clean client render -* renders a self-closing tag with client render on top of good server markup * renders a self-closing tag as a child with server string render * renders a self-closing tag as a child with clean client render -* renders a self-closing tag as a child with client render on top of good server markup +* renders simple numbers with server string render +* renders simple numbers with clean client render +* renders simple strings with server string render +* renders simple strings with clean client render +* renders string prop with true value with server string render +* renders string prop with true value with clean client render +* renders string prop with false value with server string render +* renders string prop with false value with clean client render +* renders boolean prop with true value with server string render +* renders boolean prop with true value with clean client render +* renders boolean prop with false value with server string render +* renders boolean prop with false value with clean client render +* renders boolean prop with self value with server string render +* renders boolean prop with self value with clean client render +* renders boolean prop with "" value with server string render +* renders boolean prop with "" value with clean client render +* renders boolean prop with string value with server string render +* renders boolean prop with string value with clean client render +* renders boolean prop with array value with server string render +* renders boolean prop with array value with clean client render +* renders boolean prop with object value with server string render +* renders boolean prop with object value with clean client render +* renders boolean prop with non-zero number value with server string render +* renders boolean prop with non-zero number value with clean client render +* renders boolean prop with zero value with server string render +* renders boolean prop with zero value with clean client render +* renders download prop with true value with server string render +* renders download prop with true value with clean client render +* renders download prop with false value with server string render +* renders download prop with false value with clean client render +* renders download prop with string value with server string render +* renders download prop with string value with clean client render +* renders download prop with string "true" value with server string render +* renders download prop with string "true" value with clean client render +* renders className prop with string value with server string render +* renders className prop with string value with clean client render +* renders className prop with empty string value with server string render +* renders className prop with empty string value with clean client render +* renders className prop with true value with server string render +* renders className prop with true value with clean client render +* renders className prop with false value with server string render +* renders className prop with false value with clean client render +* renders htmlFor with string value with server string render +* renders htmlFor with string value with clean client render +* renders htmlFor with an empty string with server string render +* renders htmlFor with an empty string with clean client render +* renders className prop with true value with server string render +* renders className prop with true value with clean client render +* renders className prop with false value with server string render +* renders className prop with false value with clean client render +* renders no ref attribute with server string render +* renders no ref attribute with clean client render +* renders no children attribute with server string render +* renders no children attribute with clean client render +* renders no key attribute with server string render +* renders no key attribute with clean client render +* renders no dangerouslySetInnerHTML attribute with server string render +* renders no dangerouslySetInnerHTML attribute with clean client render +* renders no unknown attributes with clean client render +* renders unknown data- attributes with server string render +* renders unknown data- attributes with clean client render +* renders no unknown attributes for non-standard elements with clean client render +* renders unknown attributes for custom elements with server string render +* renders unknown attributes for custom elements with clean client render +* renders unknown attributes for custom elements using is with server string render +* renders unknown attributes for custom elements using is with clean client render +* renders no HTML events with server string render +* renders no HTML events with clean client render src/renderers/dom/shared/__tests__/ReactDOMTextComponent-test.js * updates a mounted text component in place diff --git a/src/renderers/dom/shared/__tests__/ReactDOMServerIntegration-test.js b/src/renderers/dom/shared/__tests__/ReactDOMServerIntegration-test.js index 3b7b4e4d25..f3aa90ce90 100644 --- a/src/renderers/dom/shared/__tests__/ReactDOMServerIntegration-test.js +++ b/src/renderers/dom/shared/__tests__/ReactDOMServerIntegration-test.js @@ -65,14 +65,20 @@ function renderIntoDom(reactElement, domElement, errorCount = 0) { ); } +async function renderIntoString(reactElement, errorCount = 0) { + return await expectErrors( + () => + new Promise(resolve => + resolve(ReactDOMServer.renderToString(reactElement))), + errorCount, + ); +} + // Renders text using SSR and then stuffs it into a DOM node; returns the DOM // element that corresponds with the reactElement. // Does not render on client or perform client-side revival. async function serverRender(reactElement, errorCount = 0) { - const markup = await expectErrors( - () => Promise.resolve(ReactDOMServer.renderToString(reactElement)), - errorCount, - ); + const markup = await renderIntoString(reactElement, errorCount); var domElement = document.createElement('div'); domElement.innerHTML = markup; return domElement.firstChild; @@ -84,10 +90,17 @@ const clientCleanRender = (element, errorCount = 0) => { }; const clientRenderOnServerString = async (element, errorCount = 0) => { - const markup = await serverRender(element, errorCount); + const markup = await renderIntoString(element, errorCount); + resetModules(); var domElement = document.createElement('div'); domElement.innerHTML = markup; - return renderIntoDom(element, domElement, errorCount); + const serverElement = domElement.firstChild; + const clientElement = await renderIntoDom(element, domElement, errorCount); + // assert that the DOM element hasn't been replaced. + // Note that we cannot use expect(serverElement).toBe(clientElement) because + // of jest bug #1772 + expect(serverElement === clientElement).toBe(true); + return clientElement; }; const clientRenderOnBadMarkup = (element, errorCount = 0) => { @@ -135,14 +148,22 @@ function itClientRenders(desc, testFn) { testFn(clientRenderOnBadMarkup)); } +// When there is a test that renders on server and then on client and expects a logged +// error, you want to see the error show up both on server and client. Unfortunately, +// React refuses to issue the same error twice to avoid clogging up the console. +// To get around this, we must reload React modules in between server and client render. +function resetModules() { + jest.resetModuleRegistry(); + React = require('React'); + ReactDOM = require('ReactDOM'); + ReactDOMServer = require('ReactDOMServer'); + ExecutionEnvironment = require('ExecutionEnvironment'); +} + describe('ReactDOMServerIntegration', () => { beforeEach(() => { - jest.resetModuleRegistry(); - React = require('React'); - ReactDOM = require('ReactDOM'); - ReactDOMServer = require('ReactDOMServer'); + resetModules(); - ExecutionEnvironment = require('ExecutionEnvironment'); ExecutionEnvironment.canUseDOM = false; }); @@ -169,4 +190,224 @@ describe('ReactDOMServerIntegration', () => { expect(e.firstChild.tagName).toBe('BR'); }); }); + + describe('property to attribute mapping', function() { + describe('string properties', function() { + itRenders('simple numbers', async render => { + const e = await render(
); + expect(e.getAttribute('width')).toBe('30'); + }); + + itRenders('simple strings', async render => { + const e = await render(
); + expect(e.getAttribute('width')).toBe('30'); + }); + + // this seems like it might mask programmer error, but it's existing behavior. + itRenders('string prop with true value', async render => { + const e = await render(); + expect(e.getAttribute('href')).toBe('true'); + }); + + // this seems like it might mask programmer error, but it's existing behavior. + itRenders('string prop with false value', async render => { + const e = await render(); + expect(e.getAttribute('href')).toBe('false'); + }); + }); + + describe('boolean properties', function() { + itRenders('boolean prop with true value', async render => { + const e = await render(