【问题标题】:React-Redux Connected Component testing correct action has been dispatch on click eventReact-Redux Connected Component 测试正确操作已在点击事件上分派
【发布时间】: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


    【解决方案1】:

    你快到了,这对我有用。
    例如

    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 withProvider = (
                <Provider store={store}>
                    <loginDefault />
                </Provider>
            );
    
        const wrapper = mount(withProvider);
        let loginButton = wrapper.find('button'); //here you can use other selector like #myButton or .btn, but be sure it can find it.
        loginButton.simulate('click');
        
        const unsubscribe = store.subscribe(() => {	    
                        const actions = store.getActions();
    		    //here your expectation after the action is raised
                        expect(actions.length).to.have.length.above(0);
                    });
    
        expect(store.getActions().length).toBe(0);    
    
    });

    希望对您有所帮助。

    【讨论】:

    • 谢谢,不走运。由于某种原因,我的订阅未被调用:我收到以下错误消息:警告:道具类型失败:按钮中未指定必需的道具“onpress”。但是在 loginDefault 组件中作为一个函数存在。
    猜你喜欢
    • 2018-06-19
    • 2018-06-18
    • 2021-05-08
    • 2023-02-04
    • 2023-01-30
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多