61

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

Braiam
  • 1
  • 11
  • 47
  • 78
reectrix
  • 7,999
  • 20
  • 53
  • 81
  • That should work. Are you using `shallow` or `mount`? – ZekeDroid Aug 15 '16 at 18:33
  • @ZekeDroid is there any sure way to verify that it's working? My test of whether it's working or not involves checking if a new component is created, and in this case, that's not happening. Also, I've had issues with both `sinon` and `chai`'s spy utilities in enzyme, so I haven't been able to use those to verify user events. – reectrix Aug 15 '16 at 18:58
  • I would add an `onChange` event to your input field and console.log every event. Then run your test and see if you get the `keyDown` event – ZekeDroid Aug 15 '16 at 19:18
  • Where are these simulate-able events documented? The airbnb docs only mention 'click', not e.g., keypress or keyDown[sic]. Are they borrowed from the WebAPI somewhere? – pgblu Nov 08 '17 at 16:10
  • This works with enzyme 3.1.0 using mount. Event name is `keydown` – Frozen Crayon Feb 08 '18 at 17:02

8 Answers8

84

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

wrapper.find('input').simulate('keypress', {key: 'Enter'})
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
alexfigtree
  • 1,516
  • 14
  • 16
26

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 });
Brian Ho
  • 391
  • 4
  • 7
  • where do these keycode constants come from? Couldn't find with a quick google search – Andy McCall Apr 20 '20 at 16:34
  • It is actually the index number in the ASCII table. Based on the ASCII table (http://www.asciitable.com/), number 13 represents Carriage Return (means ENTER key). Someone raised the same question at: https://stackoverflow.com/questions/6086686/keycode-13-is-for-which-key – Brian Ho Apr 20 '20 at 23:04
18

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

Black
  • 9,541
  • 3
  • 54
  • 54
10
wrapper.find('input').simulate('keydown');

It worked for me...

Thomas John
  • 2,138
  • 2
  • 22
  • 38
1
const wrapper = mount(<App />);
const input = wrapper.find('input');
input.props().onKeyDown({key: 'Enter'});
  • Enzyme 3.9.0
  • React 16.8.6
  • 1
    Code-only answers are generally frowned upon on this forum. Please edit your answer to include a description of what your code does and how it will solve OP's problems. – mypetlion May 23 '19 at 21:40
1

It actually depends on the implementation. If you've used something like this in your implementation:

if (event.charCode === 13) {
  // do something
}

you would simulate the event in your test like this:

wrapper.find('input').simulate('keypress', { charCode: 13 });

Hope it helps :-).

Aleksandar Grbic
  • 203
  • 1
  • 3
  • 13
1

This solution is working perfectly:

wrapper.find('#id1').simulate('keyDown', {key: 'ArrowRight'});
wrapper.find('#id2').simulate('keyDown', {key: 'Enter'})
Abhijith Sasikumar
  • 13,262
  • 4
  • 31
  • 45
0

If you try to simulate a Event while shallowing an Element you could mock the document.addEventListener method:

let events = [];
document.addEventListener = jest.fn((event, cb) => {
    events[event] = cb;
});

shallow(<YourElement/>);

// trigger the keypress event
events.keyup({key: 's'});

// your test expectations
expect(someMethod).toBeCalledTimes(1);
Penny Liu
  • 15,447
  • 5
  • 79
  • 98