-
Notifications
You must be signed in to change notification settings - Fork 47.6k
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
String refs cause incorrect warning in ReactTestRenderer #7645
Comments
Proposal:
This would make test renderer way more usable. A good test is to verify that it can handle components defined in |
What kind of object would edit: that might be too ambitious, actually. It does seem better to just let them return an object that implements what ever API they're actually using. So if they're calling How would that work if they had multiple refs? |
I think an object with no-op methods for complete DOM API might be enough. I don’t think it’s worth making it act more realistic like jsdom. There will always be corner cases for sure but it’s hard to tell what they’re like until we actually ship some version of this and are able to iterate. |
I agree. With this API users could return something as sparse or as full featured as they want. As for supporting multiple refs, how would |
@aweary I think it should get relevant |
To clarify, with |
The main thing I am worried about with returning real DOM nodes is that people will expect children to be there and the full hierarchy to be rendered accurately, and that won't be the case. |
I mean both. Let user specify their own Separately, provide an implementation of it that returns something with methods like on DOM node (just so it doesn’t throw in most cases). This can be a third party package at first, but if it’s good enough and most people use it, we might as well ship it in It wouldn’t make sense for React Native though which is why I don’t want to just make it the default. Keeping it separate also lets user add special cases: ReactTestRenderer.create(el, {
getMockRef(el) {
if (el.type === 'input' && el.props['data-my-special-snowflake'])
return getMyWeirdInstance(el);
return getDOMMockRef(el); // could even look at `el.type` to determine methods to include
}
) |
So my question is, if they didn't provide any |
I think default This is slightly safer (no one ever expects refs to be nulls). It would solve the issue with |
Can we just fix the warning somehow first? My initial impression is that {} is not better than null. You'll likely go a little longer before noticing that the methods are missing and that's likely to be more confusing. |
^^ good point, agree. |
If you attach a
ref
to an element using a string (ref='foo'
) and useReactTestRenderer
you get the"Stateless function components cannot be given refs"
warningSee #7371 (comment)
This is because
getPublicInstance
returnsnull
andattachRef
requires that the instance be non-null in the warning invariant.I think we can either return a simple instance instead of
null
or use some additional check inattachRef
to see if we're dealing withReactTestRenderer
. The first option seems reasonable, given the comment in the current code:cc @spicyj
The text was updated successfully, but these errors were encountered: