【发布时间】:2017-06-20 18:31:23
【问题描述】:
我是使用 jest 和酶测试 react-redux 组件的新手,到目前为止,我发现这是一个挑战。
我有一个登录页面,我想测试如果单击登录按钮后登录失败,是否会发送正确的错误操作。
这就是我测试连接组件的方式,
require('../jsDomSetup');
import React from 'react';
import { Provider } from 'react-redux';
import loginDefault, {Login} from '../../app/routes/Login/LoginContainer';
import {mount, shallow} from 'enzyme';
import loginActions from '../../app/redux/actions/loginActions';
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import nock from 'nock';
describe("login container component", function(){
afterEach(() => {
nock.cleanAll();
});
test("loginFailure action is called when login failed due to no crendetials", () =>{
nock('http://192.168.0.2:3000')
.get('/graphql?prettify=true')
.replyWithError('login failed');
// Error message: Incorrect username and password please try again
const mockStore = configureMockStore([thunk]);
const store = mockStore();
const output = mount(<Provider store={store}><loginDefault></loginDefault></Provider>)
let loginButton = output.ref('loginButton');
expect(store.getActions().length).toBe(0);
loginButton.simulate('click');
expect(store.getActions().length).toBe(1);
});
在我运行此测试时,尝试模拟单击时失败,找不到按钮元素。但是这个 ref 存在,当我尝试像按钮这样的简单搜索时,它仍然找不到任何按钮。所以我假设安装正在返回一些无提示的错误或其他东西。
我只想检查登录失败时是否调用了正确的操作。
如何让这个测试通过?
【问题讨论】:
标签: reactjs redux react-redux jestjs enzyme