-
Notifications
You must be signed in to change notification settings - Fork 45.9k
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
Should not rely on synchronous return value of renderIntoDocument #6756
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,14 +80,38 @@ function findAllInRenderedTreeInternal(inst, test) { | |
* @lends ReactTestUtils | ||
*/ | ||
var ReactTestUtils = { | ||
renderIntoDocument: function(instance) { | ||
renderIntoDocument: function(element) { | ||
var div = document.createElement('div'); | ||
// None of our tests actually require attaching the container to the | ||
// DOM, and doing so creates a mess that we rely on test isolation to | ||
// clean up, so we're going to stop honoring the name of this method | ||
// (and probably rename it eventually) if no problems arise. | ||
// document.documentElement.appendChild(div); | ||
return ReactDOM.render(instance, div); | ||
return ReactDOM.render(element, div); | ||
}, | ||
|
||
renderIntoDocumentAsync: function(element) { | ||
invariant(typeof element.ref !== 'string', | ||
'String refs can not be used at the top-level element of a render.' | ||
); | ||
var promise = new Promise(function(fulfill, reject) { | ||
var div = document.createElement('div'); | ||
// None of our tests actually require attaching the container to the | ||
// DOM, and doing so creates a mess that we rely on test isolation to | ||
// clean up, so we're going to stop honoring the name of this method | ||
// (and probably rename it eventually) if no problems arise. | ||
// document.documentElement.appendChild(div); | ||
var oldRef = element.ref; | ||
var newRef = (instance) => { | ||
if (oldRef) { | ||
oldRef(instance); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test ensuring the exact expected behavior around refs is defined. eg, right now string refs are still a thing but you'll get runtime errors if you use them here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String refs can't be used at the top level, so it could never be a string ref, but yeah. |
||
} | ||
fulfill(instance); | ||
}; | ||
element = React.cloneElement(element, {ref: newRef}); | ||
ReactDOM.render(element, div); | ||
}); | ||
return promise; | ||
}, | ||
|
||
isElement: function(element) { | ||
|
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 think we can safely break away from the
renderIntoDocument
misnomer for something new. Might make sense to renamerenderIntoDocument
(with forwarding) at the same time.renderDetached
? Or if we want a mouthfulrenderIntoDetachedNode
. And then the Promise version… I guess...Async
is ok. I still think of "async" functions as taking a callback but I guess Promises are a thing and that's how async/await works.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.
We will also eventually need a way to render into a container asynchronously.
What about
ReactTestUtils.renderAsync()
which optionally accepts the second argument (container
) or renders into a detached node if no such container exists.renderDetached
seems verbose, since 99.99% of the time, you don't care that the node is detached. You just want to render something.Was just an idea. I truly don't have a strong preference here. Just let me know in a more definitive way when everyone is done bikeshedding and then I'll do the global replace.
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.
@sebmarkbage @spicyj @gaearon Do you guys want to join the bikeshedding on the function name?