| import MockAdapter from 'axios-mock-adapter'; | |
| import { api } from './auth'; // 添加api导入 | |
| import { createPost, getPosts, getPostDetail, likePost, addPostComment,deletePost } from './helpPost'; | |
| describe('求助帖API', () => { | |
| let mockAxios; | |
| beforeEach(() => { | |
| mockAxios = new MockAdapter(api); | |
| }); | |
| afterEach(() => { | |
| mockAxios.restore(); | |
| }); | |
| describe('createPost - 创建求助帖', () => { | |
| it('应该正确发送无图片帖子数据', async () => { | |
| const postData = { | |
| title: '测试标题', | |
| content: '测试内容', | |
| authorId: 'user123' | |
| }; | |
| // 使用函数匹配器来验证FormData内容 | |
| mockAxios.onPost('/help/posts').reply(config => { | |
| const data = config.data; | |
| expect(data.get('title')).toBe(postData.title); | |
| expect(data.get('content')).toBe(postData.content); | |
| expect(data.get('authorId')).toBe(postData.authorId); | |
| expect(data.has('image')).toBe(false); | |
| return [201, { code: 201 }]; | |
| }); | |
| const response = await createPost(postData.title, postData.content, postData.authorId); | |
| expect(response.status).toBe(201); | |
| }); | |
| }); | |
| it('应该正确处理带图片的帖子', async () => { | |
| const postData = { | |
| title: '测试标题', | |
| content: '测试内容', | |
| authorId: 'user123', | |
| selectedImage: new File(['content'], 'test.jpg') | |
| }; | |
| mockAxios.onPost('/help/posts').reply(config => { | |
| const data = config.data; | |
| expect(data.get('image')).toBeInstanceOf(File); | |
| return [201, { code: 201 }]; | |
| }); | |
| const response = await createPost( | |
| postData.title, | |
| postData.content, | |
| postData.authorId, | |
| postData.selectedImage | |
| ); | |
| 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 postId = 'post456'; | |
| const commentData = { | |
| authorId: 'user1', | |
| content: '测试评论' | |
| }; | |
| mockAxios.onPost(`/help/posts/${postId}/comments`).reply(config => { | |
| const data = config.data; | |
| expect(data.get('authorId')).toBe(commentData.authorId); | |
| expect(data.get('content')).toBe(commentData.content); | |
| expect(data.has('image')).toBe(false); | |
| return [200, { code: 200 }]; | |
| }); | |
| const response = await addPostComment('post456', commentData); | |
| expect(response.status).toBe(200); | |
| }); | |
| it('应该正确处理带图片的评论', async () => { | |
| const postId = 'post456'; | |
| const commentData = { | |
| authorId: 'user1', | |
| content: '测试评论', | |
| commentImage: new File(['content'], 'comment.jpg') | |
| }; | |
| mockAxios.onPost(`/help/posts/${postId}/comments`).reply(config => { | |
| const data = config.data; | |
| expect(data.get('image')).toBeInstanceOf(File); | |
| return [200, { code: 200 }]; | |
| }); | |
| const response = await addPostComment(postId, commentData); | |
| expect(response.status).toBe(200); | |
| }); | |
| }); | |
| describe('deletePost - 删除帖子', () => { | |
| it('应该正确发送删除请求', async () => { | |
| const postId = 'post789'; | |
| const authorId = 'user1'; | |
| mockAxios.onDelete(`/help/posts/${postId}`).reply(config => { | |
| expect(config.params).toEqual({ authorId }); | |
| return [200, { code: 200 }]; | |
| }); | |
| const response = await deletePost(postId, authorId); | |
| expect(response.status).toBe(200); | |
| }); | |
| }); | |
| }); |