Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fiber] fix ensure class components refs get re-assigned to emptyObject on mount #9045

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions scripts/fiber/tests-failing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ src/renderers/__tests__/ReactPerf-test.js
* should not count time in a portal towards lifecycle method
* should work when measurement starts during reconciliation

src/renderers/__tests__/refs-test.js
* Should increase refs with an increase in divs

src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
* gives source code refs for unknown prop warning (ssr)
* gives source code refs for unknown prop warning for exact elements (ssr)
Expand Down
1 change: 1 addition & 0 deletions scripts/fiber/tests-passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ src/renderers/__tests__/refs-destruction-test.js
* should not error when destroying child with ref asynchronously

src/renderers/__tests__/refs-test.js
* Should increase refs with an increase in divs
* Allow refs to hop around children correctly
* always has a value for this.refs
* ref called correctly for stateless component when __DEV__ = false
Expand Down
131 changes: 65 additions & 66 deletions src/renderers/__tests__/refs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,80 @@ var ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
var ReactTestUtils = require('ReactTestUtils');

/**
* Counts clicks and has a renders an item for each click. Each item rendered
* has a ref of the form "clickLogN".
* Render a TestRefsComponent and ensure that the main refs are wired up.
* we also define the classes in this function to ensure that they are
* wired up properly in accordance to how Jest might auto mock modules
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, could you explain how this is related to automocking?
If you mean resetting modules, it's a different feature from automocking.
I don't think we have automocking enabled in React repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message was wrong, I've changed it. I didn't mean to state automocking.

*/
class ClickCounter extends React.Component {
state = {count: this.props.initialCount};

triggerReset = () => {
this.setState({count: this.props.initialCount});
};

handleClick = () => {
this.setState({count: this.state.count + 1});
};

render() {
var children = [];
var i;
for (i = 0; i < this.state.count; i++) {
children.push(
<div
className="clickLogDiv"
key={'clickLog' + i}
ref={'clickLog' + i}
/>
);
function renderTestRefsComponent() {
/**
* Only purpose is to test that refs are tracked even when applied to a
* component that is injected down several layers. Ref systems are difficult to
* build in such a way that ownership is maintained in an airtight manner.
*/
class GeneralContainerComponent extends React.Component {
render() {
return <div>{this.props.children}</div>;
}
return (
<span className="clickIncrementer" onClick={this.handleClick}>
{children}
</span>
);
}
}

/**
* Only purpose is to test that refs are tracked even when applied to a
* component that is injected down several layers. Ref systems are difficult to
* build in such a way that ownership is maintained in an airtight manner.
*/
class GeneralContainerComponent extends React.Component {
render() {
return <div>{this.props.children}</div>;
/**
* Counts clicks and has a renders an item for each click. Each item rendered
* has a ref of the form "clickLogN".
*/
class ClickCounter extends React.Component {
state = {count: this.props.initialCount};

triggerReset = () => {
this.setState({count: this.props.initialCount});
};

handleClick = () => {
this.setState({count: this.state.count + 1});
};

render() {
var children = [];
var i;
for (i = 0; i < this.state.count; i++) {
children.push(
<div
className="clickLogDiv"
key={'clickLog' + i}
ref={'clickLog' + i}
/>
);
}
return (
<span className="clickIncrementer" onClick={this.handleClick}>
{children}
</span>
);
}
}
}

/**
* Notice how refs ownership is maintained even when injecting a component
* into a different parent.
*/
class TestRefsComponent extends React.Component {
doReset = () => {
this.refs.myCounter.triggerReset();
};

render() {
return (
<div>
<div ref="resetDiv" onClick={this.doReset}>
Reset Me By Clicking This.
/**
* Notice how refs ownership is maintained even when injecting a component
* into a different parent.
*/
class TestRefsComponent extends React.Component {
doReset = () => {
this.refs.myCounter.triggerReset();
};

render() {
return (
<div>
<div ref="resetDiv" onClick={this.doReset}>
Reset Me By Clicking This.
</div>
<GeneralContainerComponent ref="myContainer">
<ClickCounter ref="myCounter" initialCount={1}/>
</GeneralContainerComponent>
</div>
<GeneralContainerComponent ref="myContainer">
<ClickCounter ref="myCounter" initialCount={1}/>
</GeneralContainerComponent>
</div>
);
);
}
}
}

/**
* Render a TestRefsComponent and ensure that the main refs are wired up.
*/
var renderTestRefsComponent = function() {
var testRefsComponent =
ReactTestUtils.renderIntoDocument(<TestRefsComponent />);
expect(testRefsComponent instanceof TestRefsComponent).toBe(true);
Expand Down Expand Up @@ -146,11 +148,8 @@ describe('reactiverefs', () => {
expectClickLogsLengthToBe(testRefsComponent, 1);

});

});



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