Mocha-chai test reaction not recognized in store

I have a Mocha-chai test in a React component related to Reux. To transfer Redux storage to a test component, I create it in a test file and pass it as a support, but the following error occurs as a result of the test:

Invariant violation: could not find the "repository" in the context or the details of "Connect (Project)". Either wrap the root component in <Provider>, or explicitly pass the "repository" as a support for "Connect (project)".

Here is the test:

import React from 'react';
import ReactDOM from 'react-dom';
import { 
  renderIntoDocument,
  scryRenderedComponentsWithType
} from 'react-dom/test-utils';
import Project from '../../src/components/Project';
import { expect } from 'chai';
import { createStore } from 'redux';
import reducer from '../../src/reducers/reducers';

const store = createStore(reducer);

const component = renderIntoDocument(
  <Project 
    store={ store } 
    project={
      {
        "name": "MyName",
        "img": "path.jpg",
        "img_alt": "alt desc",
        "description": "lorem ipsum",
        "github": "repository",
        "link": "website.com"
      }
    } />
);

describe('Project', () => {

  // tests ...

});

This is the React component:

import React from 'react';
import ProjectImage from './ProjectImage';
import ProjectText from './ProjectText';
import { connect } from 'react-redux';
import * as actions from '../actions/actions';

export const Project = React.createClass({

  getProject: function() {
    return this.props.project || {};
  },

  handleClick: function(event) {
    this.props.dispatch(actions.showModal(true));
    this.props.dispatch(
      actions.setModalContent(this.getProject())
    );
  },

  render: function() {
    return (
      <div className="project">

        <ProjectImage 
          img={ this.getProject().img } 
          imgAlt={ this.getProject().img_alt }
          link={ this.getProject().link } />

        <ProjectText 
          projectName={ this.getProject().name } 
          tagline={ this.getProject().tagline } 
          description={ this.getProject.description }
          github={ this.getProject().github }
          webLink={ this.getProject().link } 
          openModal={ this.handleClick } />

      </div>
    );
  }

});

export default connect()(Project);
+4
source share
2 answers

connect Project mapStateToProps mapDispatchToProps ?

... connect. , , Project.

? - , .

0

, :

redux-mock-store npm install redux-mock-store

:

import configureStore from 'redux-mock-store';

const middlewares = [];
const mockStore = configureStore(middlewares);

, , . , - ..

:

initialState = {}
const store = mockStore(initialState);

:

const component = renderIntoDocument(
  <Project 
    store={ store } 
    project={
      {
        "name": "MyName",
        "img": "path.jpg",
        "img_alt": "alt desc",
        "description": "lorem ipsum",
        "github": "repository",
        "link": "website.com"
      }
    } />
);

describe('Project', () => {

  // tests ...

});
0

Source: https://habr.com/ru/post/1683763/


All Articles