gpt4 book ai didi

javascript - React/Jest/Enzyme 浅渲染测试中如何将文本输入到表单中?

转载 作者:行者123 更新时间:2023-11-29 15:20:24 24 4
gpt4 key购买 nike

我有以下表格:

<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 做到这一点? ?

最佳答案

仍在等待更好的答案,但感谢此链接 https://github.com/airbnb/enzyme/issues/76

能够使用以下代码将文本输入到我的表单输入中

it('input fields should be filled correctly', () => {
const credentials = { username: 'leongaban', password: 'testpass' };
expect(loginComponent.find('#input-auth-username').length).toBe(1);

const usernameInput = loginComponent.find('#input-auth-username');
usernameInput.value = credentials.username;
expect(usernameInput.value).toBe('leongaban');

const passwordInput = loginComponent.find('#input-auth-password');
passwordInput.value = credentials.password;
expect(passwordInput.value).toBe('testpass');
});

关于javascript - React/Jest/Enzyme 浅渲染测试中如何将文本输入到表单中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44420362/

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