How to write a test for checking whether value for input field is been set in Jest for a React application?

Viewed 1252

I have created a login form and am using Jest to test it. I am having trouble using simulate with change to check whether the value has been set or not. Code has been provided for reference.

Login.js (The input field for username)

<Form onSubmit={handleSubmit}>
                    <Form.Group className="form-elements" controlId="nameForm">
                        <Form.Label className="form-label">Username:</Form.Label>
                        <Form.Control className="form-control" type="text" name="username" value={name} required placeholder="Enter name" onChange={(e)=>setName(e.target.value)}/>
                    </Form.Group>

Login.test.js - Test case for checking username

describe('Test case for testing login',() =>{
  let wrapper;
  test('username check',()=>
  {
    wrapper = shallow(<Login/>);
    const ele = wrapper.find("FormControl[type='text']")
    ele.simulate("change", {target: { name:'username',value: 'UserFirst'}});
    console.log(...ele);
    expect(ele).toHaveProperty('value', 'UserFirst')
    
  })
})

The result for console.log(...ele) is:

It shows the value is not set

What could be the solution for this?

0 Answers
Related