mocha + enzyme, 或者React Testing Library + Jest 的ajax测试
// app.test.js
import { rest } from 'msw';
import { setupServer } from 'msw/node';
// 模拟请求,和返回数据
const server = setupServer(
rest.get('/api/os/platforms', (req, res, ctx) => {
return res(
ctx.json({
code: '200',
data: [],
message: 'success'
})
);
}),
rest.post('/api/os/create', (req, res, ctx) => {
return res(
ctx.json({
code: '200',
data: '',
message: 'success'
})
);
})
);
// 生命周期监听
describe('test axios', () => {
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
it('1. render Component', () => {
const wrapper = shallow(<Component{...props} />);
});
});
// Component.tsx
class Component extends React.PureComponent{
async componentDidMount() {
let result = await api.getData(...);
}
}