Skip to content Skip to sidebar Skip to footer

Enzyme Wrapper.find(..).simulate Keypress Doesnt Trigger Event Listener

I am trying to press enter on one of the input boxes. Doing it manually triggers the event listener, however, while trying with enzyme, the event listener is not triggered. What am

Solution 1:

I was struggling with this problem too. But now I`ve found a solution. In addition to { which: 13 } parameter, you need to specify at least key parameter, so your simulate expression will be like:

searchInput.simulate('keyPress', {
    key: 'Enter',
    keyCode: 13,
    which: 13,
});

Solution 2:

Enzyme, built for React testing, likely doesn't know about your native JavaScript event listener. If you bind the event with JSX, Enzyme should be able to pick it up. https://reactjs.org/docs/handling-events.html

Change this:

this.input.addEventListener('keypress', function(event){
    debugger;
    onEnter(event);
});

To this:

<searchBar>
    <input onKeypress={this.onEnter.bind(this)} aria-label="Search" />
</searchBar>

Post a Comment for "Enzyme Wrapper.find(..).simulate Keypress Doesnt Trigger Event Listener"