Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | import MockAdapter from 'axios-mock-adapter';
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 2 | import { api } from './auth';
|
| 3 | import {
|
| 4 | createTorrent,
|
| 5 | getTorrents,
|
| 6 | getTorrentDetail,
|
| 7 | likeTorrent,
|
| 8 | addTorrentComment,
|
| 9 | searchTorrents
|
| 10 | } from './torrent';
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 11 |
|
| 12 | describe('种子资源API', () => {
|
| 13 | let mockAxios;
|
| 14 |
|
| 15 | beforeEach(() => {
|
| 16 | mockAxios = new MockAdapter(api);
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 17 | localStorage.setItem('token', 'test-token');
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 18 | });
|
| 19 |
|
| 20 | afterEach(() => {
|
| 21 | mockAxios.restore();
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 22 | localStorage.clear();
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 23 | });
|
| 24 |
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 25 |
|
| 26 | describe('getTorrents - 获取种子列表', () => {
|
| 27 | it('应该发送带分页参数的请求', async () => {
|
| 28 | const mockResponse = {
|
| 29 | code: 200,
|
| 30 | data: {
|
| 31 | list: [{ id: 1, name: '种子1' }, { id: 2, name: '种子2' }],
|
| 32 | total: 2
|
| 33 | }
|
| 34 | };
|
| 35 |
|
| 36 | mockAxios.onGet('/torrent', { params: { page: 2, size: 10 } })
|
| 37 | .reply(200, mockResponse);
|
| 38 |
|
| 39 | const response = await getTorrents(2, 10);
|
| 40 | expect(response.data).toEqual(mockResponse);
|
| 41 | });
|
| 42 | });
|
| 43 |
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 44 | describe('getTorrentDetail - 获取种子详情', () => {
|
| 45 | it('应该规范化返回的数据结构', async () => {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 46 | const mockResponse = {
|
| 47 | code: 200,
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 48 | data: {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 49 | torrent: { id: '123', name: '测试种子' },
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 50 | comments: [{ id: '1', content: '评论1' }]
|
| 51 | }
|
| 52 | };
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 53 |
|
| 54 | mockAxios.onGet('/torrent/123').reply(200, mockResponse);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 55 |
|
| 56 | const response = await getTorrentDetail('123');
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 57 | expect(response.data.data.torrent.name).toBe('测试种子');
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 58 | expect(response.data.data.comments).toHaveLength(1);
|
| 59 | });
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 60 |
|
| 61 | it('应该处理没有评论的情况', async () => {
|
| 62 | const mockResponse = {
|
| 63 | code: 200,
|
| 64 | data: {
|
| 65 | torrent: { id: '123', name: '测试种子' },
|
| 66 | comments: null
|
| 67 | }
|
| 68 | };
|
| 69 |
|
| 70 | mockAxios.onGet('/torrent/123').reply(200, mockResponse);
|
| 71 |
|
| 72 | const response = await getTorrentDetail('123');
|
| 73 | expect(response.data.data.comments).toEqual([]);
|
| 74 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 75 | });
|
| 76 |
|
| 77 | describe('likeTorrent - 点赞种子', () => {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 78 | it('应该发送点赞请求', async () => {
|
| 79 | const mockResponse = { code: 200, message: '点赞成功' };
|
| 80 | mockAxios.onPost('/torrent/123/like').reply(200, mockResponse);
|
| 81 |
|
| 82 | const response = await likeTorrent('123');
|
| 83 | expect(response.data).toEqual(mockResponse);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 84 | });
|
| 85 | });
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame] | 86 |
|
| 87 | describe('addTorrentComment - 添加种子评论', () => {
|
| 88 | it('应该发送评论数据', async () => {
|
| 89 | const commentData = { content: '测试评论' };
|
| 90 | const mockResponse = { code: 200, message: '评论成功' };
|
| 91 |
|
| 92 | mockAxios.onPost('/torrent/123/comments', commentData)
|
| 93 | .reply(200, mockResponse);
|
| 94 |
|
| 95 | const response = await addTorrentComment('123', commentData);
|
| 96 | expect(response.data).toEqual(mockResponse);
|
| 97 | });
|
| 98 | });
|
| 99 |
|
| 100 | describe('searchTorrents - 搜索种子', () => {
|
| 101 | it('应该发送带搜索关键词和分页的请求', async () => {
|
| 102 | const mockResponse = {
|
| 103 | code: 200,
|
| 104 | data: {
|
| 105 | list: [{ id: 1, name: '匹配的种子' }],
|
| 106 | total: 1
|
| 107 | }
|
| 108 | };
|
| 109 |
|
| 110 | mockAxios.onGet('/torrent/search', {
|
| 111 | params: { keyword: '测试', page: 1, size: 5 }
|
| 112 | }).reply(200, mockResponse);
|
| 113 |
|
| 114 | const response = await searchTorrents('测试');
|
| 115 | expect(response.data).toEqual(mockResponse);
|
| 116 | });
|
| 117 | });
|
| 118 | }); |