import MockAdapter from 'axios-mock-adapter'; | |
import { api } from './auth'; // Import api from auth | |
import { createTorrent, getTorrents, getTorrentDetail, likeTorrent, addTorrentComment } from './torrent'; | |
describe('种子资源API', () => { | |
let mockAxios; | |
beforeEach(() => { | |
mockAxios = new MockAdapter(api); | |
}); | |
afterEach(() => { | |
mockAxios.restore(); | |
}); | |
// Test for getting torrent detail | |
describe('getTorrentDetail - 获取种子详情', () => { | |
it('应该规范化返回的数据结构', async () => { | |
const mockData = { | |
data: { | |
post: { id: '123', name: '测试种子' }, | |
comments: [{ id: '1', content: '评论1' }] | |
} | |
}; | |
mockAxios.onGet('/torrent/123').reply(200, mockData); | |
const response = await getTorrentDetail('123'); | |
expect(response.data.data.torrent.name).toBe('测试种子'); // Corrected key to `post.name` | |
expect(response.data.data.comments).toHaveLength(1); | |
}); | |
}); | |
// Test for liking a torrent | |
describe('likeTorrent - 点赞种子', () => { | |
it('应该成功发送点赞请求', async () => { | |
mockAxios.onPost('/torrent/t123/like').reply(200, { code: 200 }); | |
const response = await likeTorrent('t123'); | |
expect(response.status).toBe(200); | |
}); | |
}); | |
}); |