Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simulate('change' ...) on input not updating its value #2426

Open
2 of 13 tasks
Nikhil22 opened this issue Aug 4, 2020 · 5 comments
Open
2 of 13 tasks

simulate('change' ...) on input not updating its value #2426

Nikhil22 opened this issue Aug 4, 2020 · 5 comments
Labels
Projects

Comments

@Nikhil22
Copy link

Nikhil22 commented Aug 4, 2020

Current behavior

As the title says, when I try to simulate a change on my <input/>, the input's value remains the same - it doesn't change.

Here is my HTML. This <input/> is within a <form/>

<input 
   className="w-100 ba b--black-20 br1 pa3 bg--easy-gray mb2" 
   type="text" 
   name="firstName" 
   ref={register({ required: true })}
/>

register is from the useForm hook of react-hook-form.

Here is my mount.

const signup = mount(
   <Provider store={mockStore}>
        <Router history={history}>
               <Signup />
          </Router>
   </Provider>,
)

The <form/> and <input/> are within the <Signup/> component.

Here is my enzyme code

const firstNameInput = signup.findWhere((n) => n.prop('name') === 'firstName')
firstNameInput.simulate('change', {target: {value: 'Nikhil'}})
await act( async () => {
    signup.update();
})
console.log(firstNameInput.props())

Here is the output

 {
    className: 'w-100 ba b--black-20 br1 pa3 bg--easy-gray mb2',
    type: 'text',
    name: 'firstName'
}

I don't see value anywhere.

Expected behavior

 {
    className: 'w-100 ba b--black-20 br1 pa3 bg--easy-gray mb2',
    type: 'text',
    name: 'firstName',
    value: 'Nikhil'
}

Your environment

API

  • shallow
  • mount
  • render

Version

library version
enzyme ^3.11.0
react ^16.13.1
react-dom ^16.13.1"
react-test-renderer
adapter (below)

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-16.3
  • enzyme-adapter-react-16.2
  • enzyme-adapter-react-16.1
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )
@ljharb
Copy link
Member

ljharb commented Aug 4, 2020

simulate shouldn't be used; it doesn't faithfully simulate anything.

If you want to change the input's value, set the value prop on it; if you want to invoke an onChange, invoke it.

If you're doing something unidiomatic for React, and binding events directly to reffed DOM elements, then you'll have to test them by directly interacting with the DOM elements as well.

@ljharb ljharb added the question label Aug 4, 2020
@Nikhil22
Copy link
Author

Nikhil22 commented Aug 5, 2020

Can you please tell me how to do this in enzyme?

setProps won't work because the <input> is a child o the component <Signup> being mounted.

@ljharb
Copy link
Member

ljharb commented Aug 8, 2020

in this case, you're not actually using onChange, you're letting react-hook-form manipulate the DOM directly. I'm not really sure how you'd test that; that sounds like a react antipattern to me tbh :-/

@codeDude64
Copy link

@ljharb But if simulate doesn't simulate the events How can I simulate my onChange function?

I mean in my case I have an input text, it has a event onChange that updates the state and it have the attribute value with the state.

<input type="text" onChange={changeCategory} value={newCategory} />

@janzenz
Copy link

janzenz commented Feb 10, 2022

I'm late to the party. I just learned that simulate is deprecated. I think the confusion for people is because this info isn't so clear in the doc. There is an existing PR for this atm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
simulate
Awaiting triage
Development

No branches or pull requests

4 participants