我需要关于笑话测试的帮助。我的测试中出现错误TypeError: Cannot read property 'then' of undefined
Action Creators中的signIn函数进行POST调用
import {SIGNING_IN, SIGNED_IN, SIGNING_FAILED, SIGNED_OUT} from "../actionTypes";
import {makePostCall} from "../../api/apiCalls";
export function signIn(data){
return dispatch => {
dispatch({type: SIGNING_IN});
makePostCall('http://localhost:8000/api/v1/signin', data)
.then(response => {
const auth = {token: response.token, userId: response.id};
localStorage.setItem('auth', JSON.stringify(auth));
dispatch({type: SIGNED_IN, message: 'You signed in successfully.'});
})
.catch(error => {
console.log('error: ', error);
dispatch({type: SIGNING_FAILED, message: 'Email or Password incorrect. Please try again!'});
});
}
}
这是在上述调用中被调用的POST调用函数
export function makePostCall(url, data){
return axios({method: 'post', url, data})
.then(response => response.data)
}
登录方法测试
jest.mock('../../../src/api/apiCalls');
describe('authenticationActionCreators', () => {
describe('signIn', () => {
let dispatch;
beforeEach(() => {
jest.clearAllMocks();
const authResponse = getAuthResponse();
makePostCall.mockReturnValue(Promise.resolve(authResponse));
dispatch = jest.fn();
});
test('should make post call with correct URL and data', () => {
const data = {email: '[email protected]', password: 'password'};
return signIn(data)(dispatch).then(() => {
expect(makePostCall).toHaveBeenCalledWith('http://localhost:8000/api/v1/signin', {
email: '[email protected]',
password: 'password'
})
})
});
});
[每当我运行测试时,我在行return signIn(data)(dispatch).then(() => {
上都会出现错误
我做错了。我更改了它,它的工作原理
test('should make post call with correct URL and data', () => {
const data = {email: '[email protected]', password: 'password'};
signIn(data)(dispatch);
expect(makePostCall).toHaveBeenCalledWith('http://localhost:8000/api/v1/signin', {
email: '[email protected]',
password: 'password'
})
});