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

Can't pass on Context when using with react-redux's connect #436

Closed
5punk opened this issue Jun 3, 2016 · 8 comments
Closed

Can't pass on Context when using with react-redux's connect #436

5punk opened this issue Jun 3, 2016 · 8 comments

Comments

@5punk
Copy link

5punk commented Jun 3, 2016

Hi Guys,
I've a bit of a situation, when using enzyme for rendering React Components for my unit tests, I cannot pass over the context when I am exporting the component using react-redux's connect.

Let me show you what I mean:

Component:

class TestPage extends Component {
  contextType = {
     a: required
  }
  render() {
    return (<div>1</div>);
  }
}
export default TestPage;

Test:

import { shallow } from 'enzyme';

wrapper = shallow(<TestPage>, {
  context: {
    a: 10
  }
});

This WORKS!

BUT:

Component:

class TestPage extends Component {
  contextType = {
     a: required
  }
  render() {
    return (<div>1</div>);
  }
}
export default connect( state => ({ a: state.runtime.a }))(TestPage);

Test:

import { shallow } from 'enzyme';

wrapper = shallow(<TestPage>, {
  context: {
    a: 10
  }
});

Spits out: Warning: Failed Context Types: Required context awas not specified inTestPage``

Help... How do I pass on my context through Redux's connect function?

@MQuy
Copy link

MQuy commented Jun 4, 2016

@5punk it's the same as my issue #435

@aweary
Copy link
Collaborator

aweary commented Jun 4, 2016

@MQuy can you try defining a childContextTypes as well? It should work then. I use redux-mock-store and enzyme at work and this works fine.

wrapper = shallow(<TestPage>, {
  context: {
   store: mockStore
  },
  childContextTypes: {
    store: React.PropTypes.Object
  }

@MQuy
Copy link

MQuy commented Jun 4, 2016

@aweary I tried, it's still the same, just tested in this repo https://github.com/MQuy/redux-store-enzyme

@aweary
Copy link
Collaborator

aweary commented Jun 4, 2016

@MQuy this is exactly what we do internally where I work, so I'll have to try and reproduce when I'm back home to see what's going on 👍

@MQuy
Copy link

MQuy commented Jun 4, 2016

thank @aweary 👍

@5punk
Copy link
Author

5punk commented Jun 7, 2016

Does rewiring react-redux's connect function make sense?
I was trying to do something like..

import TestPage, { __RewireAPI__ as TestPageRewired } from '../TestPage';

// Rewire React-redux, connect API
  TestPageRewired.__Rewire__('connect', (props) => {
    console.log('Rewired Connect');
    return {
      contextTypes: [{ requiredContext: () => {} }],
      ...props
    };
  });

But this isn't working for me. Am I missing something?

@blainekasten
Copy link
Contributor

blainekasten commented Jun 27, 2016

closing in favor of #472

@clayrisser
Copy link

There's an npm module that makes it really easy to add redux to the react context

https://github.com/jamrizzi/redux-context-provider
https://www.npmjs.com/package/redux-context-provider

import React, { Component } from 'react';
import ReduxContextProvider from 'redux-context-provider';
import createStore from './createStore';
import actions from './actions';
import Routes from './routes';

export default class App extends Component {
  render() {
    return (
      <ReduxContextProvider store={store} actions={actions}>
        <Routes />
      </ReduxContextProvider>
    );
  }
}

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

5 participants