我正在为我的react应用程序使用react-testing library。在一个测试用例中,我需要在文本框中填充一些值并进行聚焦。
这是测试脚本-
it('searchbox wrapper',async()=>{
let wrapper=getSearchBoxWrapperInstance('')
let inputBox=wrapper.findByTestId('inputText');
inputBox.value='12345';
fireEvent(inputBox,'focusOut');
})当我运行测试用例时,我得到以下错误-
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)如果我能提供更多的信息,请告诉我
发布于 2019-04-10 20:20:32
我假设getSearchBoxWrapperInstance返回render的结果。
如果此方法有效,请尝试:
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,如果找不到该元素,则会返回错误。
https://stackoverflow.com/questions/55612008
复制相似问题