0
votes

I’m in a situation where I need to unit-test a stateful component which is also connected to redux store, for dispatching events. I'm using Jest and Enzyme.

I’ve tried the various examples available in the documentation: https://react.i18next.com/misc/testing

None of the examples cover the use case of using redux-connect.

Consider the following react component in pseudo code:

// ../MyComponent.js

import React, { Component } from 'react';
import { bindActionCreators, compose } from 'redux';
import { connect } from 'react-redux';
import { withNamespaces } from 'react-i18next';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    // init state…
  }
  // Code body…
}

// Using compose from Redux lib
export default compose(
  connect(null, mapDispatchToProps),
  withNamespaces('myNamespace'),
)(MyComponent);

Unit-test:

// ../__tests__/MyComponent.test.js

import React from 'react';
import { shallow, mount } from 'enzyme';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import { I18nextProvider } from 'react-i18next';

import i18n from '../../i18nForTests';
import reducers from '../../reducers';
import MyComponent from '../MyComponent';

const createStoreWithMiddleware = applyMiddleware()(createStore);

let MyComponentForTest = (
  <Provider store={createStoreWithMiddleware(reducers)}>
    <I18nextProvider i18n={i18n}>
      <MyComponent />
    </I18nextProvider>
  </Provider>
);

describe('MyComponent state', () => {
    let myPage;
    test('init', () => {
      myPage = shallow(MyComponentForTest); <-- Not getting the actual component.
    });
});

Using the following dependencies

"react": "^16.6.1",
"react-app-polyfill": "^0.1.3",
"react-dev-utils": "^6.0.5",
"react-dom": "^16.6.1",
"react-i18next": "^8.3.8",
"react-inlinesvg": "^0.8.2",
"react-redux": "^5.1.1",
"redux": "^4.0.1",
"i18next": "^12.1.0",
"identity-obj-proxy": "3.0.0",
"jest": "23.6.0",
"jest-pnp-resolver": "1.0.1",
"jest-resolve": "23.6.0",
"webpack": "4.19.1",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.7.0",

I’m not able to retrieve the actual component when using the shallow or mount function, from Enzyme. Instead I’m getting the wrapped contexts, either the Provider, a Context object or something else -- This means I can’t test the component.

For clarification -- This is what my unit test looked like before adding the react-i18next lib…

export in MyComponent.js

export default connect(null, mapDispatchToProps)(MyComponent); <-- Note, not using compose as it is not needed.

Unit test, MyComponent.test.js

let MyComponentForTest = <MyComponent store={createStoreWithMiddleware(reducers)} />;

describe(’MyComponent', () => {
  describe('initially', () => {
    let myPage;

    test('init', () => {
      myPage = shallow(MyComponentForTest).dive(); <-- Note, using dive(), to get the component.
      console.log('*** myPage: ', myPage.debug());
    });
});

In the terminal:

<MyComponent store={{...}} redirectPage={[Function]} />

So, Im' clearly able to get the component when not using react-i18next.

I think at least part of the problem is that withNamespaces() does not return the component, instead it wraps it in some sort of Context-object...

1

1 Answers

0
votes

Well, my suggestion here would be to export the MyComponent and pass it mocked state as props. Something like this:

const mockedState = {
    ...state data you want to pass
}

const MyComponentForTest = <MyComponent state={mockedState} />


describe('MyComponent state', () => {
    let myPage;
    test('init', () => {
      myPage = shallow(MyComponentForTest);
    });
});

The whole point here is that how your component behaves on a certain state that is passed which what you want to test here. This should serve the purpose here.