-
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
Shallow rendering should support context #3721
Comments
From what I can tell, this is related to the owner/parent context changes discussed in https://gist.github.com/jimfb/0eb6e61f300a8c1b2ce7 0.13 still uses owner-based contexts, so there's no owner context - but I guess master uses parent-based context so the test passes? |
Here's a little workaround I ended up doing to ensure both owner-context and parent-context is set when using shallow rendering for testing. function shallowRender(makeComponent, context) {
context = context || {};
var ReactContext = require('react/lib/ReactContext');
ReactContext.current = context;
var shallowRenderer = TestUtils.createRenderer();
shallowRenderer.render(makeComponent(), context);
ReactContext.current = {};
return shallowRenderer.getRenderOutput();;
}
// Usage:
var output = shallowRender(() =>
<MyComponent
with={props}
and={stuff}
/>,
{ here: 'is', my: 'context'}
); Note use of the arrow function to delay creation of the component until owner-context can be set. |
I'm trying out the shallow renderer and my components are using ReactIntl. I have to render them like this: shallowRenderer.render(element, { locales: null, messages: null }); so that I won't get an error about there being no locales key in the context during ReactCompositeComponentMixin._maskContext. Is this a general limitation related to this issue or does that sound like something specific to the ReactIntl library? |
How would you use edit: I now realize that this is a naive question and I should be looking for a way to |
Looks like this got fixed at some point. I'm not sure when, but there's been a passing test since #3912 was merged. Yay! |
The shallow renderer's render method currently accepts a second (undocumented) argument,
context
, but values you pass don't actually make it through. If you pass{foo: 'bar'}
the component receives the context object{foo: undefined}
.Related: #2393, #3696
The text was updated successfully, but these errors were encountered: