在响应正文中测试动作创建者,以测试axios动作创建者

时间:2018-11-01 07:28:05

标签: javascript reactjs jestjs enzyme

我正在尝试测试一个操作,该操作首先请求服务器获取数据,然后在获取响应之后,在响应正文中派遣另一个Action创建者来获取其他数据。

在编写动作测试时,这些测试并未涵盖动作创建者部分。

这是我的动作创建者:

export const createCommentAction = (comment, slug) => dispatch => {

    dispatch(requestLoadingAction(true));

    addToken();

    // noinspection JSUnresolvedFunction
    return axios.post(`${API_URLS.FETCH_ALL_ARTICLES}${slug}/comment/`, {comment})
        .then(resp => resp.data)
        .then(async response => {
            // noinspection JSUnresolvedVariable
            await dispatch(fetchAllArticles());
            dispatch(viewOneArticleActionCreator(slug));
            notify.show('You have created a comment successfully', 'success', 5000);

        }).catch(error => {
            dispatch(requestLoadingAction(false));

            notify.show(`There's a problem with creating a comment .${error}`, 'error', 4000);
        });
};

这是模拟该端点的测试:

it('should test fetch comments per article', () => {
        moxios.stubRequest(API_URLS.FETCH_ALL_ARTICLES+'great-work/comment/', {
            status: 201,
            response: { comments: {
                    id: 16,
                    body: "How can I rebase on 2 different branches",
                    article: "great-work"
                }
            },


    });

        const expecting = {"payload": {"slug": "slug"}, "type": "VIEW_ONE_ARTICLE"};
        const expectedActions = [
            {'isRequestLoading': true, 'type': 'REQUEST_LOADING'},
            {'payload': {}, 'type': 'ADD_MANY_FROM_SERVER'},
            {'isRequestLoading': false, 'type': 'REQUEST_LOADING'}
        ];



        store.dispatch(createCommentAction({"comment": {"body": "Thanks for the nice article. "}}, 'great-work'))
            .then(() => {
                expect(store.getActions()).toEqual(expectedActions);
            });

        expect(store.dispatch(viewOneArticleActionCreator('slug'))).toEqual(expecting);

        jest.mock('react-notify-toast');
    });

因此,代码的某些行显示为已测试/覆盖,但有些行未涵盖,如下所示:

dispatch(viewOneArticleActionCreator(slug));
            notify.show('You have created a comment successfully', 'success', 5000);

上面的行是未覆盖的行。我该如何用玩笑/酵素遮盖它们。

0 个答案:

没有答案