Javascript 测试点击事件
我有一个测试-它相对简单:Javascript 测试点击事件,javascript,reactjs,jestjs,react-testing-library,Javascript,Reactjs,Jestjs,React Testing Library,我有一个测试-它相对简单: import { fireEvent, render, } from '@testing-library/react'; // ... it('Events should bubble when they aren\'t prevented from doing so', () => { const parentF = jest.fn(); const childF = jest.fn();
import {
fireEvent,
render,
} from '@testing-library/react';
// ...
it('Events should bubble when they aren\'t prevented from doing so', () => {
const parentF = jest.fn();
const childF = jest.fn();
const testDom = render(
<div onClick={parentF}>
<button onClick={childF} id="test-button">Test</button>
</div>
);
testDom.findByText('Test')
.then(element => {
fireEvent.click(element);
expect(parentF).toBeCalled();
expect(childF).toBeCalled();
});
});
// ...
导入{
fireEvent,
提供,
}来自“@testing library/react”;
// ...
它('当事件没有被阻止时,它们应该冒泡',()=>{
const parentF=jest.fn();
const childF=jest.fn();
const testDom=render(
试验
);
testDom.findByText('Test')
.然后(元素=>{
fireEvent.单击(元素);
expect(parentF).toBeCalled();
expect(childF).toBeCalled();
});
});
// ...
这并不能说明两人都没有被叫来。我猜我在做傻事,但有人能告诉我是什么吗?你有一个承诺,但jest不知道。请尝试返回:
return testDom.findByText('Test')
/* rest of the code */
通过同步路径,测试通过了—例如fireEvent.click(testDom.getByText('test'))