reactjs - 在 Contenteditable 元素上触发事件 keyDown(测试 + react + Jest + enzyme )

标签 reactjs testing contenteditable jestjs enzyme

我正在使用enzyme + jest来测试react App,是否可以在可编辑的span元素上触发onKeyDown事件?

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

我尝试过:

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

但是没有成功。元素内容保持不变。

最佳答案

这不是 enzyme 的工作原理。它只能模拟在React Explicit中添加的事件。来自文档

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.

关于reactjs - 在 Contenteditable 元素上触发事件 keyDown(测试 + react + Jest + enzyme ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43763398/

相关文章:

javascript - 从 Protractor 执行一个方法并获取返回值

javascript - 在浏览器中使用JS Pact时,如何创建pact文件?

javascript - React Contenteditable - 如何将 onClick 分配给react-contenteditable 中的 <span> ?

jquery - 带有粗体选项的 Contenteditable div

javascript - 尝试在 React w/Formik 中每次输入字段完成时创建一个计数器

javascript - react 单选按钮 : checked cycle not aligned

reactjs - enzyme 测试,上下文不渲染元素

javascript - 如何使 contenteditable div 只允许某些标签?

reactjs - 使用 React JS 使用 Material UI 链接链接到另一个组件

javascript - ReactJs/Redux 不变违规 : Could not find "store" in either the context or props of "Connect(LoginContainer)"