【发布时间】:2017-11-09 04:48:50
【问题描述】:
我正在编写一个测试来检查如果登录表单提交时没有数据,是否会显示错误通知组件。
describe('User signin', () => {
it('should fail if no credentials are provided', () => {
const loginComponent = shallow(<Login />);
expect(loginComponent.find('.form-login').length).toBe(1);
loginComponent.find('.form-login').simulate('submit');
expect(loginComponent.find(Notification).length).toBe(1);
});
});
看起来.simulate 正在工作,因为测试达到了我的handleSubmit 函数,但是我在测试中遇到了这个错误:
TypeError: 无法读取未定义的属性“preventDefault”
handleSubmit 函数
handleSubmit(e) {
e.preventDefault(); // <-- problem here
const user = this.state.username;
const pass = this.state.password;
const gotoDashboard = () => this.props.history.push('/dashboard');
const postLogin = (user, pass) => {
api.userLogin(user, pass)
如果我删除 e.preventDefault(); 行,我的测试将通过,但是我需要该行以防止默认的表单提交事件。
【问题讨论】:
标签: javascript reactjs testing jestjs enzyme