react-bootstrap-typeahead - TypeError:e.persist 不是 Typeahead Bootstrap React 中的函数
问题描述
尝试尝试不同的方法在 Enzyme 中测试我的应用程序时出现错误TypeError: e.persist is not a function
,但似乎没有任何效果。
input.prop('onChange')({target: {value: "valuehere", name: "namehere", placeholder: "placeholderhere"}});
input.simulate('change', { target: {value: "valuehere", name: "namehere", placeholder: "placeholderhere"}});
input.props().onChange( {target: {value: "valuehere", name: "namehere", placeholder: "placeholderhere"}});
我的代码是:
import {Typeahead} from 'react-bootstrap-typeahead';
import 'react-bootstrap-typeahead/css/Typeahead.css';
export interface InputFormProps {
label: string;
id: string;
icon: string;
input: any;
value: string[];
}
export default class InputForm extends React.Component<InputFormProps> {
state = {
value: this.props.value, list: []
}
async componentDidMount() {
// API Calls happen here, not relevant
}
change = (value, label) => {
//Converts to a form that the upper function understands from events
let result = { target: {value, placeholder: label}};
this.props.input(result);
}
render () {
//Doesn't render list unless it has loaded
return(
<Typeahead
multiple
selected={this.props.value}
name={this.props.label}
type="text"
className="form-control"
placeholder={this.props.label}
id={this.props.id}
onChange={e => this.change(e, this.props.label)}
options={this.state.list}/>
)
}
}
请注意,某些值已更改以保护我公司工作的隐私。
解决方案
该库似乎在事件对象中使用了其他功能/属性。如果您的测试不依赖于它在浏览器中的工作方式,您可以使用以下内容删除使用的内容:
input.prop('onChange')({
persist: jest.fn(),
target: { value: "valuehere", name: "namehere", placeholder: "placeholderhere" }
});
推荐阅读
- flutter - 如果 Flutter 中的有状态小部件可以实现同样的功能,为什么我们还需要无状态小部件?
- c# - 字典错误调试中不存在给定的键
- tensorflow - TensorRT 中的 Depthwise_conv2d
- php - 将 MySQLi 结果填充到关联数组中
- android - 创建可能不是从 Play 商店安装的应用程序
- c++11 - std::thread 对象如何复制参数?
- python-3.x - 通过使用列名和按行名分组来子集一个数据框
- javascript - 用于在 JavaScript 和 HTML 中下载 SVG 的按钮?
- ios - 每当将新的孩子添加到我的 firebase 数据库中的节点时,我想向用户发送通知
- css - col-md 覆盖 col-sm 以获得较低的分辨率