Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame^] | 1 | import MockAdapter from 'axios-mock-adapter';
|
| 2 | import { api } from './auth'; // 添加api导入
|
| 3 | import { likeTorrentComment, getCommentReplies, addCommentReply } from './torrentComment';
|
| 4 |
|
| 5 | describe('种子评论API', () => {
|
| 6 | let mockAxios;
|
| 7 |
|
| 8 | beforeEach(() => {
|
| 9 | mockAxios = new MockAdapter(api);
|
| 10 | });
|
| 11 |
|
| 12 | afterEach(() => {
|
| 13 | mockAxios.restore();
|
| 14 | });
|
| 15 |
|
| 16 | describe('likeTorrentComment - 点赞种子评论', () => {
|
| 17 | it('应该成功发送点赞请求', async () => {
|
| 18 | const commentId = '123';
|
| 19 | const mockResponse = { code: 200, message: '点赞成功' };
|
| 20 |
|
| 21 | mockAxios.onPost(`/torrent/comments/${commentId}/like`).reply(200, mockResponse);
|
| 22 |
|
| 23 | const response = await likeTorrentComment(commentId);
|
| 24 | expect(response.data).toEqual(mockResponse);
|
| 25 | });
|
| 26 |
|
| 27 | it('应该处理点赞失败的情况', async () => {
|
| 28 | mockAxios.onPost('/torrent/comments/123/like').reply(500);
|
| 29 | await expect(likeTorrentComment('123')).rejects.toThrow();
|
| 30 | });
|
| 31 | });
|
| 32 |
|
| 33 | describe('getCommentReplies - 获取评论回复', () => {
|
| 34 | it('应该成功获取回复列表', async () => {
|
| 35 | const commentId = '456';
|
| 36 | const mockResponse = {
|
| 37 | code: 200,
|
| 38 | data: [{ id: '1', content: '回复1' }, { id: '2', content: '回复2' }]
|
| 39 | };
|
| 40 |
|
| 41 | mockAxios.onGet(`/torrent/comments/${commentId}/replies`).reply(200, mockResponse);
|
| 42 |
|
| 43 | const response = await getCommentReplies(commentId);
|
| 44 | expect(response.data).toEqual(mockResponse);
|
| 45 | });
|
| 46 | });
|
| 47 |
|
| 48 | describe('addCommentReply - 添加评论回复', () => {
|
| 49 | it('应该成功添加回复', async () => {
|
| 50 | const commentId = '789';
|
| 51 | const replyData = { content: '测试回复' };
|
| 52 | const mockResponse = { code: 200, data: { id: '3', ...replyData } };
|
| 53 |
|
| 54 | mockAxios.onPost(`/torrent/comments/${commentId}/replies`, replyData).reply(200, mockResponse);
|
| 55 |
|
| 56 | const response = await addCommentReply(commentId, replyData);
|
| 57 | expect(response.data).toEqual(mockResponse);
|
| 58 | });
|
| 59 | });
|
| 60 | }); |