From 91dc94cec6e0b95c35f8e912ed666b64e2be4f3e Mon Sep 17 00:00:00 2001 From: Brandon Dail Date: Tue, 4 Oct 2016 15:22:03 -0700 Subject: [PATCH] Resolve Flow & Test issues with ReactTestRenderer This is a manual cherry-pick of 2 PRs, updated to handle differences in the stable branch: - c78464f8ea9a5b00ec80252d20a71a1482210e57 - Resolve flow errors with ReactTestRenderer (#7736) - 7dfa01f9fa3bd4205a3c0faa386319eee2493cea - Revert ReactMultiChild to plain object (#7757) --- .../shared/stack/reconciler/ReactOwner.js | 3 ++- src/renderers/testing/ReactTestEmptyComponent.js | 2 ++ src/renderers/testing/ReactTestMount.js | 6 ++++-- .../testing/ReactTestReconcileTransaction.js | 4 +++- src/renderers/testing/ReactTestRenderer.js | 14 +++++++++++--- src/renderers/testing/ReactTestTextComponent.js | 2 ++ 6 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/renderers/shared/stack/reconciler/ReactOwner.js b/src/renderers/shared/stack/reconciler/ReactOwner.js index 1e9b411f9d..a804513d73 100644 --- a/src/renderers/shared/stack/reconciler/ReactOwner.js +++ b/src/renderers/shared/stack/reconciler/ReactOwner.js @@ -15,6 +15,7 @@ var invariant = require('invariant'); import type { ReactInstance } from 'ReactInstanceType'; +import type { Transaction } from 'Transaction'; /** * @param {?object} object @@ -73,7 +74,7 @@ var ReactOwner = { component: ReactInstance, ref: string, owner: ReactInstance, - transaction, + transaction: Transaction, ): void { invariant( isValidOwner(owner), diff --git a/src/renderers/testing/ReactTestEmptyComponent.js b/src/renderers/testing/ReactTestEmptyComponent.js index 9f6659ea8d..40b62e03fc 100644 --- a/src/renderers/testing/ReactTestEmptyComponent.js +++ b/src/renderers/testing/ReactTestEmptyComponent.js @@ -13,6 +13,8 @@ 'use strict'; class ReactTestEmptyComponent { + _currentElement: null; + constructor() { this._currentElement = null; } diff --git a/src/renderers/testing/ReactTestMount.js b/src/renderers/testing/ReactTestMount.js index 71811e4bfa..f13e5d20f6 100644 --- a/src/renderers/testing/ReactTestMount.js +++ b/src/renderers/testing/ReactTestMount.js @@ -20,8 +20,10 @@ var getHostComponentFromComposite = require('getHostComponentFromComposite'); var instantiateReactComponent = require('instantiateReactComponent'); var invariant = require('invariant'); -type TestRendererOptions = { - createNodeMock: (element: ReactElement) => Object, +import type { ReactElement } from 'ReactElementType'; + +export type TestRendererOptions = { + createNodeMock: (element: ReactElement) => any, }; var defaultTestOptions = { diff --git a/src/renderers/testing/ReactTestReconcileTransaction.js b/src/renderers/testing/ReactTestReconcileTransaction.js index ba87192001..50585e08be 100644 --- a/src/renderers/testing/ReactTestReconcileTransaction.js +++ b/src/renderers/testing/ReactTestReconcileTransaction.js @@ -16,6 +16,8 @@ var PooledClass = require('PooledClass'); var Transaction = require('Transaction'); var ReactUpdateQueue = require('ReactUpdateQueue'); +import type { TestRendererOptions } from 'ReactTestMount'; + /** * Provides a `CallbackQueue` queue for collecting `onDOMReady` callbacks during * the performing of the transaction. @@ -57,7 +59,7 @@ var TRANSACTION_WRAPPERS = [ON_DOM_READY_QUEUEING]; * * @class ReactTestReconcileTransaction */ -function ReactTestReconcileTransaction(testOptions) { +function ReactTestReconcileTransaction(testOptions: TestRendererOptions) { this.reinitializeTransaction(); this.testOptions = testOptions; this.reactMountReady = CallbackQueue.getPooled(this); diff --git a/src/renderers/testing/ReactTestRenderer.js b/src/renderers/testing/ReactTestRenderer.js index a7daf6ebf9..e33c0bca9c 100644 --- a/src/renderers/testing/ReactTestRenderer.js +++ b/src/renderers/testing/ReactTestRenderer.js @@ -24,11 +24,13 @@ var ReactTestTextComponent = require('ReactTestTextComponent'); var ReactTestEmptyComponent = require('ReactTestEmptyComponent'); import type { ReactElement } from 'ReactElementType'; +import type { ReactInstance } from 'ReactInstanceType'; type ReactTestRendererJSON = { type: string, props: { [propName: string]: string }, - children: Array, + children: null | Array, + $$typeof?: any } /** @@ -47,6 +49,10 @@ function getRenderedHostOrTextFromComponent(component) { } class ReactTestComponent { + _currentElement: ReactElement; + _renderedChildren: null | Object; + _topLevelWrapper: null | ReactInstance; + constructor(element: ReactElement) { this._currentElement = element; this._renderedChildren = null; @@ -60,6 +66,7 @@ class ReactTestComponent { context: Object, ) { var element = this._currentElement; + // $FlowFixMe https://github.com/facebook/flow/issues/1805 this.mountChildren(element.props.children, transaction, context); } @@ -69,6 +76,7 @@ class ReactTestComponent { context: Object, ) { this._currentElement = nextElement; + // $FlowFixMe https://github.com/facebook/flow/issues/1805 this.updateChildren(nextElement.props.children, transaction, context); } @@ -89,7 +97,7 @@ class ReactTestComponent { childrenJSON.push(json); } } - var object = { + var object: ReactTestRendererJSON = { type: this._currentElement.type, props: props, children: childrenJSON.length ? childrenJSON : null, @@ -104,7 +112,7 @@ class ReactTestComponent { unmountComponent(): void {} } -Object.assign(ReactTestComponent.prototype, ReactMultiChild); +Object.assign(ReactTestComponent.prototype, ReactMultiChild.Mixin); // ============================================================================= diff --git a/src/renderers/testing/ReactTestTextComponent.js b/src/renderers/testing/ReactTestTextComponent.js index 18e22d4e9f..7c6c4de901 100644 --- a/src/renderers/testing/ReactTestTextComponent.js +++ b/src/renderers/testing/ReactTestTextComponent.js @@ -15,6 +15,8 @@ import type { ReactText } from 'ReactTypes'; class ReactTestTextComponent { + _currentElement: ReactText; + constructor(element: ReactText) { this._currentElement = element; }