DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 1 | import MockAdapter from 'axios-mock-adapter'; |
| 2 | import { api } from './auth'; |
| 3 | import { |
| 4 | likeRequestPostComment, |
| 5 | getRequestCommentReplies, |
| 6 | addRequestCommentReply, |
| 7 | deleteRequestComment |
| 8 | } from './requestComment'; |
| 9 | |
| 10 | describe('求种帖评论API', () => { |
| 11 | let mockAxios; |
| 12 | |
| 13 | beforeEach(() => { |
| 14 | mockAxios = new MockAdapter(api); |
| 15 | }); |
| 16 | |
| 17 | afterEach(() => { |
| 18 | mockAxios.restore(); |
| 19 | }); |
| 20 | |
| 21 | describe('likeRequestPostComment - 点赞求种帖评论', () => { |
| 22 | it('应该成功发送点赞请求', async () => { |
| 23 | const commentId = 'reqc123'; |
| 24 | const mockResponse = { code: 200, message: '点赞成功' }; |
| 25 | mockAxios.onPost(`/request/comments/${commentId}/like`).reply(200, mockResponse); |
| 26 | |
| 27 | const response = await likeRequestPostComment(commentId); |
| 28 | expect(response.data).toEqual(mockResponse); |
| 29 | }); |
| 30 | }); |
| 31 | |
| 32 | describe('getRequestCommentReplies - 获取评论回复', () => { |
| 33 | it('应该返回正确的回复数据结构', async () => { |
| 34 | const commentId = 'reqc456'; |
| 35 | const mockData = [{ id: '1', content: '求种回复内容' }]; |
| 36 | mockAxios.onGet(`/request/comments/${commentId}/replies`) |
| 37 | .reply(200, { code: 200, data: mockData }); |
| 38 | |
| 39 | const response = await getRequestCommentReplies(commentId); |
| 40 | expect(response.data.data).toEqual(mockData); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | describe('addRequestCommentReply - 添加评论回复', () => { |
| 45 | it('应该正确发送回复内容(无图片)', async () => { |
| 46 | const commentId = 'reqc789'; |
| 47 | const replyData = { |
| 48 | authorId: 'user1', |
| 49 | content: '测试求种回复' |
| 50 | }; |
| 51 | |
| 52 | mockAxios.onPost(`/request/comments/${commentId}/replies`).reply(config => { |
| 53 | const data = config.data; |
| 54 | expect(data.get('authorId')).toBe(replyData.authorId); |
| 55 | expect(data.get('content')).toBe(replyData.content); |
| 56 | expect(data.has('image')).toBe(false); |
| 57 | return [200, { code: 200 }]; |
| 58 | }); |
| 59 | |
| 60 | const response = await addRequestCommentReply(commentId, replyData); |
| 61 | expect(response.status).toBe(200); |
| 62 | }); |
| 63 | |
| 64 | it('应该正确处理带图片的回复', async () => { |
| 65 | const commentId = 'reqc789'; |
| 66 | const replyData = { |
| 67 | authorId: 'user1', |
| 68 | content: '测试求种回复', |
| 69 | image: new File(['content'], 'reply.jpg') |
| 70 | }; |
| 71 | |
| 72 | mockAxios.onPost(`/request/comments/${commentId}/replies`).reply(config => { |
| 73 | const data = config.data; |
| 74 | expect(data.get('image')).toBeInstanceOf(File); |
| 75 | return [200, { code: 200 }]; |
| 76 | }); |
| 77 | |
| 78 | const response = await addRequestCommentReply(commentId, replyData); |
| 79 | expect(response.status).toBe(200); |
| 80 | }); |
| 81 | }); |
| 82 | |
| 83 | describe('deleteRequestComment - 删除评论', () => { |
| 84 | it('应该正确发送删除请求', async () => { |
| 85 | const commentId = 'reqc101'; |
| 86 | const authorId = 'user1'; |
| 87 | |
| 88 | mockAxios.onDelete(`/request/comments/${commentId}`).reply(config => { |
| 89 | expect(config.params).toEqual({ authorId }); |
| 90 | return [200, { code: 200 }]; |
| 91 | }); |
| 92 | |
| 93 | const response = await deleteRequestComment(commentId, authorId); |
| 94 | expect(response.status).toBe(200); |
| 95 | }); |
| 96 | }); |
| 97 | }); |