如何通过玩笑来测试使用捕鼠器创建的事件

问题描述 投票:4回答:1

我正在编写一个使用捕鼠器的react应用程序。我想知道如何才能对捕鼠器的事件进行测试。我想查看单击左箭头时是否正在调用组件函数。我无法在测试中模拟此事件。似乎反应的综合事件之外的事件并未对组件进行任何更改。我认为Mousetrap会将事件附加到窗口。我需要知道如何测试此功能。

it("checks on leftclick", () => {

    var spy = jest.spyOn(MyComponent.prototype, "onLeftClick");
    var wrapper = mount(<MyComponent />);
    var event = simulant( 'keyDown', { key: 37, which: 37 });
    simulant.fire(window, event); // It did not work
    ReactTestUtils.simulate.keyDown(window, { key: "ArrowLeft", keyCode: 37, which: 37 }) //Did not work either
    expect(spy).toHaveBeenCalled();
    spy.mockReset();
    spy.mockRestore();

  });
reactjs jest mousetrap
1个回答
0
投票
it("checks on leftclick", () => { var spy = jest.spyOn(MyComponent.prototype, "onLeftClick"); var wrapper = mount(<MyComponent />); simulant.fire(document.documentElement, "keydown", { keyCode: 37 }); expect(spy).toHaveBeenCalled(); spy.mockReset(); spy.mockRestore(); });
© www.soinside.com 2019 - 2024. All rights reserved.