【发布时间】:2017-06-08 04:09:16
【问题描述】:
我有以下表格:
<form onSubmit={ this.handleSubmit } className="form-login">
<ul>
<li className="rel">
<div className="icon-user-circle-o"></div>
<input type="text" id="input-auth-username" placeholder="username" />
</li>
<li className="rel">
<div className="icon-lock"></div>
<input type="password" id="input-auth-password" placeholder="password" />
</li>
<li>
<button className="btn-green" type="submit">Login</button>
</li>
</ul>
</form>
我目前的测试
第一个测试通过了,这是我目前无法编写的第二个测试。
describe('User Login', () => {
it('should fail if no credentials are provided', () => {
const fakeEvent = { preventDefault: () => '' };
expect(loginComponent.find('.form-login').length).toBe(1);
loginComponent.find('.form-login').simulate('submit', fakeEvent);
expect(loginComponent.find(Notification).length).toBe(1);
// console.log(loginComponent.debug());
});
it('should log user into dashboard if correct credentials are provided', () => {
const credentials = { username: 'joe', password: 'myspecialpassword' };
});
});
我想测试表单提交,但是我首先需要在#input-auth-username 和#input-auth-password 输入字段中输入文本。
您如何使用 Enzyme 做到这一点?
【问题讨论】:
-
您可能应该将输入转换为受控组件:facebook.github.io/react/docs/forms.html#controlled-components。如果您的组件状态不控制输入字段的值和更改处理程序,则酶不能直接对它们进行更改。唯一的方法是进行 dom 操作。
标签: javascript reactjs testing jestjs enzyme