Skip to content

Commit 91e8081

Browse files
authored
Merge pull request #9045 from trueadm/emptyObject-reference-mismatch-due-to-mocking
[Fiber] fix ensure class components refs get re-assigned to emptyObject on mount
2 parents 23f2451 + a99d77e commit 91e8081

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

scripts/fiber/tests-failing.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ src/renderers/__tests__/ReactPerf-test.js
1616
* should not count time in a portal towards lifecycle method
1717
* should work when measurement starts during reconciliation
1818

19-
src/renderers/__tests__/refs-test.js
20-
* Should increase refs with an increase in divs
21-
2219
src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
2320
* gives source code refs for unknown prop warning (ssr)
2421
* gives source code refs for unknown prop warning for exact elements (ssr)

scripts/fiber/tests-passing.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,8 @@ src/renderers/__tests__/refs-destruction-test.js
849849
* should not error when destroying child with ref asynchronously
850850

851851
src/renderers/__tests__/refs-test.js
852+
* Should increase refs with an increase in divs
853+
* Should correctly get the ref
852854
* Allow refs to hop around children correctly
853855
* always has a value for this.refs
854856
* ref called correctly for stateless component when __DEV__ = false

src/renderers/__tests__/refs-test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,23 @@ describe('reactiverefs', () => {
144144
// Now reset again
145145
ReactTestUtils.Simulate.click(testRefsComponent.refs.resetDiv);
146146
expectClickLogsLengthToBe(testRefsComponent, 1);
147-
148147
});
149-
150148
});
151149

150+
describe('factory components', () => {
151+
it('Should correctly get the ref', () => {
152+
function Comp() {
153+
return {
154+
render() {
155+
return <div ref="elemRef" />;
156+
},
157+
};
158+
}
152159

160+
const inst = ReactTestUtils.renderIntoDocument(<Comp />);
161+
expect(inst.refs.elemRef.tagName).toBe('DIV');
162+
});
163+
});
153164

154165
/**
155166
* Tests that when a ref hops around children, we can track that correctly.

src/renderers/shared/fiber/ReactFiberClassComponent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ module.exports = function(
291291

292292
instance.props = props;
293293
instance.state = state;
294+
instance.refs = emptyObject;
294295
instance.context = getMaskedContext(workInProgress, unmaskedContext);
295296

296297
if (typeof instance.componentWillMount === 'function') {

0 commit comments

Comments
 (0)