Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
65
rated 0 times [  71] [ 6]  / answers: 1 / hits: 15715  / 7 Years ago, wed, june 7, 2017, 12:00:00

I have the following form:



<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>


My current test



The first test passes, it's the 2nd test I'm unable to write at the moment.



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' };

});
});


I want to test a form submission, however I first need to enter in text into the #input-auth-username and the #input-auth-password input fields.



How do you do that with Enzyme?


More From » reactjs

 Answers
14

Still waiting on a better answer, but thanks to this link https://github.com/airbnb/enzyme/issues/76



Was able to accomplish text being entered into my form's inputs using the following code



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');
});

[#57538] Sunday, June 4, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;