1
votes

Im my react component, I have props like below

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

and in the test I'm invoking the component like below

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

I get an error

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

How do I mock props element?

1
As its name says JSX.Element either stands for JSX element(e.g. <span />) or is a string. Maybe null also works but I'm not sure. Did not find JSX.Element declaration in React or DefinitelyTypes so not filling "official answer"skyboyer

1 Answers

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

The above code fixed the issue.