如题,举个例子:
// ListItem.tsx
interface Props {
onTouchStart: (e: React.TouchEvent) => void;
}
const ListItem: React.FC<Props> = (props) => {
const handleTouchStart = (e: React.TouchEvent) => {
// 在组件内部函数调用传入的函数
props.onTouchStart(e);
// 执行一些其他操作
}
return <div onTouchStart={handleTouchStart}></div>;
}
export default ListItem;
// ListItem.test.tsx
import { createElement } from 'rax';
import renderer from 'rax-test-renderer';
it('Test ListItem TouchStart', () => {
const mockFunc = jest.fn();
const component = renderer.create(<ListItem onTouchStart={mockFunc} />)
const tree = component.toJSON();
tree.eventListeners.touchstart();
// 报错
expect(tree.eventListeners.touchstart).toHaveBeenCalled();
})
expect(received).toHaveBeenCalled()
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function handleTouchStart]
怎么在测试用例中测试传入props
的函数是否存在?
怎么在测试用例中测试传入props
的函数是否被调用?
1
ayase252 2020-05-23 20:37:46 +08:00 via iPhone
expect(mockFunc).toHaveBeenCalled()
你应该测的是你的 spy |