Reactjs 使用jest对axios post进行单元测试
我正在使用jest编写一个单元测试。如何测试axios.post的Reactjs 使用jest对axios post进行单元测试,reactjs,jestjs,axios,Reactjs,Jestjs,Axios,我正在使用jest编写一个单元测试。如何测试axios.post的?我查看了所有内容,但几乎每个人都有使用axios.get的示例,但这篇文章中没有 handleLogout(event) { event.preventDefault(); axios .post("/logout") .then(function(response) { window.location = "/login"; }) .catch(
?我查看了所有内容,但几乎每个人都有使用axios.get的示例,但这篇文章中没有
handleLogout(event) {
event.preventDefault();
axios
.post("/logout")
.then(function(response) {
window.location = "/login";
})
.catch(function(error) {
console.log(error);
});
此方法由按钮onclick
事件触发
<button
type="button"
value="Logout"
onClick={this.handleLogout}
>
Logout
</button>
它给了我一个错误:
TypeError:_axios2.default.post不是函数
如果我不模拟防止默认,它会给我这个
TypeError:无法读取未定义的属性'preventDefault'
因此,我假设在处理preventDefault()之后,我需要以某种方式模拟axios。我走对了吗?如何解决这个问题?以下是解决方案:
<代码>索引.tsx
:
import React from 'react';
import axios from 'axios';
export class SomeComponent extends React.Component {
constructor(props) {
super(props);
this.handleLogout = this.handleLogout.bind(this);
}
public render() {
return (
<div>
<button type="button" value="Logout" onClick={this.handleLogout}>
Logout
</button>
</div>
);
}
private handleLogout(event: React.MouseEvent<HTMLButtonElement>) {
event.preventDefault();
axios
.post('/logout')
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
}
import React from 'react';
import { shallow } from 'enzyme';
import axios from 'axios';
import { SomeComponent } from './';
jest.mock('axios', () => {
return {
post: jest.fn()
};
});
describe('SomeComponent', () => {
describe('#handleLogout', () => {
const mockedMouseEvent = {
preventDefault: jest.fn()
};
it('should logout correctly', done => {
const mockedData = 'mocked data';
const wrapper = shallow(<SomeComponent></SomeComponent>);
expect(wrapper.find('button')).toHaveLength(1);
(axios.post as jest.MockedFunction<typeof axios.post>).mockResolvedValueOnce(mockedData);
const consoleLogSpyOn = jest.spyOn(console, 'log');
wrapper.find('button').simulate('click', mockedMouseEvent);
expect(wrapper.find('button').text()).toBe('Logout');
expect(axios.post).toBeCalledWith('/logout');
setImmediate(() => {
expect(consoleLogSpyOn).toBeCalledWith(mockedData);
consoleLogSpyOn.mockRestore();
done();
});
});
it('should throw error when axios post error', done => {
const mockedError = new Error('network error');
const wrapper = shallow(<SomeComponent></SomeComponent>);
expect(wrapper.find('button')).toHaveLength(1);
(axios.post as jest.MockedFunction<typeof axios.post>).mockRejectedValueOnce(mockedError);
const consoleLogSpyOn = jest.spyOn(console, 'log');
wrapper.find('button').simulate('click', mockedMouseEvent);
expect(wrapper.find('button').text()).toBe('Logout');
expect(axios.post).toBeCalledWith('/logout');
setImmediate(() => {
expect(consoleLogSpyOn).toBeCalledWith(mockedError);
consoleLogSpyOn.mockRestore();
done();
});
});
});
});
100%覆盖率的单元测试结果:
PASS src/stackoverflow/54555039/index.spec.tsx
某些成分
#手工登录
✓ 应正确注销(29毫秒)
✓ axios post错误时应引发错误(14ms)
console.log node_modules/jest mock/build/index.js:860
模拟数据
console.log node_modules/jest mock/build/index.js:860
错误:网络错误
在对象上。为什么不能将此请求放入文件中并模拟它进行测试??试试这个例子,可能是我根据@PraveenKumar的建议编辑的问题的重复。这给了我一个preventDefault()错误。请查看我的编辑。尝试wrapper.simulate(“单击”),新建事件(“单击”);
import React from 'react';
import { shallow } from 'enzyme';
import axios from 'axios';
import { SomeComponent } from './';
jest.mock('axios', () => {
return {
post: jest.fn()
};
});
describe('SomeComponent', () => {
describe('#handleLogout', () => {
const mockedMouseEvent = {
preventDefault: jest.fn()
};
it('should logout correctly', done => {
const mockedData = 'mocked data';
const wrapper = shallow(<SomeComponent></SomeComponent>);
expect(wrapper.find('button')).toHaveLength(1);
(axios.post as jest.MockedFunction<typeof axios.post>).mockResolvedValueOnce(mockedData);
const consoleLogSpyOn = jest.spyOn(console, 'log');
wrapper.find('button').simulate('click', mockedMouseEvent);
expect(wrapper.find('button').text()).toBe('Logout');
expect(axios.post).toBeCalledWith('/logout');
setImmediate(() => {
expect(consoleLogSpyOn).toBeCalledWith(mockedData);
consoleLogSpyOn.mockRestore();
done();
});
});
it('should throw error when axios post error', done => {
const mockedError = new Error('network error');
const wrapper = shallow(<SomeComponent></SomeComponent>);
expect(wrapper.find('button')).toHaveLength(1);
(axios.post as jest.MockedFunction<typeof axios.post>).mockRejectedValueOnce(mockedError);
const consoleLogSpyOn = jest.spyOn(console, 'log');
wrapper.find('button').simulate('click', mockedMouseEvent);
expect(wrapper.find('button').text()).toBe('Logout');
expect(axios.post).toBeCalledWith('/logout');
setImmediate(() => {
expect(consoleLogSpyOn).toBeCalledWith(mockedError);
consoleLogSpyOn.mockRestore();
done();
});
});
});
});