How do you simulate an keyDown enter event (or others) in Enzyme?
Instead of using a keyCode, I used a key, in the case of 'Enter', using mount
:
wrapper.find('input').simulate('keypress', {key: 'Enter'})
I'm using 'shallow' mount (Enzyme 3.7.0 with Jest 23.6.0). This work for me:
const input = wrapper.find('input');input.simulate('change', { target: { value: 'abcdefg'} });input.simulate('keydown', { keyCode: 13 });
Simulate solution is deprecated
Enzyme simulate is supposed to be removed in version 4. Main maintainer is suggesting directly invoking prop functions. One solution is to directly test that invoking those props does the right thing; or you can mock out instance methods, test that the prop functions call them and unit test the instance methods.
You could call key down for example
wrapper.find('input').prop('onKeyDown')({ key: 'Enter' })
or
wrapper.find('input').props().onKeyDown({ key: 'Enter' })
Information about deprecation: https://github.com/airbnb/enzyme/issues/2173