-
Notifications
You must be signed in to change notification settings - Fork 46.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
Implement createNodeMock for ReactTestRenderer #7649
Conversation
@@ -20,6 +20,10 @@ var getHostComponentFromComposite = require('getHostComponentFromComposite'); | |||
var instantiateReactComponent = require('instantiateReactComponent'); | |||
var invariant = require('invariant'); | |||
|
|||
type TestRendererMockConfig = { | |||
getMockRef: (element: ReactElement) => 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.
Not sure how this would be typed, is there a better way to type an object that looks like a DOM node?
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.
Looks fine to me.
FYI this has some issues with refs rendered by nested components, still working that out |
Hmm, I'm not sure how I can efficiently provide access to the |
I haven't looked at the diff in detail but I think you can make it have a custom transaction (like server rendering does). Then you can read the config from the transaction since it's threaded all the way down. An alternative is to put context on TopLevelWrapper but this is probably more hacky. |
Yeah I tried TopLevelWrapper first, but it's not actually set on all instances. It only seems to be set on instances at the top of the render tree. I'll give the transaction approach a try. |
Context should propagate downwards too so it must be something else. Regardless transaction seems to be a better approach. |
For I'll look into the transactional approach today 👍 |
Does |
Oh, no, sorry I was just responding to
|
One issue I see with trying to use the transaction to propagate the mock config, is that |
Let's pass it in there? |
Actually it should be easier to save mock config from transaction in mountComponent as an instance field. Then access it anytime. Would that work? |
Does it make sense to use the Also, Is there a guarantee that Global injection makes this tricky 😄 |
Maybe I misunderstand how it works but I thought transaction is created on every root mount. For example server rendering creates a transaction (passing |
I'm probably the one misunderstanding, but from what I can see it injects the transaction class in ReactUpdates.injection.injectReconcileTransaction(
ReactTestReconcileTransaction
); Then the actual var transaction = ReactUpdates.ReactReconcileTransaction.getPooled();
var image = transaction.perform(
mountComponentIntoNode,
null,
componentInstance,
transaction
);
ReactUpdates.ReactReconcileTransaction.release(transaction); but the way |
Dynamic injection is a mess. Sorry. |
Yes it is, and no sorry needed 🤘 It's at the very least a fun challenge |
Great job. |
* Implement optional mockConfig and getMockRef * default mockConfig, walk render tree * Pass mockConfig to transaction * Attach mockConfig to transaction * type mockConfig in ReactRef * Expect object in native component ref test * Fix argument name for attachRefs * Add mockConfig support to legacy refs * Pass transaction to getPublicInstance * Implement getMockConfig on ReactTestReconcileTransaction * Merge defaultMockConfig and mockConfig options * Rename mockConfig to testOptions * Break getPublicInstnce into three lines * createMockRef -> createNodeMock (cherry picked from commit f3569a2)
This lets users pass in an optional
testOptions
object as the second argument toReactTestRenderer.create
that allows for custom mocking behavior. As of now that only includescreateNodeMock
, which accepts the current element and should return a mock ref object.The
testOptions
is stored on the component instance so it doesn't leak between instances.cc @gaearon @spicyj