我创建了一个 API 服务工厂,动态传递 URL,函数作为参数。一旦成功,数据就会进入回调函数,并且可以按预期正常工作。同样,我将编写 JEST 测试用例。我无法找到正确的方法来做到这一点。可以帮助某人。非常感谢。
代码在这里
function userLogin(username, password) {
const reqBody = {
companyEmailAddress: username,
password,
};
const url = `${config.apiBaseUrl}${serviceMethodConstants.login}`;
return (dispatch) => {
dispatch(serviceFactory.postData(url, false, reqBody, function (response, dispatch) {
if (response !== undefined) {
console.log(response )
}
}));
};
}
同样,我编写了 JEST 测试用例,但它没有按预期显示任何错误或成功消息。
JEST 测试代码
import { userConstants } from './constants';
import { serviceFactory } from '../../services/_helpers/serviceFactory';
const loginData = {
companyEmailAddress: 'rameshffdfdf.lambanihghgh@gmail.com',
password: 'Ramesh@1',
};
axiosMock.onPost(routeUrl).reply(200, JSON.stringify(loginData));
const spy = jest.spyOn(axios, 'post');
await store.dispatch(userActions.userLogin(...loginData, function (response, dispatch) {
expect(response.message).toEqual('Failure');
expect(spy).toBeCalled();
}));