@samitouri / QOS-React / commits / 724ae9b35a

reverts previous changes to inlining requires and fixes test suite instead

Dominic Gannaway committed Feb 23, 2017 at 12:31 UTC 724ae9b35a287dc183894f60f2750fdb2815684f
3 files changed +70 -72
src/isomorphic/modern/class/ReactBaseClasses.js
+3 -4
@@ -14,6 +14,7 @@
14 var ReactNoopUpdateQueue = require('ReactNoopUpdateQueue');
15
16 var canDefineProperty = require('canDefineProperty');
17 +var emptyObject = require('emptyObject');
18 var invariant = require('invariant');
19 var warning = require('warning');
20
@@ -23,8 +24,7 @@ var warning = require('warning');
24 function ReactComponent(props, context, updater) {
25 this.props = props;
26 this.context = context;
26 - // emptyObject is inlined to help avoid module mocking referential mismatches
27 - this.refs = require('emptyObject');
27 + this.refs = emptyObject;
28 // We initialize the default updater but the real one gets injected by the
29 // renderer.
30 this.updater = updater || ReactNoopUpdateQueue;
@@ -133,8 +133,7 @@ function ReactPureComponent(props, context, updater) {
133 // Duplicated from ReactComponent.
134 this.props = props;
135 this.context = context;
136 - // emptyObject is inlined to help avoid module mocking referential mismatches
137 - this.refs = require('emptyObject');
136 + this.refs = emptyObject;
137 // We initialize the default updater but the real one gets injected by the
138 // renderer.
139 this.updater = updater || ReactNoopUpdateQueue;
src/renderers/__tests__/refs-test.js
+65 -66
@@ -16,78 +16,80 @@ var ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
16 var ReactTestUtils = require('ReactTestUtils');
17
18 /**
19 - * Counts clicks and has a renders an item for each click. Each item rendered
20 - * has a ref of the form "clickLogN".
19 + * Render a TestRefsComponent and ensure that the main refs are wired up.
20 + * we also define the classes in this function to ensure that they are
21 + * wired up properly in accordance to how Jest might auto mock modules
22 */
22 -class ClickCounter extends React.Component {
23 - state = {count: this.props.initialCount};
24 -
25 - triggerReset = () => {
26 - this.setState({count: this.props.initialCount});
27 - };
28 -
29 - handleClick = () => {
30 - this.setState({count: this.state.count + 1});
31 - };
32 -
33 - render() {
34 - var children = [];
35 - var i;
36 - for (i = 0; i < this.state.count; i++) {
37 - children.push(
38 - <div
39 - className="clickLogDiv"
40 - key={'clickLog' + i}
41 - ref={'clickLog' + i}
42 - />
43 - );
23 +function renderTestRefsComponent() {
24 + /**
25 + * Only purpose is to test that refs are tracked even when applied to a
26 + * component that is injected down several layers. Ref systems are difficult to
27 + * build in such a way that ownership is maintained in an airtight manner.
28 + */
29 + class GeneralContainerComponent extends React.Component {
30 + render() {
31 + return <div>{this.props.children}</div>;
32 }
45 - return (
46 - <span className="clickIncrementer" onClick={this.handleClick}>
47 - {children}
48 - </span>
49 - );
33 }
51 -}
34
53 -/**
54 - * Only purpose is to test that refs are tracked even when applied to a
55 - * component that is injected down several layers. Ref systems are difficult to
56 - * build in such a way that ownership is maintained in an airtight manner.
57 - */
58 -class GeneralContainerComponent extends React.Component {
59 - render() {
60 - return <div>{this.props.children}</div>;
35 + /**
36 + * Counts clicks and has a renders an item for each click. Each item rendered
37 + * has a ref of the form "clickLogN".
38 + */
39 + class ClickCounter extends React.Component {
40 + state = {count: this.props.initialCount};
41 +
42 + triggerReset = () => {
43 + this.setState({count: this.props.initialCount});
44 + };
45 +
46 + handleClick = () => {
47 + this.setState({count: this.state.count + 1});
48 + };
49 +
50 + render() {
51 + var children = [];
52 + var i;
53 + for (i = 0; i < this.state.count; i++) {
54 + children.push(
55 + <div
56 + className="clickLogDiv"
57 + key={'clickLog' + i}
58 + ref={'clickLog' + i}
59 + />
60 + );
61 + }
62 + return (
63 + <span className="clickIncrementer" onClick={this.handleClick}>
64 + {children}
65 + </span>
66 + );
67 + }
68 }
62 -}
69
64 -/**
65 - * Notice how refs ownership is maintained even when injecting a component
66 - * into a different parent.
67 - */
68 -class TestRefsComponent extends React.Component {
69 - doReset = () => {
70 - this.refs.myCounter.triggerReset();
71 - };
72 -
73 - render() {
74 - return (
75 - <div>
76 - <div ref="resetDiv" onClick={this.doReset}>
77 - Reset Me By Clicking This.
70 + /**
71 + * Notice how refs ownership is maintained even when injecting a component
72 + * into a different parent.
73 + */
74 + class TestRefsComponent extends React.Component {
75 + doReset = () => {
76 + this.refs.myCounter.triggerReset();
77 + };
78 +
79 + render() {
80 + return (
81 + <div>
82 + <div ref="resetDiv" onClick={this.doReset}>
83 + Reset Me By Clicking This.
84 + </div>
85 + <GeneralContainerComponent ref="myContainer">
86 + <ClickCounter ref="myCounter" initialCount={1}/>
87 + </GeneralContainerComponent>
88 </div>
79 - <GeneralContainerComponent ref="myContainer">
80 - <ClickCounter ref="myCounter" initialCount={1}/>
81 - </GeneralContainerComponent>
82 - </div>
83 - );
89 + );
90 + }
91 }
85 -}
92
87 -/**
88 - * Render a TestRefsComponent and ensure that the main refs are wired up.
89 - */
90 -var renderTestRefsComponent = function() {
93 var testRefsComponent =
94 ReactTestUtils.renderIntoDocument(<TestRefsComponent />);
95 expect(testRefsComponent instanceof TestRefsComponent).toBe(true);
@@ -146,11 +148,8 @@ describe('reactiverefs', () => {
148 expectClickLogsLengthToBe(testRefsComponent, 1);
149
150 });
149 -
151 });
152
152 -
153 -
153 /**
154 * Tests that when a ref hops around children, we can track that correctly.
155 */
src/renderers/shared/fiber/ReactChildFiber.js
+2 -2
@@ -32,6 +32,7 @@ var ReactFiber = require('ReactFiber');
32 var ReactTypeOfSideEffect = require('ReactTypeOfSideEffect');
33 var ReactTypeOfWork = require('ReactTypeOfWork');
34
35 +var emptyObject = require('emptyObject');
36 var getIteratorFn = require('getIteratorFn');
37 var invariant = require('invariant');
38 var ReactFeatureFlags = require('ReactFeatureFlags');
@@ -100,8 +101,7 @@ function coerceRef(current: Fiber | null, element: ReactElement) {
101 return current.ref;
102 }
103 const ref = function(value) {
103 - // emptyObject is inlined to help avoid module mocking referential mismatches
104 - const refs = inst.refs === require('emptyObject') ? (inst.refs = {}) : inst.refs;
104 + const refs = inst.refs === emptyObject ? (inst.refs = {}) : inst.refs;
105 if (value === null) {
106 delete refs[stringRef];
107 } else {