Skip to content
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

Closed
graue opened this issue Apr 22, 2015 · 5 comments
Closed

Shallow rendering should support context #3721

graue opened this issue Apr 22, 2015 · 5 comments

Comments

@graue
Copy link
Contributor

graue commented Apr 22, 2015

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

@glenjamin
Copy link
Contributor

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?

@glenjamin
Copy link
Contributor

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.

@vegetabill
Copy link

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?

@mmw
Copy link

mmw commented Jul 27, 2015

How would you use shallowRender with ReactTestUtils#Simulate? I'm trying to test interactions on an element that needs context but also needs renderIntoDocument so findRenderedDOMComponentWithClass can find the dom node to simulate events on.

edit: I now realize that this is a naive question and I should be looking for a way to renderIntoDocument with context for the problem I was trying to solve

@graue
Copy link
Contributor Author

graue commented Nov 29, 2015

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants