2017-08-15 121 views
4

识别存储我有连接的终极版一个摩卡齐测试阵营组件。为了通过Redux的商店测试组件,我在测试文件创建,并把它作为一个道具,但测试引发以下错误:阵营摩卡齐测试不从丙

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

}); 

这是阵营组成:

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

你可以尝试使用'Provider'并传递它存储。 – nrgwsth

+0

'<提供者储存= {存储}><项目... otherprops />' – nrgwsth

+0

是的,我也尝试添加提供程序和传递店。同样的错误。 – mhatch

回答

0

为什么你用connectProject组件,而不mapStateToPropsmapDispatchToProps的功能呢?

不过......这不是必要的测试包裹组件为connect。 您所需要的只是测试素材Project组件。

如何导出两个组件? - 请在这个问题上关注此link

0

要解决此问题,你可以做以下;

安装一个叫做终极版 - 模拟店npm install redux-mock-store

设置店里像这样的库:

import configureStore from 'redux-mock-store'; 

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

添加要包括在商店,如任何中间件。终极版-的thunk,终极版,传奇等

定义您的初始状态,并建立您的商店如下:

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

});