How do you simulate an keyDown enter event (or oth

2019-03-23 07:50发布

I'm trying to simulate a keyDown event, specifically for Enter, keyCode: 13. I've tried a number of different ways of doing this, but none of them are working. I've also looked online and it seems like this feature is either buggy or not working in the current version of Enzyme. Does anyone know definitively if this feature works, and if so, what is the proper syntax for simulating an enter, or other types of key events? Thanks!

This is what I have currently, and it's not working:

const input = wrapper.find('input');
input.simulate('keyDown', {keyCode: 13});

My current Enzyme version is 2.4.1

3条回答
Bombasti
2楼-- · 2019-03-23 08:22

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 });
查看更多
姐就是有狂的资本
3楼-- · 2019-03-23 08:38

Instead of using a keyCode, I used a key, in the case of 'Enter', using mount:

wrapper.find('input').simulate('keypress', {key: 'Enter'})
查看更多
看我几分像从前
4楼-- · 2019-03-23 08:46
wrapper.find('input').simulate('keydown');

It works for me...

查看更多
登录 后发表回答