unit-testing - Redux:如何测试连接的组件?

标签 unit-testing reactjs redux enzyme

我正在使用 Enzyme 对我的 React 组件进行单元测试。我知道,为了测试原始的未连接组件,我必须导出它并测试它(我已经这样做了)。我已经设法为连接的组件编写了一个测试,但我真的不确定这是否是正确的方法,以及我到底想测试连接的组件什么。

Container.jsx

import {connect} from 'react-redux';
import Login from './Login.jsx';
import * as loginActions from './login.actions';

const mapStateToProps = state => ({
  auth: state.auth
});
const mapDispatchToProps = dispatch => ({
  loginUser: credentials => dispatch(loginActions.loginUser(credentials))

});
export default connect(mapStateToProps, mapDispatchToProps)(Login);

Container.test.js

import React from 'react';
import {Provider} from 'react-redux';
import {mount, shallow} from 'enzyme';
import {expect} from 'chai';
import LoginContainer from '../../src/login/login.container';
import Login from '../../src/login/Login';


describe('Container Login', () => {
  it('should render the container component', () => {
    const storeFake = state => ({
      default: () => {
      },
      subscribe: () => {
      },
      dispatch: () => {
      },
      getState: () => ({ ...state })
    });
    const store = storeFake({
      auth: {
        sport: 'BASKETBALL'
      }
    });

    const wrapper = mount(
      <Provider store={store}>
        <LoginContainer />
      </Provider>
    );

    expect(wrapper.find(LoginContainer).length).to.equal(1);
    const container = wrapper.find(LoginContainer);
    expect(container.find(Login).length).to.equal(1);
    expect(container.find(Login).props().auth).to.eql({ sport: 'BASKETBALL' });
  });
});

最佳答案

这是一个有趣的问题。

我通常会导入容器和组件来进行测试。对于容器测试,我使用 redux-mock-store。组件测试用于测试异步函数。例如,在您的情况下,登录过程是使用 sinon stub 的异步函数。这是相同的片段,

import React from 'react';
import {Provider} from 'react-redux';
import {mount, shallow} from 'enzyme';
import {expect} from 'chai';
import LoginContainer from '../../src/login/login.container';
import Login from '../../src/login/Login';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { stub } from 'sinon';

const mockStore = configureMockStore([thunk]);

describe('Container Login', () => {
  let store;
  beforeEach(() => {
    store = mockStore({
      auth: {
        sport: 'BASKETBALL',
      },
    });
  });
  it('should render the container component', () => {
    const wrapper = mount(
      <Provider store={store}>
        <LoginContainer />
      </Provider>
    );

    expect(wrapper.find(LoginContainer).length).to.equal(1);
    const container = wrapper.find(LoginContainer);
    expect(container.find(Login).length).to.equal(1);
    expect(container.find(Login).props().auth).to.eql({ sport: 'BASKETBALL' });
  });

  it('should perform login', () => {
    const loginStub = stub().withArgs({
      username: 'abcd',
      password: '1234',
    });
    const wrapper = mount(<Login
      loginUser={loginStub}
    />);
  wrapper.find('button').simulate('click');
  expect(loginStub.callCount).to.equal(1);
  });
});

关于unit-testing - Redux:如何测试连接的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39006563/

相关文章:

javascript - 如何使用 JQuery 填充 React 生成的输入字段

javascript - 状态设置方法没有影响主对象 - ReactJS

angular - Jasmin + karma : "Error: Unexpected value ' HttpClient' imported by the module 'DynamicTestModule' . 请添加@NgModule 注解。”

javascript - 针对不同浏览器的 JavaScript 中的一般单元测试概念/实践?

reactjs - Flow : destructuring. React/Preact 中缺少注释

javascript - 非标准化数据如何导致 React Redux 出现问题?

javascript - 与 redux 一起使用像 SPA 这样的rest-api 的好解决方案吗?

reactjs - 如何在 react 中从 reducer 状态添加和删除项目

unit-testing - 使用Resharper和Phantom JS调试 Jasmine 测试

Python单元测试跳过主应用程序中的特定步骤