首页 > 解决方案 > 我有一个 JSX.Element 的道具。我如何在玩笑测试中模拟相同的东西

问题描述

我是我的反应组件,我有像下面这样的道具

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'.

如何模拟道具元素?

标签: reactjstypescriptjestjs

解决方案


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

上面的代码解决了这个问题。


推荐阅读