-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Unable to change the value of form inputs #364
Comments
FYI this has been reported previously here: #308 |
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: class TheForm extends React.Component {
constructor(props) {
super(props);
this.state = {
firstName: 'Mike',
lastName: 'Tyson'
}
this.onFirstNameChange = this.onFirstNameChange.bind(this);
this.onLastNameChange = this.onLastNameChange.bind(this);
}
submit() {
console.log('first name:', this.state.firstName);
console.log('last name:', this.state.lastName);
}
onFirstNameChange(e) {
this.setState({
firstName: e.target.value
})
}
onLastNameChange(e) {
this.setState({
lastName: e.target.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"
onChange={this.onFirstNameChange}
value={this.state.firstName}/>
</div>
<div>
<input ref="last"
type="text"
placeholder="your last name..."
required="required"
onChange={this.onLastNameChange}
value={this.state.lastName} />
</div>
<button>ADD</button>
</form>
);
}
} Controlled components are typically recommended as well: https://facebook.github.io/react/docs/forms.html |
Yea, thanks. The issue, in my case is that I cannot change the TheForm, I only have access to the test for the form. |
I was able to create a test that passed based on what I found in issue #76. describe("Testing a form", () => {
let wrapper;
before(() => {
wrapper = mount(<TheForm />);
wrapper.ref('first').get(0).value = 'David';
wrapper.ref('last').get(0).value = 'Blane';
wrapper.find('form').simulate('submit');
});
it("can set first name", () => expect(wrapper.ref('first').get(0).value).to.equal("David"));
it("can set last name", () => expect(wrapper.ref('last').get(0).value).to.equal("Blane"));
}); It would be nice if there were a cleaner way to do this as requested in Issue #76 |
@MoonTahoe I'm curious, does this work? wrapper.find('input').first().simulate('change', {target: {value: 'David'}});` |
@MoonTahoe also, following the example of const wrapper = mount(<TheForm/>);
const first = wrapper.find('first');
first.node.value = 'David'
first.simulate('change', first) |
I also ran into issues getting and setting values on
http://stackoverflow.com/questions/37219772/enzyme-how-to-access-and-set-input-value |
Hi, I'm sorry for hijacking this thread, but the examples listed here should go into the documentation. Form handling is badly documented (IMHO). Also, how about a Then you can do T |
using the solution proposed by @aweary, I get |
@Moezalez what testing environment are you using (jsdom, karma, etc.)? |
@aweary nyc + mocha |
@Moezalez so you're using jsdom? What version of React and what version of enzyme? Can you share a small reproducable case? |
This works fine for me: // <div><input/></div>
let a = mount(<TestComponent/>);
a.find('input').node.value = 'Task1'; |
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 |
@bochen2014 thanks so much, your solution is the only one working for me! you really helped me! |
@bochen2014 another +1 for you. Thanks a lot. Your solution alone worked for me. |
The following worked for me also. const username = wrapper.ref('username')
username.node.value='test'
username.simulate('change', username) However I am disappointed the following did not work. wrapper.ref('username').simulate('change', {target: {value: 'test'}}) |
@aweary's solution worked for my testing purposes, yet I still don't know why. 😉 |
Hi, I opened PR #995 adding a test case to (hopefully) demonstrate this issue. |
I also am getting errors related to this subject (simulate('change' ...) not causing a change) test('TaskHeader changes Value after Inputted', () => {
const component = mount(<TaskHeader listName="Demo" totalTasks={55} />)
const taskInput = component.find('.app-tasks-newTask-input')
expect(taskInput.props().value).toEqual('')
// const event = { target: { value: 'First Task' } }
taskInput.props().onChange({currentTarget: {value: 'First Task'}}) // NOTE: this works
expect(taskInput.props().value).toEqual('First Task')
// taskInput.simulate('change',{target: {value: 'Second Task'}}) // NOTE: doesn't work
taskInput.node.value='Second Task'
taskInput.simulate('change', taskInput) // NOTE: works as a replacement
expect(taskInput.props().value).toEqual('Second Task')
}) |
It's not meant to cause a change; it's mean to call the I recommend avoiding |
Good to know, thanks - I probably spent an hour trying to hunt down the solution w/o anyone saying that a particular way is the better way to do it. |
Hmm, using node.value no longer works "Attempted to access ReactWrapper::node, which was previously a private property on Enzyme ReactWrapper instances, but is no longer and should not be relied upon. Consider using the getElement() method instead". Is there an alternative way of setting the value? |
There's a |
Using mocha 3, react 16, and enzyme 3, this worked for me:
|
Thanks @bjudson . because according to documentation, |
seems better than #76 (comment) |
I am trying this with a redux state component, The dispatch is working as I can confirm from the action logging out the output but the reducer is not working as I cant seems to get a logged out output at all |
This worked with me:
|
@engmyahya Your code throws a typescript error for me - |
here is my code..
I have update my DOM with |
You need to cast the elements as
|
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 workingwrapper.find("input#first").simulate('change', {target: {value: 'David'}});
is not workingI have created a reduced test case that demonstrates the problem. The output for this example looks like:
Output
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
also... wrapper.find('form').simulate('submit') works, but clicking the button does not. Even though clicking the button submits the form in the browser.
The text was updated successfully, but these errors were encountered: