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 component with getChildContext should work #664

Closed
ljharb opened this issue Nov 7, 2016 · 5 comments
Closed

shallow-rendering component with getChildContext should work #664

ljharb opened this issue Nov 7, 2016 · 5 comments

Comments

@ljharb
Copy link
Member

ljharb commented Nov 7, 2016

class Foo extends React.Component {
  getChildContext() {
    return { a: 1 };
  }
  render() {
    return <Bar b={this.context.b} />;
  }
}
Foo.contextTypes = {
  b: React.PropTypes.number,
};

class Bar extends React.Component {
  render() {
    return <div />;
  }
}
Bar.contextTypes = {
  a: React.PropTypes.number,
  b: React.PropTypes.number,
};
const wrapper = shallow(<Foo />, { context: { b: 2 });

expect(wrapper).props()).to.eql({ b: 2 }); // works
// .props() provides me with the first argument that will be passed to `Bar`'s constructor

expect(wrapper).context()).to.eql({ a: 1, b: 2 }); // does not work
// I expect .context() to provide me with the second argument that will be passed to `Bar`'s constructor
@neville1355
Copy link

I'll take a try at this one

@neville1355
Copy link

neville1355 commented Dec 9, 2016

I took a stab at this but I'm not sure it's a real bug. @ljharb why would you expect context to have both a and b populated?

https://jsfiddle.net/6jc2o6vv/

This fiddle (I believe) represents what you are trying to render in an actual react environment and the behaviour exhibited by enzyme appears to be correct.

@ljharb
Copy link
Member Author

ljharb commented Dec 9, 2016

@jordann-nulogy React only provides context keys for which there are contextTypes, and yes, what React is doing is correct. However, enzyme will not in fact replicate this when shallow-rendering ContextComponent with a context of { b: 2 } and then attempting to determine the context of the ChildContextComponent element (or, to shallow-render it, and have it inherit the context of { a: 1, b: 2 })

@ljharb
Copy link
Member Author

ljharb commented Jul 1, 2017

@lelandrichardson can we make sure this works with your rewrite?

@ljharb ljharb marked this as a duplicate of #1053 Jul 28, 2017
@borela
Copy link

borela commented Sep 13, 2017

Any updates on this one?

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

No branches or pull requests

3 participants