我确实有像这样的组件
<Hotkeys id="hot-keys-id" keyMap={KEY_MAP} handlers={this.handlers}>
<MyComponent/>
<Hotkeys/>我正在尝试模拟事件onKeyDown它不起作用
const someRandom = jest.fn();
const component = mount(<MyComponent onKeyDown={someRandom} />);
component.find('#hot-keys-id').at(0).simulate('focus');
component.find('#hot-keys-id').at(0).simulate('keyDown', { keyCode: 81 });
expect(someRandom).toBeCalled();发布于 2020-05-06 19:29:28
您应该看看该库的测试,因为它们定义了许多用于验证热键行为的技术。我发现他们中的大多数都使用.simulate('keyPress', {})和.simulate('keyUp', {})的帮助包,所以我怀疑这就是他们在听的。
https://github.com/greena13/react-hotkeys/tree/master/test
和支持库
https://stackoverflow.com/questions/61633196
复制相似问题