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 { createTorrent, getTorrents, getTorrentDetail, likeTorrent, addTorrentComment } from './torrent';
|
| 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('createTorrent - 创建种子', () => {
|
| 17 | it('应该发送完整的种子数据', async () => {
|
| 18 | const torrentData = {
|
| 19 | torrentName: '测试种子',
|
| 20 | description: '描述内容',
|
| 21 | username: 'user1',
|
| 22 | category: '电影',
|
| 23 | region: '美国',
|
| 24 | resolution: '1080p',
|
| 25 | subtitle: '中文字幕',
|
| 26 | filePath: '/path/to/file'
|
| 27 | };
|
| 28 | mockAxios.onPost('/torrent', torrentData).reply(201, { code: 201 });
|
| 29 |
|
| 30 | const response = await createTorrent(...Object.values(torrentData));
|
| 31 | expect(response.status).toBe(201);
|
| 32 | });
|
| 33 | });
|
| 34 |
|
| 35 | describe('getTorrentDetail - 获取种子详情', () => {
|
| 36 | it('应该规范化返回的数据结构', async () => {
|
| 37 | const mockData = {
|
| 38 | data: {
|
| 39 | post: { id: '123', name: '测试种子' },
|
| 40 | comments: [{ id: '1', content: '评论1' }]
|
| 41 | }
|
| 42 | };
|
| 43 | mockAxios.onGet('/torrent/123').reply(200, mockData);
|
| 44 |
|
| 45 | const response = await getTorrentDetail('123');
|
| 46 | expect(response.data.data.torrent.name).toBe('测试种子');
|
| 47 | expect(response.data.data.comments).toHaveLength(1);
|
| 48 | });
|
| 49 | });
|
| 50 |
|
| 51 | describe('likeTorrent - 点赞种子', () => {
|
| 52 | it('应该成功发送点赞请求', async () => {
|
| 53 | mockAxios.onPost('/torrent/t123/like').reply(200, { code: 200 });
|
| 54 | const response = await likeTorrent('t123');
|
| 55 | expect(response.status).toBe(200);
|
| 56 | });
|
| 57 | });
|
| 58 | }); |