首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-testing-library中的fireevent问题

react-testing-library中的fireevent问题
EN

Stack Overflow用户
提问于 2019-04-10 19:58:07
回答 1查看 7.1K关注 0票数 1

我正在为我的react应用程序使用react-testing library。在一个测试用例中,我需要在文本框中填充一些值并进行聚焦。

这是测试脚本-

代码语言:javascript
复制
it('searchbox wrapper',async()=>{
    let wrapper=getSearchBoxWrapperInstance('')
    let inputBox=wrapper.findByTestId('inputText');
    inputBox.value='12345';
    fireEvent(inputBox,'focusOut');

})

当我运行测试用例时,我得到以下错误-

代码语言:javascript
复制
TypeError: element.dispatchEvent is not a function

  79 |     let inputBox=wrapper.findByTestId('inputText');
  80 |     inputBox.value='12345';
> 81 |     fireEvent(inputBox,'focusOut');
     |     ^
  82 |     //fireEvent('Blur',
  83 | 
  84 |     //await (() => wrapper.getByText('')))

  at fireEvent (node_modules/dom-testing-library/dist/events.js:533:18)

如果我能提供更多的信息,请告诉我

EN

回答 1

Stack Overflow用户

发布于 2019-04-10 20:20:32

我假设getSearchBoxWrapperInstance返回render的结果。

如果此方法有效,请尝试:

代码语言:javascript
复制
it('searchbox wrapper',async()=>{
  let wrapper=getSearchBoxWrapperInstance('')
  let inputBox=wrapper.findByTestId('inputText');
  fireEvent.change(inputBox, { target:  { value: '12345' } });
  fireEvent.focusOut(inputBox);
  // In alternative you could try fireEvent.blur
})

也有可能是findByTestId找不到你的元素。尝试将其注销以查看是否是这种情况,或者使用getByTestId,如果找不到该元素,则会返回错误。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55612008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档