gpt4 book ai didi

javascript - Jest.fn() 在 React 单元测试中不起作用

转载 作者:行者123 更新时间:2023-11-29 17:51:00 25 4
gpt4 key购买 nike

我正在测试一个名为 handleSubmit 的组件方法(名称无关紧要...)。

测试

// ...imported all modules at the top, including enzyme

it('should submit form data', () => {
let form = shallow(<Form />);
let handleSubmit = jest.fn(); // <= this doesn't work!!

form.find('.submit-btn').simulate('click');

expect(form.find('.submit-btn').length).toEqual(1);
expect(handleSubmit).toHaveBeenCalled();
});

组件

import React, { Component } from 'react';
import axios from 'axios';

class CarnetSidebarForm extends Component {
constructor(props) {
super(props);

this.state = {
title: ''
};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(e) {
const target = e.target;
const value = target.value;
const name = target.name;

this.setState({
[name]: value
});
}

handleSubmit(e) {
e.preventDefault();

let payload = {
title: this.state.title
};

this.postCard(payload);

console.log('Payload: ', payload);
}

postCard(data) {
return axios.post('http://localhost:4000/api/cards', data)
.then(response => {
console.log('Response: ', response.message);
});
}

render() {
return (
<div className="card-form-panel">
<form className="card-form" onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="card-title-field">Title</label>
<input className="form-control"
type="text"
placeholder="Title..."
id="card-title-field"
name="title"
value={this.state.title}
onChange={this.handleChange} />
</div>

<input className="card-submit-btn btn btn-primary" type="submit" value="Save" />
</form>
</div>
);
}
}

export default CarnetSidebarForm;

我不断收到此错误消息,这现在很烦人:

expect(jest.fn()).toHaveBeenCalled()

Expected mock function to have been called.

但是如果我在测试中创建一个假组件那么它就可以工作

it('should submit form data', () => {
let handleSubmit = jest.fn();

// create a fake component
let form = mount(
<form onSubmit={handleSubmit}>
<input className="submit-btn" type="submit" value="Save" />
</form>
);

form.find('.submit-btn').simulate('submit');

expect(form.find('.submit-btn').length).toEqual(1);
expect(handleSubmit).toHaveBeenCalled();
});

这是否与 shallow()enzyme 中的 mount 与导入组件有关?我花了很多天寻找答案,但我迷路了。

最佳答案

添加到@rauliyohmc 的回答。问题是,即使在模拟组件方法之后,它也没有被调用,而是调用了实际的方法。因此,在花了一些时间之后,我找到了解决方案。在模拟其方法后,您需要forceUpdate您的组件。

it('should submit form data', () => {
let form = mount(<Form />); // need to use mount to make it work.
form.instance().handleSubmit = jest.fn();
form.update(); // equivalent to calling form.instance().forceUpdate();

form.find('.submit-btn').simulate('submit'); // simulated event must be submit

expect(form.find('.submit-btn').length).toEqual(1);
expect(form.instance().handleSubmit).toHaveBeenCalled();
});

最小示例:gist

关于javascript - Jest.fn() 在 React 单元测试中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43825926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com