监视智能组件中的导出功能

时间:2017-09-06 19:04:56

标签: reactjs sinon jestjs enzyme

我想测试调度在我的智能组件中调用正确的函数。但是我只是测试了该函数的返回值。是否可以模拟,间谍或存根函数?

我的组件:

import { connect } from 'react-redux';
import { fetchDocumentsIfNeeded } from '../actions';
import SearchComponent from '../components/Search';

const mapDispatchToProps = (dispatch) => {
    return {
        triggerSearch: (event) => {
            dispatch(fetchDocumentsIfNeeded(event.target.value));
        }
    };
};

export default connect(
    mapDispatchToProps
)(SearchComponent);

到目前为止我的测试:

import React from 'react';
import Search from '../../src/containers/Search';
import {shallow} from 'enzyme';
import {spy} from 'sinon';
import configureMockStore from 'redux-mock-store';

const mockStore = configureMockStore();

describe('containers/Search', () => {
    let wrapper, store;

    beforeEach(() => {
        store = mockStore({searchedText: "fun"});
        store.dispatch = spy();
        wrapper = shallow(<Search store={store} />);
    });

    it('maps triggerSearch to dispatch search action', () => {
        const event = {target: {value: "test"}};
        wrapper.props().triggerSearch(event);

        expect(store.dispatch.calledWithMatch({text: "test", type: 'SEARCH_DOCUMENTS'})).toEqual(true);
    });
});

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案:

{
    "files.insertFinalNewline": true
}