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 doesn't work properly with redux connect(store) #435

Closed
MQuy opened this issue Jun 3, 2016 · 13 comments
Closed

shallow doesn't work properly with redux connect(store) #435

MQuy opened this issue Jun 3, 2016 · 13 comments

Comments

@MQuy
Copy link

MQuy commented Jun 3, 2016

related to this issue reduxjs/redux#1534, #183

Not sure is this correct place to post, but i encounter a bug when using enzyme with redux
Could not find "store" in either the context or props .... Either wrap the root component in a <Provider>, or explicitly pass "store"

Here is my test

describe("(Component) CoreLayout", () => {
  let _wrapper, _props

  beforeEach(() => {
    _props = {
      children: <anchor/>
    }
    _wrapper = shallow(<CoreLayout {..._props}/>, { context: { store } })
  })

  it('Should render Header', () => {
    console.log(_wrapper.html());
    expect(_wrapper.find('Header')).to.have.length(1)
  })

  it('Should render Footer', () => {
    expect(_wrapper.find('Footer')).to.have.length(1)
  })
})

Header.js

export class Header extends Component {
  static contextTypes = {
    router: PropTypes.object
  }
  render() {
    const { currentUser } = this.props;

    return (
      <div>
        {currentUser.fullName}
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    currentUser: state.currentUser
  }
}

I also created my example repo
https://github.com/MQuy/redux-store-enzyme

@5punk
Copy link

5punk commented Jun 3, 2016

I am using redux-mock-store for my unit tests.

import configureStore from 'redux-mock-store';
const mockStore = configureStore();
const dispatch = sinon.spy();

const wrapper = shallow(
    <Test dispatch={dispatch} store={mockStore({ runtime: {} })}
      testData={fakeTestData}
    />
  );

Does this work for you?

@MQuy
Copy link
Author

MQuy commented Jun 4, 2016

@5punk I use redux-mock-store, too 😄

@MQuy
Copy link
Author

MQuy commented Jun 4, 2016

Not sure it's correct or not, but I think it's related to our problem
https://github.com/facebook/react/blob/master/src/test/ReactTestUtils.js#L434
The context is passed to that function but, it doesn't work

@eloleon
Copy link

eloleon commented Jun 23, 2016

This issue is really painful as we need to use react-addons for testing in mentioned scenario when child component is connected. In my opinion Unit Test should not pass store via context - we just to test component as a atomic unit.

@blainekasten
Copy link
Contributor

blainekasten commented Jun 27, 2016

closing in favor of #472

@vladCovaliov
Copy link

You can also use dive(). Thus _wrapper.dive().find('Footer')

@mssroboto
Copy link

What we want to test is the actual component itself, so you need to export that and import as
{myComponent} on yourComponent.test

@ljharb
Copy link
Member

ljharb commented Apr 27, 2017

@Zzamanta yes, .dive() lets you do that without having to export/import the inner component.

@jewertow
Copy link

@Zzamanta thanks! It solved my problem.

@benkermode
Copy link

benkermode commented Nov 19, 2018

@ljharb, managed to my connected, HOC'd component to test like so:

wrapper = shallow(
            <Provider store={store}><SignIn {...props} /></Provider> 
).dive({ context: { store } }).dive();

But componentDidMount() is not triggered. Using mount does trigger componentDidMount() though. Any idea why? enzyme@3.7.0

@ljharb
Copy link
Member

ljharb commented Nov 20, 2018

Do you have disableLifecycleMethods set anywhere, like perhaps in your configure call?

@benkermode
Copy link

benkermode commented Nov 20, 2018

Do you have disableLifecycleMethods set anywhere, like perhaps in your configure call?

No. It seems I didn't dive deep enough. <Provider> was wrapping <SignIn>, which was wrapped by my own HOC, which was wrapped by connect.
I found that componendDidMount was in fact triggered when I used

wrapper = shallow(
            <Provider store={store}><SignIn {...props} /></Provider> 
).dive({ context: { store } }).dive().dive();

Notice the extra .dive() at the end. I think mount will actually be a better choice as it is properly re-rendering when props are updated in componentDidMount during the test. Thanks @ljharb!

@rghose
Copy link

rghose commented Sep 11, 2019

What we want to test is the actual component itself, so you need to export that and import as
{myComponent} on yourComponent.test

This was the easiest solution! However, one also needs to export the corresponding class that is being imported.

export class myComponent extends Component {

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

10 participants