enzyme: simulate behavior is not as expected
Please suggest how to fix this scenario to simulate currentTarget object :
let TestComponent = React.createClass({
updateField(event){
console.log(event.target.value);
console.log(event.currentTarget.value);
this.setState({
value: event.currentTarget.value
});
},
render(){
return <input value={this.props.value} onChange={this.updateField} />
}
});
target.value is available in the event object as expected
let component = mount(<TestComponent />);
let input = component.find("input");
input.simulate("change", {target:{value:"hello"});
currentTarget.value is NOT available in the event object as expected, currentTarget is a real dom object. Looks like jsdom is overwriting it?
let component = mount(<TestComponent />);
let input = component.find("input");
input.simulate("change", {currentTarget:{value:"world"});
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 23
- Comments: 18 (6 by maintainers)
I solved this by doing:
Two years later and still no update?
I too am finding this issue. I was using
event.target.value
in my<input />
change handler before, but after getting some Typescript type errors for using it and reading this post about howevent.currentTarget
instead ofevent.target
is the actual correct value to use, I found my enzyme tests broke.Now, when I try setting
event.currentTarget
in my simulated change event, itcurrentTarget
doesn’t get set. Like so:Don’t think this is how this is supposed to behave. It does seem to work if I use
shallow()
though.It’s been over a year, any updates on this? I’m having the same problem.
In my test:
My current solution is to use:
Through my limited research, this seems to be an issue related to
mount
andjsdom
.(Sorry for reviving an old thread, but this is what came up in a Google search, and I see this issue is still open.)
Edit: Relevant conversation here as well: https://github.com/airbnb/enzyme/issues/76
A PR with a failing test case would be helpful.
In general though, I suggest never using
simulate
, as it does not faithfully simulate anything. If you want to invoke a prop callback likeonClick
, invoke it directly with.prop('onClick')()
.This seemed to work for me:
I think this should be natively supported though.
@ljharb isn’t getting into props an implementation detail though? I don’t particularly want to know a lot about the component/React API, I just want to know something fired correctly when I update a value in an input element.
The underlying culprit has been closed! https://github.com/facebook/react/issues/4950. Methinks the Enzyme folks should weigh-in.
currentTarget
is a no-brainer MUST HAVE forsimulate()
to be useful.You can properly simulate this in
shallow
renders because it does not have a DOM. Butmount
ed renders actually use the dom, which then the event system kicks out the real event with real nodes.I’m not sure if there would be a way to over-ride the values in enzyme or not. If you could take a look into it and submit a working PR that would be great.
Otherwise, I am seeing for myself that if I do this test, the
currentTarget.value === input.value
e.g.,