Dmytro Zhytomyrsky
Dmytro Zhytomyrsky

Reputation: 1401

Trigger event keyDown on Contenteditable element (testing + react + jest + enzyme)

I am using enzyme + jest to test react App, is it possible to trigger onKeyDown event on contetnteditable span element?

<span id = 'name' contenteditable = 'true'> Editable text </span>

I tried:

result.find('#name').simulate('keyDown', { key: 'm', keyCode: 77, which: 77 });,

but it did not work. Element content stayed unchanged.

Upvotes: 2

Views: 1503

Answers (1)

Andreas K&#246;berle
Andreas K&#246;berle

Reputation: 110892

This is not how enzyme works. It can only simulate events that were added in react explicit. From the docs

Even though the name would imply this simulates an actual event, .simulate() will in fact target the component's prop based on the event you give it. For example, .simulate('click') will actually get the onClick prop and call it.

Upvotes: 2

Related Questions