反应单元测试on Change |类组件
我想为这些功能编写单元测试。
enable () {
this.setState({
show: true
});
this.showValue();
}
showValue () {
const { model } = this.props;
model(A).onChange({ target: { value: '' } });
}
isChecked () {
if ((this.state.show)) {
if (!this.state.show) {
this.enable();
}
return true;
} else {
return false;
}
}
我的测试看起来像这样
it('isChecked', () => {
const wrapper = shallow(<Component/>);
const instance = wrapper.instance();
const mockSetStateFn = jest.fn();
instance.setState = mockSetStateFn;
instance.setState({ show: false });
instance.isChecked();
instance.enable();
expect(instance.isChecked).toHaveBeenCalledWith();
const showValue= spyOn(wrapper.instance(), 'onChange');
wrapper.find(model(A)).simulate('change', { target: { value: '' } });
expect(instance.isChecked).equals('');
});
,我会发现Onchange不是函数的错误。我是单位测试的新手。如何为这三个功能编写单元测试。
I want to write unit test for these functions.
enable () {
this.setState({
show: true
});
this.showValue();
}
showValue () {
const { model } = this.props;
model(A).onChange({ target: { value: '' } });
}
isChecked () {
if ((this.state.show)) {
if (!this.state.show) {
this.enable();
}
return true;
} else {
return false;
}
}
My test looks like this
it('isChecked', () => {
const wrapper = shallow(<Component/>);
const instance = wrapper.instance();
const mockSetStateFn = jest.fn();
instance.setState = mockSetStateFn;
instance.setState({ show: false });
instance.isChecked();
instance.enable();
expect(instance.isChecked).toHaveBeenCalledWith();
const showValue= spyOn(wrapper.instance(), 'onChange');
wrapper.find(model(A)).simulate('change', { target: { value: '' } });
expect(instance.isChecked).equals('');
});
I am getting error that onchange is not a function. I am new to unit tests. How do i write unit test for these three functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论