enzyme: Unable to change the value of form inputs
First… let me mention that I love using enzyme. Great work. I am currently putting togther training materials for React that will use enzyme for testing.
Problem
I am having a problem testing a form. Specifically changing input values on the form.
wrapper.ref('first').simulate('change', {target: {value: 'David'}});
is not working
wrapper.find("input#first").simulate('change', {target: {value: 'David'}});
is not working
I have created a reduced test case that demonstrates the problem. The output for this example looks like:
Output
Testing a form
first name: Mike
last name: Tyson
✓ can fill out the form
No matter what the default Mike Tyson is always the name, despite the fact that I am chanign it. Why does this happen? Is there another way to change the values for the form?
Test
import { Component } from 'react'
import { expect } from 'chai'
import { mount } from 'enzyme'
class TheForm extends Component {
submit() {
console.log('first name:', this.refs.first.value);
console.log('last name:', this.refs.last.value);
}
render() {
return (
<form action="javascript:void(0)"
onSubmit={this.submit.bind(this)}>
<div>
<input ref="first"
type="text"
placeholder="your first name..."
required="required"
defaultValue="Mike"/>
</div>
<div>
<input ref="last"
type="text"
placeholder="your last name..."
required="required"
defaultValue="Tyson" />
</div>
<button>ADD</button>
</form>
);
}
}
describe("Testing a form", () => {
it("can fill out the form", () => {
const wrapper = mount(<TheForm />);
wrapper.ref('first').simulate('change', {target: {value: 'David'}});
wrapper.ref('last').simulate('change', {target: {value: 'Blane'}});
wrapper.find('form').simulate('submit');
});
});
also… wrapper.find(‘form’).simulate(‘submit’) works, but clicking the button does not. Even though clicking the button submits the form in the browser.
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 24
- Comments: 32 (2 by maintainers)
@MoonTahoe also, following the example of
ReactTestUtils.Simulate.change
, this should work:Using mocha 3, react 16, and enzyme 3, this worked for me:
Thanks @bjudson . Start using
wrapper.find('input').instance().value = "foo"
instead ofwrapper.find('input').node.value = "foo"
,because according to documentation,
node
method is private in enzyme3 http://airbnb.io/enzyme/docs/guides/migration-from-2-to-3.html#private-properties-and-methods-have-been-removedThe following worked for me also.
However I am disappointed the following did not work.
just to help people who are struggling with changing input value (and do some further checks, like validation). here is something that works for me after I read through this post
my rendered dom tree is like
and yes, i’m using redux-form
@MoonTahoe I’m curious, does this work?
This worked with me:
seems better than https://github.com/airbnb/enzyme/issues/76#issuecomment-189606849 and also works as mentioned above -
wrapper.find('input').instance().value = "foo"
I’m able to reproduce your issue with the current release of enzyme, I’ll look into how this is expected to work. For what it’s worth, you can use controlled components to handle the input state, which works as you expected:
Controlled components are typically recommended as well: https://facebook.github.io/react/docs/forms.html
It’s not meant to cause a change; it’s mean to call the
onChange
function.I recommend avoiding
simulate
, and manually invoking the prop function you want instead.I also am getting errors related to this subject (simulate(‘change’ …) not causing a change)
This works fine for me:
@Moezalez so you’re using jsdom? What version of React and what version of enzyme? Can you share a small reproducable case?
I also ran into issues getting and setting values on
<input />
tags.input.node.value
works great, but I’m curious why this doesn’t work:http://stackoverflow.com/questions/37219772/enzyme-how-to-access-and-set-input-value