blob: 6515bdc4f695dc805bedb117ace035c036c793b7 [file] [log] [blame]
Akane121765b61a72025-05-17 13:52:25 +08001import MockAdapter from 'axios-mock-adapter';
Akane12173a7bb972025-06-01 01:05:27 +08002import { api } from './auth';
3import {
4 createTorrent,
5 getTorrents,
6 getTorrentDetail,
7 likeTorrent,
8 addTorrentComment,
9 searchTorrents
10} from './torrent';
Akane121765b61a72025-05-17 13:52:25 +080011
12describe('种子资源API', () => {
13 let mockAxios;
14
15 beforeEach(() => {
16 mockAxios = new MockAdapter(api);
Akane12173a7bb972025-06-01 01:05:27 +080017 localStorage.setItem('token', 'test-token');
Akane121765b61a72025-05-17 13:52:25 +080018 });
19
20 afterEach(() => {
21 mockAxios.restore();
Akane12173a7bb972025-06-01 01:05:27 +080022 localStorage.clear();
Akane121765b61a72025-05-17 13:52:25 +080023 });
24
Akane12173a7bb972025-06-01 01:05:27 +080025 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
Akane121765b61a72025-05-17 13:52:25 +080060 describe('getTorrentDetail - 获取种子详情', () => {
61 it('应该规范化返回的数据结构', async () => {
Akane12173a7bb972025-06-01 01:05:27 +080062 const mockResponse = {
63 code: 200,
Akane121765b61a72025-05-17 13:52:25 +080064 data: {
Akane12173a7bb972025-06-01 01:05:27 +080065 torrent: { id: '123', name: '测试种子' },
Akane121765b61a72025-05-17 13:52:25 +080066 comments: [{ id: '1', content: '评论1' }]
67 }
68 };
Akane12173a7bb972025-06-01 01:05:27 +080069
70 mockAxios.onGet('/torrent/123').reply(200, mockResponse);
Akane121765b61a72025-05-17 13:52:25 +080071
72 const response = await getTorrentDetail('123');
Akane12173a7bb972025-06-01 01:05:27 +080073 expect(response.data.data.torrent.name).toBe('测试种子');
Akane121765b61a72025-05-17 13:52:25 +080074 expect(response.data.data.comments).toHaveLength(1);
75 });
Akane12173a7bb972025-06-01 01:05:27 +080076
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 });
Akane121765b61a72025-05-17 13:52:25 +080091 });
92
93 describe('likeTorrent - 点赞种子', () => {
Akane12173a7bb972025-06-01 01:05:27 +080094 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);
Akane121765b61a72025-05-17 13:52:25 +0800100 });
101 });
Akane12173a7bb972025-06-01 01:05:27 +0800102
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});