简体   繁体   English

在React / Redux中测试连接的组件

[英]Test connected component in React/Redux

I am trying test my connected component of my React/Redux app and I wrote some test case which actually throws the error: 我正在尝试测试我的React / Redux应用程序的已连接组件,并且编写了一些实际引发错误的测试用例:

App component › shows account info and debits and credits`

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

The test case which trow an error app.test.js is below. 引发错误app.test.js如下。 And my problem is that I don't understand what should I wrap here by Connect() because I didn't use AccountInfo here: 我的问题是我不明白应该通过Connect()在这里包装什么,因为我没有在这里使用AccountInfo

import React from 'react';
import { mount } from 'enzyme';
import { Provider } from 'react-redux';
import App from './App';
import * as actions from '../../actions';

function setup() {
  const props = {
    errorMessage: null,
    actions
  };

  const enzymeWrapper = mount(<App {...props} />);

  return {
    props,
    enzymeWrapper,
  };
}

describe('App component', () => {
  it('shows account info and debits and credits`', () => {
    const {enzymeWrapper} = setup();
    expect(enzymeWrapper.find('.account-info').exists()).toBe(true);
    expect(enzymeWrapper.find('.debits-and-credits').exists()).toBe(true);
  });

  it('shows error message', () => {
    const {enzymeWrapper} = setup();
    enzymeWrapper.setProps({ errorMessage: 'Service Unavailable' });
    expect(enzymeWrapper.find('.error-message').exists()).toBe(true);
  });
});

My containers/app.js : 我的containers/app.js

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as actions from '../actions';
import AppComponent from '../components/App/App';

const mapStateToProps = state => ({
  isFetching: state.balance.isFetching,
  errorMessage: state.errorMessage,
});

const mapDispatchToProps = dispatch => ({
  actions: bindActionCreators(actions, dispatch),
});

const AppContainer = connect(mapStateToProps, mapDispatchToProps)(AppComponent);

export default AppContainer;

The component app.js : 组件app.js

import React, { Component } from 'react';
import ErrorMessage from '../../containers/ErrorMessage';
import AccountInfo from '../../containers/AccountInfo';
import DebitsAndCredits from '../../containers/DebitsAndCredits';
import './App.css';

const AppComponent = () =>
  <div className="app">
    <AccountInfo />
    <DebitsAndCredits />
  </div>;

export class App extends Component {
  componentWillMount() {
    const { actions } = this.props;
    actions.fetchBalance();
  }

  render() {
    const { errorMessage } = this.props;
    return errorMessage ? <ErrorMessage /> : <AppComponent />;
  }
}

export default App;

UPD: UPD:

I updated my test case and now it looks like: 我更新了测试用例,现在看起来像:

import React from 'react';
import { mount } from 'enzyme';
import { Provider } from 'react-redux';
import configureMockStore from 'redux-mock-store';
import createSagaMiddleware from 'redux-saga';
import { initialState } from '../../reducers/balance/balance';
import App from './App';
import * as actions from '../../actions';

const middlewares = [createSagaMiddleware];
const mockStore = configureMockStore(middlewares);
const store = mockStore(initialState);

function setup() {
  const props = {
    errorMessage: null,
    actions,
  };

  const enzymeWrapper = mount(
    <Provider store={store}>
      <App {...props} />
    </Provider>
  );

  return {
    props,
    enzymeWrapper,
  };
}

describe('App component', () => {
  it('shows account info and debits and credits`', () => {
    const { enzymeWrapper } = setup();
    expect(enzymeWrapper.find('.account-info').exists()).toBe(true);
    expect(enzymeWrapper.find('.debits-and-credits').exists()).toBe(true);
  });

  it('shows error message', () => {
    const { enzymeWrapper } = setup();
    enzymeWrapper.setProps({ errorMessage: 'Service Unavailable' });
    expect(enzymeWrapper.find('.error-message').exists()).toBe(true);
  });
});

And my error now is: 现在我的错误是:

App component › shows account info and debits and credits`

    TypeError: Cannot read property 'account' of undefined

UPD 2: UPD 2:

My initialState which I put when I create mocked store: 我在创建模拟商店时输入的initialState

const initialState = {
  isFetching: false,
  account: {},
  currency: '',
  debitsAndCredits: [],
};

My AccountInfo component: 我的AccountInfo组件:

import React from 'react';

const AccountInfo = ({ account, currency }) =>
  <header className="account-info">
    <p>{account.name}</p>
    <p>
      IBAN: {account.iban}<br />
      Balance: {account.balance}<br />
      Currency: {currency}<br />
    </p>
  </header>;

export default AccountInfo;

For testing the connected component, you need to mock the provider as well, since the connect picks state variables from redux store. 为了测试连接的组件,您还需要模拟提供程序,因为连接会从redux存储中选择状态变量。

Do this 做这个

const enzymeWrapper = mount (<Provider store={mockStore}><App {...props}/></Provider>)

You need to mock the redux store too. 您还需要模拟redux存储。

Edit 1: 编辑1:

Just looking at your AccountInfo component it tells me that you are expecting account in the props here. 只要查看您的AccountInfo组件,它就会告诉我您在此处的道具中期望帐户。

AccountInfo = ({account}) =>

So that means App.js has to pass down the accounts' value in the props. 因此,这意味着App.js必须在道具中传递帐户的值。 Same thing goes for currency. 货币也一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM