blob: 6cd481e07a33dc922e60aaaa65c9eb94465ccd3a [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
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
Akane121765b61a72025-05-17 13:52:25 +080044 describe('getTorrentDetail - 获取种子详情', () => {
45 it('应该规范化返回的数据结构', async () => {
Akane12173a7bb972025-06-01 01:05:27 +080046 const mockResponse = {
47 code: 200,
Akane121765b61a72025-05-17 13:52:25 +080048 data: {
Akane12173a7bb972025-06-01 01:05:27 +080049 torrent: { id: '123', name: '测试种子' },
Akane121765b61a72025-05-17 13:52:25 +080050 comments: [{ id: '1', content: '评论1' }]
51 }
52 };
Akane12173a7bb972025-06-01 01:05:27 +080053
54 mockAxios.onGet('/torrent/123').reply(200, mockResponse);
Akane121765b61a72025-05-17 13:52:25 +080055
56 const response = await getTorrentDetail('123');
Akane12173a7bb972025-06-01 01:05:27 +080057 expect(response.data.data.torrent.name).toBe('测试种子');
Akane121765b61a72025-05-17 13:52:25 +080058 expect(response.data.data.comments).toHaveLength(1);
59 });
Akane12173a7bb972025-06-01 01:05:27 +080060
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 });
Akane121765b61a72025-05-17 13:52:25 +080075 });
76
77 describe('likeTorrent - 点赞种子', () => {
Akane12173a7bb972025-06-01 01:05:27 +080078 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);
Akane121765b61a72025-05-17 13:52:25 +080084 });
85 });
Akane12173a7bb972025-06-01 01:05:27 +080086
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});