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

Use _hostContainerInfo to track test renderer options #8261

Merged
merged 5 commits into from
Nov 11, 2016

Conversation

aweary
Copy link
Contributor

@aweary aweary commented Nov 10, 2016

Resolves #7740

We discussed using _hostContainerInfo in #8164 to support findDOMNode, but it also helps us here. Since the transaction isn't easily available when we're updating or unmounting, tracking the test options as _hostContainerInfo simplifies things

The transaction is not available when unmounting or updating the
instance, so we track it using _hostContainerInfo
@@ -83,8 +86,11 @@ class ReactTestComponent {

getPublicInstance(transaction: ReactTestReconcileTransaction): Object {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we remove transaction as an argument here now? As well as not pass it wherever we currently do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yupp, I'll go ahead and do that as well 👍

it('supports unmounting when using refs', () => {
class Foo extends React.Component {
render() {
return <div ref="foo"/>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: space before />

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we add a linting rule for this? hard to remember if lint doesn't complain.

}
}
const inst = ReactTestRenderer.create(
<Foo/>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same

}
const inst = ReactTestRenderer.create(
<Foo useDiv={true} />,
{ createNodeMock }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: no spaces in object literals, {createNodeMock}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A linting rule for this would also be great.

@@ -83,8 +86,11 @@ class ReactTestComponent {

getPublicInstance(transaction: ReactTestReconcileTransaction): Object {
var element = this._currentElement;
var options = transaction.getTestOptions();
return options.createNodeMock(element);
var options = this._hostContainerInfo;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's call this hostContainerInfo everywhere and get rid of options.

}

mountComponent(
transaction: ReactTestReconcileTransaction,
nativeParent: null | ReactTestComponent,
nativeContainerInfo: ?null,
hostContainerInfo: null | Object,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't it always exist in test renderer? We probably shouldn't check for its existence below as well.

Copy link
Contributor Author

@aweary aweary Nov 10, 2016

Choose a reason for hiding this comment

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

True, it should always be populated here.

if (options && options.createNodeMock) {
return options.createNodeMock(element);
}
return {};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this branch ever runs. We always use defaultTestOptions if options didn't exist, and it returns null rather than {}. I want to completely avoid conditions here.

Copy link
Contributor Author

@aweary aweary Nov 10, 2016

Choose a reason for hiding this comment

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

This was because of flow, _hostContainerInfo is typed as null | Object, which is valid since it is null when the instance is created. The _hostContainerInfo isn't populated until the instance mounts.

While it never should be null when this method is called, flow doesn't know that. getPublicInstance is also typed as returning an Object. That's easy to change though if you want to do null | Object.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you throw if it is null? I think this might make it clear to Flow that it's not null.

Copy link
Collaborator

@gaearon gaearon left a comment

Choose a reason for hiding this comment

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

Style nits, let's remove old code, and remove unnecessary conditions.

@gaearon gaearon added this to the 15.4.0 milestone Nov 10, 2016
Brandon Dail added 3 commits November 10, 2016 13:57
We don't need to pass the transaction around anymore since we store the
test options on _hostContainerInfo instead
@aweary aweary force-pushed the react-test-renderer-lifecycle-fixes branch from e3dc639 to a4b488f Compare November 11, 2016 04:16
@gaearon
Copy link
Collaborator

gaearon commented Nov 11, 2016

LGTM. Thank you!

@gaearon gaearon merged commit e43aaab into facebook:master Nov 11, 2016
zpao pushed a commit that referenced this pull request Nov 16, 2016
* Use _hostContainerInfo to track test renderer options

The transaction is not available when unmounting or updating the
instance, so we track it using _hostContainerInfo

* Throw if hostContainerInfo is not populated in getPublicInstance

* Linting fixes

* Remove transaction from ref lifecycle code path

We don't need to pass the transaction around anymore since we store the
test options on _hostContainerInfo instead

* Remove unused argument

(cherry picked from commit e43aaab)
acusti pushed a commit to brandcast/react that referenced this pull request Mar 15, 2017
* Use _hostContainerInfo to track test renderer options

The transaction is not available when unmounting or updating the
instance, so we track it using _hostContainerInfo

* Throw if hostContainerInfo is not populated in getPublicInstance

* Linting fixes

* Remove transaction from ref lifecycle code path

We don't need to pass the transaction around anymore since we store the
test options on _hostContainerInfo instead

* Remove unused argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants