异步,等待似乎并不适合我。 也许我的实施是错误的。 任何人都建议我如何正确地模拟这个功能' userLogin'调用
组件:
export default Wrapped =>
class extends React.Component {
import { userLogin } from './services/userService';
logIn = () => {
return userLogin('user, 'password')
.then(() => {
this.props.history.push('/');
})
.catch(error => {
//I'll test something happens in here
console.log('error');
});
}
};
服务
import axios from 'axios';
export const userLogin = (email, password) => {
return axios
.post('/login')
.then(response => {
.....
});
};
测试
it('calls ... when rejected', async () => {
const Container = loginContainer(Login);
const wrapper = shallow(<Container history={ history }/>);
userService.userLogin = jest.fn().mockRejectedValue('error');
await wrapper.instance().logIn()
.catch(() => {
expect(something).toHaveBeenCalled();
});
});