0

Am new in writing testcases using React Test library.

Here is my component

    import React from 'react';
    import PropTypes from 'prop-types';
    import { connect } from 'react-redux';
    class MyContainer extends React.Component {
      static propTypes = {
        graphicalData: PropTypes.object.isRequired,
      };
      render() {
        const { graphicalData } = this.props;
        return (
         graphicalData && (
            <div>
             /////some action and rendering
        </div>
        ))}
        }
        const mapStateToProps = (state) => {
      return {
        graphicalData: state.design.contents ? state.design.contents.graphicalData : {},
      };
    };
    const mapDispatchToProps = (dispatch) => ({});
    export default connect(mapStateToProps, mapDispatchToProps)(MyContainer)));

So i am writing my test case file using React Testing library

    import React from 'react';
    import '@testing-library/jest-dom';
    import { render, cleanup, shallow } from '@testing-library/react';
    import MyContainer from '../../MyContainer';
    import configureMockStore from 'redux-mock-store';
    import ReactDOM from 'react-dom';
    const mockStore = configureMockStore();
    import { Provider } from 'react-redux';
    
    
    const store = mockStore({
        state: {
            design: {
              contents: {
                graphicalModel: { cars: [{},{}], bikes: [{},{}] },
              },
            },
          },
    });
    
    afterEach(cleanup);
    
    it('renders without crashing', () => {
      const div = document.createElement('div');
      ReactDOM.render(
        <Provider store={store}>
          <MyContainer />
        </Provider>,
        div
      );
      ReactDOM.unmountComponentAtNode(div);
    });

Am not sure what went wrong , my idea is to check if the component loads without crashing , also if the cars array length is greater than 0 , check something rendered on page.

But am getting some error, any help with example or suggestion will save my day

Coder
  • 69
  • 8

1 Answers1

0

The error seems correct. Check the structure that you have passed into the mockStore() function. It is

state: {
  design: {
    contents: {
      graphicalModel: { cars: [{},{}], bikes: [{},{}] },
    },
  },
},

So, when you access in your MyContainer, you should access it as

state.state.design.contents

It is just that you have an extra hierarchy in the state object.

Or, if you don't want to change the component, change the structure of the state passed into your mockStore() method as:

const store = mockStore({
  design: {
    contents: {
      graphicalModel: { cars: [{},{}], bikes: [{},{}] },
    },
  },
});
Bms bharadwaj
  • 483
  • 5
  • 18
  • see am rendering my MyContainer correctly , but am having issue while running test cases . When i run npm test , am getting error like cannot read property contents of undefined – Coder Jan 20 '22 at 02:46
  • I meant the same. Inside your `mapStateToProps`, do this: `state.state.design.contents`. It's because your test method is passing an extra hierarchical key to the component. – Bms bharadwaj Jan 20 '22 at 04:17
  • still am not able to understand why you are saying to change in component rather than test file. would you mind to help with fiddle or running example – Coder Jan 20 '22 at 09:38
  • :) I have updated the answer with an alternate approach, where I am not changing your component. Instead, the state fed into the `mockStore()` method is changed. – Bms bharadwaj Jan 20 '22 at 10:01