@samitouri / QOS-React-2 / commits / 5a3abab660

Clean up ReactTestRenderer (#7716)

* create ReactTestTextComponent fil * create ReactTestEmptyComponent * Use class for ReactTestRenderer * Add flow to ReactTestRenderer

Brandon Dail committed Sep 13, 2016 at 21:20 UTC 5a3abab66085433c35e1138ecfdeea35c8d9327a
3 files changed +123 -78
src/renderers/testing/ReactTestEmptyComponent.js new
+26
@@ -0,0 +1,26 @@
1 +/**
2 + * Copyright 2013-present, Facebook, Inc.
3 + * All rights reserved.
4 + *
5 + * This source code is licensed under the BSD-style license found in the
6 + * LICENSE file in the root directory of this source tree. An additional grant
7 + * of patent rights can be found in the PATENTS file in the same directory.
8 + *
9 + * @providesModule ReactTestEmptyComponent
10 + * @flow
11 + */
12 +
13 +'use strict';
14 +
15 +class ReactTestEmptyComponent {
16 + constructor() {
17 + this._currentElement = null;
18 + }
19 + receiveComponent(): void {}
20 + toJSON(): void {}
21 + mountComponent(): void {}
22 + getHostNode(): void {}
23 + unmountComponent(): void {}
24 +}
25 +
26 +module.exports = ReactTestEmptyComponent;
src/renderers/testing/ReactTestRenderer.js
+62 -78
@@ -7,6 +7,7 @@
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 * @providesModule ReactTestRenderer
10 + * @flow
11 */
12
13 'use strict';
@@ -19,6 +20,16 @@ var ReactHostComponent = require('ReactHostComponent');
20 var ReactTestMount = require('ReactTestMount');
21 var ReactTestReconcileTransaction = require('ReactTestReconcileTransaction');
22 var ReactUpdates = require('ReactUpdates');
23 +var ReactTestTextComponent = require('ReactTestTextComponent');
24 +var ReactTestEmptyComponent = require('ReactTestEmptyComponent');
25 +
26 +import type { ReactElement } from 'ReactElementType';
27 +
28 +type ReactTestRendererJSON = {
29 + type: string,
30 + props: { [propName: string]: string },
31 + children: Array<string | ReactTestRendererJSON>,
32 +}
33
34 /**
35 * Drill down (through composites and empty components) until we get a native or
@@ -35,92 +46,65 @@ function getRenderedHostOrTextFromComponent(component) {
46 return component;
47 }
48
49 +class ReactTestComponent {
50 + constructor(element: ReactElement) {
51 + this._currentElement = element;
52 + this._renderedChildren = null;
53 + this._topLevelWrapper = null;
54 + }
55
39 -// =============================================================================
40 -
41 -var ReactTestComponent = function(element) {
42 - this._currentElement = element;
43 - this._renderedChildren = null;
44 - this._topLevelWrapper = null;
45 -};
46 -
47 -ReactTestComponent.prototype.mountComponent = function(
48 - transaction,
49 - nativeParent,
50 - nativeContainerInfo,
51 - context
52 -) {
53 - var element = this._currentElement;
54 - this.mountChildren(element.props.children, transaction, context);
55 -};
56 -
57 -ReactTestComponent.prototype.receiveComponent = function(
58 - nextElement,
59 - transaction,
60 - context
61 -) {
62 - this._currentElement = nextElement;
63 - this.updateChildren(nextElement.props.children, transaction, context);
64 -};
56 + mountComponent(
57 + transaction: ReactTestReconcileTransaction,
58 + nativeParent: null | ReactTestComponent,
59 + nativeContainerInfo: ?null,
60 + context: Object,
61 + ) {
62 + var element = this._currentElement;
63 + this.mountChildren(element.props.children, transaction, context);
64 + }
65
66 -ReactTestComponent.prototype.getHostNode = function() {};
66 + receiveComponent(
67 + nextElement: ReactElement,
68 + transaction: ReactTestReconcileTransaction,
69 + context: Object,
70 + ) {
71 + this._currentElement = nextElement;
72 + this.updateChildren(nextElement.props.children, transaction, context);
73 + }
74
68 -ReactTestComponent.prototype.getPublicInstance = function(transaction) {
69 - var element = this._currentElement;
70 - var options = transaction.getTestOptions();
71 - return options.createNodeMock(element);
72 -};
75 + getPublicInstance(transaction: ReactTestReconcileTransaction): Object {
76 + var element = this._currentElement;
77 + var options = transaction.getTestOptions();
78 + return options.createNodeMock(element);
79 + }
80
74 -ReactTestComponent.prototype.unmountComponent = function() {};
75 -
76 -ReactTestComponent.prototype.toJSON = function() {
77 - var {children, ...props} = this._currentElement.props;
78 - var childrenJSON = [];
79 - for (var key in this._renderedChildren) {
80 - var inst = this._renderedChildren[key];
81 - inst = getRenderedHostOrTextFromComponent(inst);
82 - var json = inst.toJSON();
83 - if (json !== undefined) {
84 - childrenJSON.push(json);
81 + toJSON(): ReactTestRendererJSON {
82 + var {children, ...props} = this._currentElement.props;
83 + var childrenJSON = [];
84 + for (var key in this._renderedChildren) {
85 + var inst = this._renderedChildren[key];
86 + inst = getRenderedHostOrTextFromComponent(inst);
87 + var json = inst.toJSON();
88 + if (json !== undefined) {
89 + childrenJSON.push(json);
90 + }
91 }
92 + var object = {
93 + type: this._currentElement.type,
94 + props: props,
95 + children: childrenJSON.length ? childrenJSON : null,
96 + };
97 + Object.defineProperty(object, '$$typeof', {
98 + value: Symbol.for('react.test.json'),
99 + });
100 + return object;
101 }
87 - var object = {
88 - type: this._currentElement.type,
89 - props: props,
90 - children: childrenJSON.length ? childrenJSON : null,
91 - };
92 - Object.defineProperty(object, '$$typeof', {
93 - value: Symbol.for('react.test.json'),
94 - });
95 - return object;
96 -};
97 -Object.assign(ReactTestComponent.prototype, ReactMultiChild);
102
99 -// =============================================================================
100 -
101 -var ReactTestTextComponent = function(element) {
102 - this._currentElement = element;
103 -};
104 -ReactTestTextComponent.prototype.mountComponent = function() {};
105 -ReactTestTextComponent.prototype.receiveComponent = function(nextElement) {
106 - this._currentElement = nextElement;
107 -};
108 -ReactTestTextComponent.prototype.getHostNode = function() {};
109 -ReactTestTextComponent.prototype.unmountComponent = function() {};
110 -ReactTestTextComponent.prototype.toJSON = function() {
111 - return this._currentElement;
112 -};
113 -
114 -// =============================================================================
103 + getHostNode(): void {}
104 + unmountComponent(): void {}
105 +}
106
116 -var ReactTestEmptyComponent = function(element) {
117 - this._currentElement = null;
118 -};
119 -ReactTestEmptyComponent.prototype.mountComponent = function() {};
120 -ReactTestEmptyComponent.prototype.receiveComponent = function() {};
121 -ReactTestEmptyComponent.prototype.getHostNode = function() {};
122 -ReactTestEmptyComponent.prototype.unmountComponent = function() {};
123 -ReactTestEmptyComponent.prototype.toJSON = function() {};
107 +Object.assign(ReactTestComponent.prototype, ReactMultiChild);
108
109 // =============================================================================
110
src/renderers/testing/ReactTestTextComponent.js new
+35
@@ -0,0 +1,35 @@
1 +/**
2 + * Copyright 2013-present, Facebook, Inc.
3 + * All rights reserved.
4 + *
5 + * This source code is licensed under the BSD-style license found in the
6 + * LICENSE file in the root directory of this source tree. An additional grant
7 + * of patent rights can be found in the PATENTS file in the same directory.
8 + *
9 + * @providesModule ReactTestTextComponent
10 + * @flow
11 + */
12 +
13 +'use strict';
14 +
15 +import type { ReactText } from 'ReactTypes';
16 +
17 +class ReactTestTextComponent {
18 + constructor(element: ReactText) {
19 + this._currentElement = element;
20 + }
21 +
22 + receiveComponent(nextElement: ReactText) {
23 + this._currentElement = nextElement;
24 + }
25 +
26 + toJSON(): ReactText {
27 + return this._currentElement;
28 + }
29 +
30 + mountComponent(): void {}
31 + getHostNode(): void {}
32 + unmountComponent(): void {}
33 +}
34 +
35 +module.exports = ReactTestTextComponent;