个人中心全部,模糊乱序搜索,类型筛选
Change-Id: Id635654fccccaea80bfbf4d1480abd55f7d12046
diff --git a/src/api/torrent.test.js b/src/api/torrent.test.js
index ca755c3..6515bdc 100644
--- a/src/api/torrent.test.js
+++ b/src/api/torrent.test.js
@@ -1,41 +1,134 @@
import MockAdapter from 'axios-mock-adapter';
-import { api } from './auth'; // Import api from auth
-import { createTorrent, getTorrents, getTorrentDetail, likeTorrent, addTorrentComment } from './torrent';
+import { api } from './auth';
+import {
+ createTorrent,
+ getTorrents,
+ getTorrentDetail,
+ likeTorrent,
+ addTorrentComment,
+ searchTorrents
+} from './torrent';
describe('种子资源API', () => {
let mockAxios;
beforeEach(() => {
mockAxios = new MockAdapter(api);
+ localStorage.setItem('token', 'test-token');
});
afterEach(() => {
mockAxios.restore();
+ localStorage.clear();
});
- // Test for getting torrent detail
+ describe('createTorrent - 创建种子', () => {
+ it('应该正确发送包含文件和数据的表单请求', async () => {
+ const mockFile = new File(['test'], 'test.torrent');
+ const torrentData = { name: '测试种子', description: '测试描述' };
+ const mockResponse = { code: 200, message: '创建成功' };
+
+ mockAxios.onPost('/torrent').reply((config) => {
+ expect(config.headers['Content-Type']).toBe('multipart/form-data');
+ expect(config.headers['Authorization']).toBe('Bearer test-token'); // 修改为包含Bearer
+ return [200, mockResponse];
+ });
+
+ const response = await createTorrent(torrentData, mockFile);
+ expect(response.data).toEqual(mockResponse);
+ });
+ });
+
+ describe('getTorrents - 获取种子列表', () => {
+ it('应该发送带分页参数的请求', async () => {
+ const mockResponse = {
+ code: 200,
+ data: {
+ list: [{ id: 1, name: '种子1' }, { id: 2, name: '种子2' }],
+ total: 2
+ }
+ };
+
+ mockAxios.onGet('/torrent', { params: { page: 2, size: 10 } })
+ .reply(200, mockResponse);
+
+ const response = await getTorrents(2, 10);
+ expect(response.data).toEqual(mockResponse);
+ });
+ });
+
describe('getTorrentDetail - 获取种子详情', () => {
it('应该规范化返回的数据结构', async () => {
- const mockData = {
+ const mockResponse = {
+ code: 200,
data: {
- post: { id: '123', name: '测试种子' },
+ torrent: { id: '123', name: '测试种子' },
comments: [{ id: '1', content: '评论1' }]
}
};
- mockAxios.onGet('/torrent/123').reply(200, mockData);
+
+ mockAxios.onGet('/torrent/123').reply(200, mockResponse);
const response = await getTorrentDetail('123');
- expect(response.data.data.torrent.name).toBe('测试种子'); // Corrected key to `post.name`
+ expect(response.data.data.torrent.name).toBe('测试种子');
expect(response.data.data.comments).toHaveLength(1);
});
+
+ it('应该处理没有评论的情况', async () => {
+ const mockResponse = {
+ code: 200,
+ data: {
+ torrent: { id: '123', name: '测试种子' },
+ comments: null
+ }
+ };
+
+ mockAxios.onGet('/torrent/123').reply(200, mockResponse);
+
+ const response = await getTorrentDetail('123');
+ expect(response.data.data.comments).toEqual([]);
+ });
});
- // 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);
+ it('应该发送点赞请求', async () => {
+ const mockResponse = { code: 200, message: '点赞成功' };
+ mockAxios.onPost('/torrent/123/like').reply(200, mockResponse);
+
+ const response = await likeTorrent('123');
+ expect(response.data).toEqual(mockResponse);
});
});
-});
+
+ describe('addTorrentComment - 添加种子评论', () => {
+ it('应该发送评论数据', async () => {
+ const commentData = { content: '测试评论' };
+ const mockResponse = { code: 200, message: '评论成功' };
+
+ mockAxios.onPost('/torrent/123/comments', commentData)
+ .reply(200, mockResponse);
+
+ const response = await addTorrentComment('123', commentData);
+ expect(response.data).toEqual(mockResponse);
+ });
+ });
+
+ describe('searchTorrents - 搜索种子', () => {
+ it('应该发送带搜索关键词和分页的请求', async () => {
+ const mockResponse = {
+ code: 200,
+ data: {
+ list: [{ id: 1, name: '匹配的种子' }],
+ total: 1
+ }
+ };
+
+ mockAxios.onGet('/torrent/search', {
+ params: { keyword: '测试', page: 1, size: 5 }
+ }).reply(200, mockResponse);
+
+ const response = await searchTorrents('测试');
+ expect(response.data).toEqual(mockResponse);
+ });
+ });
+});
\ No newline at end of file