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 | describe('createTorrent - 创建种子', () => {
|
| 26 | it('应该正确发送包含文件和数据的表单请求', async () => {
|
| 27 | const mockFile = new File(['test'], 'test.torrent');
|
| 28 | const torrentData = { name: '测试种子', description: '测试描述' };
|
| 29 | const mockResponse = { code: 200, message: '创建成功' };
|
| 30 |
|
| 31 | mockAxios.onPost('/torrent').reply((config) => {
|
| 32 | expect(config.headers['Content-Type']).toBe('multipart/form-data');
|
| 33 | expect(config.headers['Authorization']).toBe('Bearer test-token'); // 修改为包含Bearer
|
| 34 | return [200, mockResponse];
|
| 35 | });
|
| 36 |
|
| 37 | const response = await createTorrent(torrentData, mockFile);
|
| 38 | expect(response.data).toEqual(mockResponse);
|
| 39 | });
|
| 40 | });
|
| 41 |
|
| 42 | describe('getTorrents - 获取种子列表', () => {
|
| 43 | it('应该发送带分页参数的请求', async () => {
|
| 44 | const mockResponse = {
|
| 45 | code: 200,
|
| 46 | data: {
|
| 47 | list: [{ id: 1, name: '种子1' }, { id: 2, name: '种子2' }],
|
| 48 | total: 2
|
| 49 | }
|
| 50 | };
|
| 51 |
|
| 52 | mockAxios.onGet('/torrent', { params: { page: 2, size: 10 } })
|
| 53 | .reply(200, mockResponse);
|
| 54 |
|
| 55 | const response = await getTorrents(2, 10);
|
| 56 | expect(response.data).toEqual(mockResponse);
|
| 57 | });
|
| 58 | });
|
| 59 |
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 60 | describe('getTorrentDetail - 获取种子详情', () => {
|
| 61 | it('应该规范化返回的数据结构', async () => {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 62 | const mockResponse = {
|
| 63 | code: 200,
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 64 | data: {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 65 | torrent: { id: '123', name: '测试种子' },
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 66 | comments: [{ id: '1', content: '评论1' }]
|
| 67 | }
|
| 68 | };
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 69 |
|
| 70 | mockAxios.onGet('/torrent/123').reply(200, mockResponse);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 71 |
|
| 72 | const response = await getTorrentDetail('123');
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 73 | expect(response.data.data.torrent.name).toBe('测试种子');
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 74 | expect(response.data.data.comments).toHaveLength(1);
|
| 75 | });
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 76 |
|
| 77 | it('应该处理没有评论的情况', async () => {
|
| 78 | const mockResponse = {
|
| 79 | code: 200,
|
| 80 | data: {
|
| 81 | torrent: { id: '123', name: '测试种子' },
|
| 82 | comments: null
|
| 83 | }
|
| 84 | };
|
| 85 |
|
| 86 | mockAxios.onGet('/torrent/123').reply(200, mockResponse);
|
| 87 |
|
| 88 | const response = await getTorrentDetail('123');
|
| 89 | expect(response.data.data.comments).toEqual([]);
|
| 90 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 91 | });
|
| 92 |
|
| 93 | describe('likeTorrent - 点赞种子', () => {
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 94 | it('应该发送点赞请求', async () => {
|
| 95 | const mockResponse = { code: 200, message: '点赞成功' };
|
| 96 | mockAxios.onPost('/torrent/123/like').reply(200, mockResponse);
|
| 97 |
|
| 98 | const response = await likeTorrent('123');
|
| 99 | expect(response.data).toEqual(mockResponse);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 100 | });
|
| 101 | });
|
Akane1217 | 3a7bb97 | 2025-06-01 01:05:27 +0800 | [diff] [blame^] | 102 |
|
| 103 | describe('addTorrentComment - 添加种子评论', () => {
|
| 104 | it('应该发送评论数据', async () => {
|
| 105 | const commentData = { content: '测试评论' };
|
| 106 | const mockResponse = { code: 200, message: '评论成功' };
|
| 107 |
|
| 108 | mockAxios.onPost('/torrent/123/comments', commentData)
|
| 109 | .reply(200, mockResponse);
|
| 110 |
|
| 111 | const response = await addTorrentComment('123', commentData);
|
| 112 | expect(response.data).toEqual(mockResponse);
|
| 113 | });
|
| 114 | });
|
| 115 |
|
| 116 | describe('searchTorrents - 搜索种子', () => {
|
| 117 | it('应该发送带搜索关键词和分页的请求', async () => {
|
| 118 | const mockResponse = {
|
| 119 | code: 200,
|
| 120 | data: {
|
| 121 | list: [{ id: 1, name: '匹配的种子' }],
|
| 122 | total: 1
|
| 123 | }
|
| 124 | };
|
| 125 |
|
| 126 | mockAxios.onGet('/torrent/search', {
|
| 127 | params: { keyword: '测试', page: 1, size: 5 }
|
| 128 | }).reply(200, mockResponse);
|
| 129 |
|
| 130 | const response = await searchTorrents('测试');
|
| 131 | expect(response.data).toEqual(mockResponse);
|
| 132 | });
|
| 133 | });
|
| 134 | }); |