-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Use _hostContainerInfo to track test renderer options #8261
Conversation
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"/>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: space before />
There was a problem hiding this comment.
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/>, |
There was a problem hiding this comment.
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 } |
There was a problem hiding this comment.
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}
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 {}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
We don't need to pass the transaction around anymore since we store the test options on _hostContainerInfo instead
e3dc639
to
a4b488f
Compare
LGTM. Thank you! |
* 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)
* 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
Resolves #7740
We discussed using
_hostContainerInfo
in #8164 to supportfindDOMNode
, 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