blob: a4a7d993d360b9da8a617cc3686807a6d771baf3 [file] [log] [blame]
Akane121765b61a72025-05-17 13:52:25 +08001import MockAdapter from 'axios-mock-adapter';
2import { api } from './auth'; // 添加api导入
Akane1217d1e9f712025-05-29 14:36:56 +08003import { likePostComment, getCommentReplies, addCommentReply, deleteComment } from './helpComment';
Akane121765b61a72025-05-17 13:52:25 +08004
5describe('求助帖评论API', () => {
6 let mockAxios;
7
8 beforeEach(() => {
9 mockAxios = new MockAdapter(api);
10 });
11
12 afterEach(() => {
13 mockAxios.restore();
14 });
15
16 describe('likePostComment - 点赞求助帖评论', () => {
17 it('应该成功发送点赞请求', async () => {
18 const mockResponse = { code: 200, message: '点赞成功' };
19 mockAxios.onPost('/help/comments/123/like').reply(200, mockResponse);
20
21 const response = await likePostComment('123');
22 expect(response.data).toEqual(mockResponse);
23 });
24 });
25
26 describe('getCommentReplies - 获取评论回复', () => {
27 it('应该返回正确的回复数据结构', async () => {
28 const mockData = [{ id: '1', content: '回复内容' }];
29 mockAxios.onGet('/help/comments/456/replies').reply(200, { code: 200, data: mockData });
30
31 const response = await getCommentReplies('456');
32 expect(response.data.data).toEqual(mockData);
33 });
34 });
35
36 describe('addCommentReply - 添加评论回复', () => {
Akane1217d1e9f712025-05-29 14:36:56 +080037 it('应该正确发送回复内容(无图片)', async () => {
38 const commentId = '789';
39 const replyData = {
40 authorId: 'user1',
41 content: '测试回复'
42 };
43
44 mockAxios.onPost(`/help/comments/${commentId}/replies`).reply(config => {
45 const data = config.data;
46 expect(data.get('authorId')).toBe(replyData.authorId);
47 expect(data.get('content')).toBe(replyData.content);
48 expect(data.has('image')).toBe(false);
49 return [200, { code: 200 }];
50 });
Akane121765b61a72025-05-17 13:52:25 +080051
Akane1217d1e9f712025-05-29 14:36:56 +080052 const response = await addCommentReply(commentId, replyData);
53 expect(response.status).toBe(200);
54 });
55 it('应该正确处理带图片的回复', async () => {
56 const commentId = '789';
57 const replyData = {
58 authorId: 'user1',
59 content: '测试回复',
60 image: new File(['content'], 'reply.jpg')
61 };
62
63 mockAxios.onPost(`/help/comments/${commentId}/replies`).reply(config => {
64 const data = config.data;
65 expect(data.get('image')).toBeInstanceOf(File);
66 return [200, { code: 200 }];
67 });
68
69 const response = await addCommentReply(commentId, replyData);
70 expect(response.status).toBe(200);
71 });
72 });
73 describe('deleteComment - 删除评论', () => {
74 it('应该正确发送删除请求', async () => {
75 const commentId = '101112';
76 const authorId = 'user1';
77
78 mockAxios.onDelete(`/help/comments/${commentId}`).reply(config => {
79 expect(config.params).toEqual({ authorId });
80 return [200, { code: 200 }];
81 });
82
83 const response = await deleteComment(commentId, authorId);
Akane121765b61a72025-05-17 13:52:25 +080084 expect(response.status).toBe(200);
85 });
86 });
87});