import MockAdapter from 'axios-mock-adapter'; | |
import { api } from './auth'; // 添加api导入 | |
import { createPost, getPosts, getPostDetail, likePost, addPostComment } from './helpPost'; | |
describe('求助帖API', () => { | |
let mockAxios; | |
beforeEach(() => { | |
mockAxios = new MockAdapter(api); | |
}); | |
afterEach(() => { | |
mockAxios.restore(); | |
}); | |
describe('createPost - 创建求助帖', () => { | |
it('应该正确发送帖子数据', async () => { | |
const postData = { | |
title: '测试标题', | |
content: '测试内容', | |
authorId: 'user123' | |
}; | |
mockAxios.onPost('/help/posts', postData).reply(201, { code: 201 }); | |
const response = await createPost(postData.title, postData.content, postData.authorId); | |
expect(response.status).toBe(201); | |
}); | |
}); | |
describe('getPosts - 获取求助帖列表', () => { | |
it('应该支持分页参数', async () => { | |
const page = 2, size = 10; | |
mockAxios.onGet('/help/posts', { params: { page, size } }).reply(200, { | |
code: 200, | |
data: [] | |
}); | |
const response = await getPosts(page, size); | |
expect(response.status).toBe(200); | |
}); | |
}); | |
describe('likePost - 点赞求助帖', () => { | |
it('应该正确发送点赞请求', async () => { | |
mockAxios.onPost('/help/posts/post123/like').reply(200, { code: 200 }); | |
const response = await likePost('post123'); | |
expect(response.status).toBe(200); | |
}); | |
}); | |
describe('addPostComment - 添加帖子评论', () => { | |
it('应该正确发送评论数据', async () => { | |
const comment = { content: '测试评论', author: 'user1' }; | |
mockAxios.onPost('/help/posts/post456/comments', comment).reply(200, { code: 200 }); | |
const response = await addPostComment('post456', comment); | |
expect(response.status).toBe(200); | |
}); | |
}); | |
}); |