【发布时间】:2019-04-05 03:19:28
【问题描述】:
我有这个文件,我想测试它,但有些行没有被覆盖,我如何在 reactjs 中使用 Jest 和 Enzyme 测试它们。
const List = props => (
<div className={styles['comments-list']}>
<h5 className={styles['comment-header']}>Comments</h5>
<ul className="collection">
{props.comments.map(comment =>
<li className="collection-item avatar" key={comment.id}>
<img src={IMG} alt="" className="circle"/>
<span className="title"><b>{comment.author}</b></span><br/>
<span className={`title ${styles['date-font-size']}`}><i>{formatDate(comment.created_at)}</i></span>
<p className={styles['comment-body']}>
<br/>
{comment.body}
</p>
<div className="secondary-content">
<i className={`material-icons ${styles['icon-red']}`} onClick={event => props.deleteComment(comment.id)}>delete</i>
<i className="material-icons" onClick={(e) => {
$('#foo').modal('open')
props.editComment(comment)
}}>edit</i>
<i className="material-icons">reply</i>
</div>
</li>)}
</ul>
</div>
);
从上面的 sn-p 开始,以下几行没有经过测试,但其余的是:
<li className="collection-item avatar" key={comment.id}>
<i className={`material-icons ${styles['icon-red']}`} onClick={event => props.deleteComment(comment.id)}>delete</i>
$('#foo').modal('open')
props.editComment(comment)
现在上面的这些代码行是我的项目中没有测试的行,我如何在 Jest 或 Enzyme 中测试它们。
以下是其中一项测试:
it('should test List component', () => {
wrapper = shallow(
<List deleteComment='Success!' editComment={jest.fn} comments={[]} handleChange={jest.fn} body={''} />
);
const tree = renderer.create(<List comments={[]} handleChange={jest.fn} body={''} />).toJSON();
expect(tree).toMatchSnapshot();
expect(wrapper.find('li')).toHaveLength(0)
expect(wrapper.props().deleteComment).toEqual(undefined)
expect(wrapper.props().editComment).toEqual(undefined)
});
【问题讨论】: