jest.fn()值必须是模拟函数或间谍接收函数:[Function getTemplates]

时间:2018-05-29 12:18:10

标签: reactjs unit-testing react-redux jestjs enzyme

我在模块中有一个名为handlelistOfTemplates的函数,它调用另一个文件中定义的动作。我想测试当handlelistOfTemplates被调用时,动作文件中的函数被调用正确的参数。

我的容器组件:

import React from 'react';
import { connect } from 'react-redux';
import {bindActionCreators} from 'redux';

import * as getData from '../../services/DataService';

class Container extends React.Component {

    constructor(props){
        super(props) 
        this.props.actions.getTemplates(1);
        this.state = {
            value: 1
        }

    }

    handlelistOfTemplates = (template, index, value) => {
        this.props.selectedTemplate(template);
        this.setState({ value });
        this.props.actions.getTemplates(template);
    };

    componentDidMount() {
    }

    render() {
        return(

                <ListOfTemplates listOfTemplates={this.props.listOfTemplates} value={this.state.value} onChange={this.handlelistOfTemplates}/>
            );
    }
}
function mapStateToProps({state}) {
    return {
        listOfTemplates: state.listOfTemplates
    }
}
function mapDispatchToProps(dispatch) {
    return {
    actions: bindActionCreators(getData, dispatch)
    };
   }
module.exports = connect(mapStateToProps, mapDispatchToProps)(Container);

我的测试:

import React from 'react';
import sinon from 'sinon';
import expect from 'expect';
import { shallow } from 'enzyme';
import PropTypes from 'prop-types';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import { createMockStore, createMockDispatch } from 'redux-test-utils';

import Container from './Container';
const shallowWithStore = (component, store) => {
    const context = {
        store,
        muiTheme: getMuiTheme(),
    };

    const childContextTypes = {
        muiTheme: React.PropTypes.object.isRequired,
        store: PropTypes.object.isRequired
    }
    return shallow(component, { context, childContextTypes });
};
let store;
const loadComponent = (testState) => {

    const props = {
        actions: {
            getTemplates: () => {return Promise.resolve()}
        }
    }
    store = createMockStore(testState)
    return shallowWithStore(<Container {...props}/>, store);
}

const getFakeState = () => {
    return {
        listOfTemplates: [],

    };
}

describe('Container', () => {
    let testState, component;


    describe("when Appeal template is selected from select template dropdown", () => {
        beforeAll(() => {
            testState = getFakeState();
            component = loadComponent(testState);

        });

        fit('should update the content in editor', (done) => {
            component.dive().find('ListOfTemplates').props().onChange('Appeal', 1, 2);
            component.update();
            done();
            expect(component.dive().state().value).toEqual(2) // error value still is at 1
expect(component.instance().props.actions.getTemplates).toHaveBeenCalled();

        });
    });
});

当我运行上述测试时,我收到以下错误。

expect(jest.fn())[.not].toHaveBeenCalled()

    jest.fn() value must be a mock function or spy.
    Received:
      function: [Function getTemplates]

我是否应该运行其他东西才能让它发挥作用?可能是我的模拟不正确。

即使我尝试这样做: jest.spyon(component.instance()。props.actions,'getTemplates');在预期错误保持不变之前。

此外,当我检查组件的本地状态是否已被修改时。我没有得到更新状态。  当我打电话给component.dive().find('ListOfTemplates').props().onChange('Appeal', 1, 2);

component.dive().state().value应该变为2,但它是1。

你可以帮助我在哪里做错了吗?

1 个答案:

答案 0 :(得分:1)

你应该将模拟函数getTemplate传递给你的组件,否则jest无法检查它是否被调用。

你可以这样做:(注意jest.fn()

 const props = {
        actions: {
            getTemplates: jest.fn(() => Promise.resolve())
        }
    }