【问题标题】:Jest and enzyme tests snippet笑话和酶测试片段
【发布时间】: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)


    });

【问题讨论】:

    标签: reactjs jestjs enzyme


    【解决方案1】:

    在您的测试中,您提供了空的 cmets 列表 comments={[]},因此 props.comments.map 中的代码永远不会执行。再添加一项测试,检查您的组件是否呈现 cmets:

    it('should test List component with comments', () => {
    
        const comments = [
            {
                id: 1,
                author: 'John',
                body: 'Hello world'
            }
        ];
    
        const wrapper = shallow(
            <List deleteComment='Success!'  editComment={jest.fn} comments={comments} handleChange={jest.fn} body={''} />
        );
    });
    

    【讨论】:

      猜你喜欢
      • 2018-06-21
      • 1970-01-01
      • 2020-06-14
      • 2020-06-19
      • 2017-08-18
      • 2023-03-27
      • 1970-01-01
      • 2021-09-07
      • 2019-03-25
      相关资源
      最近更新 更多