网站搜索

reactjs - 我有一个包含 JSX.Element 的 props。如何在 jest 测试中模拟相同的内容


在我的 React 组件中,我有如下 props

interface Props {
  errorMessage: JSX.Element;
  addCommentCancel: () => void;
  triggerAddComment: () => void;
  showAddCommentForm: boolean;
}

在测试中,我像下面这样调用组件

const props =  {
  errorMessage: {},
  addCommentCancel: () => jest.fn(),
  triggerAddComment: () => jest.fn(),
  showAddCommentForm: true
};
let wrapper = mount(<IncidentCommentList {...props} />);

我收到一个错误

Type '{}' is not assignable to type 'Element'.

我如何模拟 props 元素?

答案#1

const props =  {
  errorMessage: <div>errorMessage</div>,
  addCommentCancel: () => jest.fn(),
  triggerAddComment: () => jest.fn(),
  showAddCommentForm: true
};
let wrapper = mount(<IncidentCommentList {...props} />);

上述代码解决了这个问题。