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

.context method not working with stateless components. #180

Closed
mauriciosoares opened this issue Feb 12, 2016 · 3 comments
Closed

.context method not working with stateless components. #180

mauriciosoares opened this issue Feb 12, 2016 · 3 comments
Assignees
Labels

Comments

@mauriciosoares
Copy link
Contributor

Hey guys, I'm trying to use the .context method from the Full DOM rendering API in a stateless component and I'm getting the following error:

  - Error: You cannot get an instance of a stateless component.
        at getInstance (node_modules/enzyme/build/ReactWrapperComponent.js:81:17)
        at ReactWrapper.instance (node_modules/enzyme/build/ReactWrapper.js:143:31)
        at ReactWrapper.<anonymous> (node_modules/enzyme/build/ReactWrapper.js:601:25)
        at ReactWrapper.single (node_modules/enzyme/build/ReactWrapper.js:1066:19)
        at ReactWrapper.context (node_modules/enzyme/build/ReactWrapper.js:600:29)
        at Spec.<anonymous> (src/shared/components/fieldset/__tests__/fields.js:155:27)

I copied the code from enzyme tests, and updated it to support context:

const Foo = ({ foo }, {_}) => (
  <div>
    <div className="bar">bar</div>
    <div className="qoo">{foo}</div>
  </div>
);

Foo.contextTypes = {
  _: React.PropTypes.func
};

const wrapper = mount(<Foo foo="qux" />, {
  context: {
    _: (e) => e
  }
});
console.log(wrapper.context('_'));

Basically I'm trying to pass a context for my stateless component, if I don't run wrapper.context('_') than no errors are thrown. But when I use it then the error occurs.

I don't know if that's the expected behaviour, or if that's happening with othre methods as well.

Cheers

@lelandrichardson
Copy link
Collaborator

Hmm. I wonder what the right way to get at context for SFCs are.

There might be a couple of other methods that call .instance() that don't have to and are failing with SFCs as well. We should increase our coverage for SFCs.

@mauriciosoares
Copy link
Contributor Author

Totally, I'd help with a PR, but I'm too new in enzyme to know what methods should be tested, started using it today :/

But I'll follow up your progress, and hopefully help you with something.

@lelandrichardson lelandrichardson self-assigned this Feb 15, 2016
@blainekasten
Copy link
Contributor

I think this might be fixed by #220

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

No branches or pull requests

3 participants