我是React Redux的新手。我正在尝试测试异步操作
动作类似于>
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();
});
});
所以在这里我得到一个错误,即
超时-异步回调未在由指定的超时内调用 jasmine.DEFAULT_TIMEOUT_INTERVAL。
因此,我没有收到此错误,有人可以帮助我吗? 我编写测试用例的方式是对的吗?
要请求我也使用令牌,
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
};
});
那么,是因为我没有传递令牌吗?
答案 0 :(得分:1)
您忘记了从动作创建者那里返回一个Promise
:您必须返回get
调用,它应该可以工作:
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('/');
}
}
})
}