2017-09-06 126 views
1

我试图按输入框。但是,手动触发事件侦听器时,尝试使用酶时,不会触发事件侦听器。我在这里做错了什么?酶wrapper.find(..)。模拟按键不触发事件监听器

事件监听器

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); 
     }) 

回答

0

我被这个问题所困扰了。但是现在我找到了一个解决方案。 除了{ which: 13 }参数,你需要指定至少key参数,让您的模拟式将是这样的:

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