本文介绍了酶wrapper.find(..)。模拟按键不会触发事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在其中一个输入框上按 enter (输入)。手动执行此操作会触发事件侦听器,但是,在尝试使用酶时,不会触发事件侦听器。我在这里做什么错了?

I am trying to press enter on one of the input boxes. Doing it manually triggers the event listener, however, while trying with enzyme, the event listener is not triggered. What am I doing wrong here?



this.input.addEventListener('keypress', function(event){
            debugger;
            onEnter(event);
        });





function setup(store, props) {
    return mount(<Provider store={store}>
            <component{...props}/>
        </Provider>
    );
}

beforeEach(() => {
        wrapper = setup(store, {});
        searchBar = wrapper.find('searchBar');
        searchInput = searchBar.find("input");
    });

it("when enter is pressed, event should be triggered", ()=> {
            let wait = false;
            runs(()=> {
                searchInput.simulate('change', {target: {value: 'helloWorld'}});
                searchInput.simulate('keyPress', {which: 13});
                setTimeout(()=> {
                    wait = true;
                }, 1000);
            })

            waitsFor(()=> {
                return wait;
            }, "", 1500);
        })


推荐答案

我也在努力解决这个问题。但是现在我找到了解决方案。
除了 {其中:13} 参数之外,您还需要至少指定 key 参数,因此您的模拟表达式将如下所示:

I was struggling with this problem too. But now I`ve found a solution.In addition to { which: 13 } parameter, you need to specify at least key parameter, so your simulate expression will be like:

searchInput.simulate('keyPress', {
    key: 'Enter',
    keyCode: 13,
    which: 13,
});

这篇关于酶wrapper.find(..)。模拟按键不会触发事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 03:47