小编som*_*eok的帖子

jest:模拟函数中没有调用Mock函数

我想测试我的react login组件,但是还没有调用mock函数:

登录:

export default class LoginPage extends PureComponent {
    static propTypes = {

        login: PropTypes.func.isRequired,
        history: PropTypes.shape({
            replace: PropTypes.func.isRequired,
        }).isRequired,
    };

    onSubmit = (e) => {
        this.props.login(this.state.username, this.state.password)
            .then(() => {

                this.props.history.replace('/');
            });
    };

    render() {
        return (
            <form onSubmit={this.onSubmit}>
                ...
            </form>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

我用jest+ enzyme来测试这个:

const props = {
    login: jest.fn(() => Promise.resolve('success')),
    history: {
        replace: jest.fn()
    },
};

const wrapper = mount(<LoginPage {...props}/>);

const form = wrapper.find(Form);
const inputs = form.find('input');
const username …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing promise reactjs jestjs

3
推荐指数
1
解决办法
4453
查看次数

标签 统计

javascript ×1

jestjs ×1

promise ×1

reactjs ×1

unit-testing ×1