javascript – React Mocha-chai测试不识别来自道具的商店

我在Redux连接的React组件上进行了Mocha-chai测试.为了将Redux存储传递给测试组件,我在测试文件中创建它并将其作为prop传递,但测试会抛出以下错误

Invariant Violation: Could not find “store” in either the context or
props of “Connect(Project)”. Either wrap the root component in a
<Provider>,or explicitly pass “store” as a prop to
“Connect(Project)”.

这是测试:

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 ...

});

这是React组件:

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);

解决方法

为什么在没有mapStatetoProps或mapdispatchToProps函数的情况下使用connect for Project组件?

但是……没有必要将包装组件测试到连接中.
您只需要测试普通项目组件.

如何导出这两个组件? – 请在同一问题上关注此link.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...