enzyme icon indicating copy to clipboard operation
enzyme copied to clipboard

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

Open MQuy opened this issue 10 years ago • 13 comments

related to this issue https://github.com/reactjs/redux/issues/1534, https://github.com/airbnb/enzyme/issues/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

MQuy avatar Jun 03 '16 07:06 MQuy

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?

rotatordisk92 avatar Jun 03 '16 18:06 rotatordisk92

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

MQuy avatar Jun 04 '16 02:06 MQuy

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

MQuy avatar Jun 04 '16 02:06 MQuy

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.

eloleon avatar Jun 23 '16 14:06 eloleon

closing in favor of #472

blainekasten avatar Jun 27 '16 19:06 blainekasten

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

vladCovaliov avatar Dec 05 '16 16:12 vladCovaliov

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

mssroboto avatar Apr 27 '17 21:04 mssroboto

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

ljharb avatar Apr 27 '17 22:04 ljharb

@Zzamanta thanks! It solved my problem.

jewertow avatar Jul 31 '18 14:07 jewertow

@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? [email protected]

benkermode avatar Nov 19 '18 23:11 benkermode

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

ljharb avatar Nov 20 '18 14:11 ljharb

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!

benkermode avatar Nov 20 '18 19:11 benkermode

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 {

rghose avatar Sep 11 '19 14:09 rghose