-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Pass a context to rendered Component's child? #144
Comments
Can you expand on " I'm curious to what you want to do that you can't currently do with the |
Allright. Let's say I have a
but
Now, when I want to mount Is it clear? |
That makes sense. So are you trying to test If so, you could mock out I'm not sure if there is a bug here or not, @lelandrichardson will probably have a better concept of if something is missing here. If I understand correctly, just to reiterate, this is effectively the problem you are having:
So when testing I guess, when I think about it, passing context to the mount/shallow API should be encompassing for all it's children. mount(
<ComponentA />,
{ options: { context: { foo: true, bar: false } }
); And so you're saying this fails for you? |
I may be misunderstanding how context works exactly, but it seems to me like the provided API should be all that is needed. My understanding was that context can be passed down to immediate children as well as deeper children (ie, grandchildren). If this is needed we can add it, but I'm not sure that it is. Do you think you could work up a test that fails that you think should pass? |
Sorry for late reply, so.. To answer @blainekasten's questions: I am trying to unit test ComponentB. ComponentC itself works great. Also shallow rendering ComponentB works allright. But when mounting ComponentB I get following:
Mocking out ComponentC is good idea, but with large project and a lot of cases where this happens it’s a hassle. So in general you got the structure of the components, however the problem is not that I wrote a test case in my fork (@lelandrichardson) : jakubzitny@9094d7c |
So I found where the problem is, I just don't know how to fix it. Yet. In if (options.context && node.type.contextTypes) {
// For full rendering, we are using this wrapper component to provide context if it is
// specified in both the options AND the child component defines `contextTypes` statically.
// In that case, we define both a `getChildContext()` function and a `childContextTypes` prop.
objectAssign(spec, {
childContextTypes: node.type.contextTypes,
getChildContext() {
return this.state.context;
},
});
} Both @lelandrichardson any ideas how to get the context types of all children components without complex traversing or something like that? I was thinking maybe there could be new field |
@jakubzitny I see the problem now. Thanks for the explanation. One thing we could do is add Do you think this could work? If you could write up a quick gist of a failing test case, I can make sure that it works for what you're trying to do... |
The failing test case looks like this: https://gist.github.com/jakubzitny/7b9d526f77b2865219d3 And yeah I think adding it as an option is allright, it's probably the fastest way to fix this. There may be another possibility to "scan" all the children for their contextTypes and do it automatically. That would be a bit harder I think.. I can try to add it and submit a Pull request. |
Not re-opening the ticket but just wanted to add for future readers that using To resolve this, you can simply wrap that root component in a |
I am using
enzyme.mount
to render a Component with some child Components and I need to pass a context to these child Components. I can pass a context withoptions.context
to the Component but I would like to pass something else to Component's children.The options object in
mount
would be good place to do this, however, it is currently not possible. I know I can create my own wrapper with childContextTypes, but for better readability and simplicity of tests, to do this directly withenzyme.mount
would be amazing.What do you think?
The text was updated successfully, but these errors were encountered: