React Redux异步动作创建者测试

编程入门 行业动态 更新时间:2024-10-10 10:22:39
本文介绍了React Redux异步动作创建者测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是React Redux的新手.我正在尝试测试异步操作

I am new to the react redux. I am trying to test the async action

动作类似于>

export function fetchUserJd() { return (dispatch) => { let url = FETCH_JD_ROOT_URL + page + "&" + size; dispatch({ type: REQUEST_INITIATED }) get(url) .then((response) => { if (response.status === 200) { dispatch({ type: REQUEST_SUCCESSED, }); dispatch({ type: FETCHING_JOBDESCRIPTION_SUCCESS, data: response.payload, } ) } else { dispatch({ type: REQUEST_SUCCESSED }) toastr.error("Error while fetching Job Description, Please check again"); if (response.status === "") { toastr.error('Our server is down. Please check again'); } dispatch({ type: FETCHING_JOBDESCRIPTION_FAILED, data: response.status, }); if (response.status === 401) { toastr.error('Please Login Again'); localStorage.clear(); history.push('/'); } } }) }

现在,我正在尝试像

describe('Header component actions', () => { beforeEach(() => moxios.install()); afterEach(() => moxios.uninstall()); it('creates Fetch Jopb description when login action is successful', async (done) => { let url = FETCH_JD_ROOT_URL + page + "&" + size; moxios.stubRequest(url, { status: 201, response: mockData.jobDescriptionResponse }); const expectedActions = [{ type: "FETCHING_JOBDESCRIPTION_SUCCESS"}]; const store = mockStore({}); await store.dispatch(fetchUserJd()) .then(() => { expect(store.getActions()).toEqual(expectedActions); }); done(); }); });

所以在这里我得到一个错误,即

so here I am getting one error here that is ,

超时-未在由指定的超时内调用异步回调 jasmine.DEFAULT_TIMEOUT_INTERVAL.

Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

因此,我没有收到此错误,有人可以帮助我吗? 我编写测试用例的方式是对的吗?

So, I am not getting this error, Can any one help me with this ? And the way I am writing the test case is right ?

要请求我也使用令牌,

headers: { "Authorization": localStorage.getItem("access_token") !== null ? `Bearer ` + localStorage.getItem("access_token") : null, "Content-Type": "application/json" }

我的通话服务就像,

export const get = (url) => axios.get( url, { headers: { "Authorization": localStorage.getItem("access_token") !== null ? `Bearer ` + localStorage.getItem("access_token") : null, "Content-Type": "application/json" } } ).then(data => { if (data.status === HttpStatus.OK) { return { status: data.status, payload: data.data }; } }).catch(err => { return { status: err.response.data, payload: null }; });

那么,是因为我没有传递令牌吗?

So, is it because of I am not passing the tokens ?

推荐答案

您忘了从动作创建者返回Promise:您必须返回get调用,它应该可以工作:

You forgot to return a Promise from your action creator : you have to return the get call and it should work :

export function fetchUserJd() { return (dispatch) => { let url = FETCH_JD_ROOT_URL + page + "&" + size; dispatch({ type: REQUEST_INITIATED }) return get(url) // you missed the return statement here .then((response) => { if (response.status === 200) { dispatch({ type: REQUEST_SUCCESSED, }); dispatch({ type: FETCHING_JOBDESCRIPTION_SUCCESS, data: response.payload, } ) } else { dispatch({ type: REQUEST_SUCCESSED }) toastr.error("Error while fetching Job Description, Please check again"); if (response.status === "") { toastr.error('Our server is down. Please check again'); } dispatch({ type: FETCHING_JOBDESCRIPTION_FAILED, data: response.status, }); if (response.status === 401) { toastr.error('Please Login Again'); localStorage.clear(); history.push('/'); } } }) }

更多推荐

React Redux异步动作创建者测试

本文发布于:2023-11-28 16:58:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建者   动作   测试   React   Redux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!