blob: 9c72182ae75e485e700f073fd1553316d9d20f3e [file] [log] [blame]
Akane12173a7bb972025-06-01 01:05:27 +08001// Upload.test.jsx
2import React from 'react';
3import { render, screen, waitFor, fireEvent } from '@testing-library/react';
4import { MemoryRouter, useNavigate, useLocation } from 'react-router-dom';
5import Upload from './Upload';
6import { getUserTorrents, deleteTorrent } from '../../api/personal';
7
8// Mock API 调用
9jest.mock('../../api/personal', () => ({
10 getUserTorrents: jest.fn(),
11 deleteTorrent: jest.fn()
12}));
13
14// Mock react-router-dom hooks
15jest.mock('react-router-dom', () => ({
16 ...jest.requireActual('react-router-dom'),
17 useNavigate: jest.fn(),
18 useLocation: jest.fn()
19}));
20
21// Mock window.confirm
22global.confirm = jest.fn(() => true);
23
24describe('Upload Component', () => {
25 const mockNavigate = jest.fn();
26 const mockLocation = {
27 pathname: '/personal/upload',
28 state: { dashboardTab: 'uploads' }
29 };
30
31 const mockTorrents = [
32 {
33 id: 1,
34 torrentName: 'Test Torrent 1',
35 formattedSize: '1.2 GB',
36 createTime: '2023-01-01T12:00:00Z',
37 downloadCount: 10
38 },
39 {
40 id: 2,
41 torrentName: 'Test Torrent 2',
42 formattedSize: '2.5 GB',
43 createTime: '2023-01-02T12:00:00Z',
44 downloadCount: 5
45 }
46 ];
47
48 beforeEach(() => {
49 useNavigate.mockReturnValue(mockNavigate);
50 useLocation.mockReturnValue(mockLocation);
51 jest.clearAllMocks();
52 getUserTorrents.mockResolvedValue({
53 records: mockTorrents,
54 total: 10
55 });
56 deleteTorrent.mockResolvedValue({ success: true });
57 });
58
59 it('应该正确加载并显示上传记录', async () => {
60 render(
61 <MemoryRouter>
62 <Upload />
63 </MemoryRouter>
64 );
65
66 // 初始加载状态
67 expect(screen.getByText('加载中...')).toBeInTheDocument();
68
69 // 等待数据加载完成
70 await waitFor(() => {
71 expect(screen.getByText('上传记录')).toBeInTheDocument();
72 expect(screen.getByText('Test Torrent 1')).toBeInTheDocument();
73 expect(screen.getByText('Test Torrent 2')).toBeInTheDocument();
74 expect(screen.getByText('1.2 GB')).toBeInTheDocument();
75 expect(screen.getByText('2.5 GB')).toBeInTheDocument();
76 expect(screen.getAllByText('删除')).toHaveLength(2);
77 });
78 });
79
80 it('应该处理删除操作', async () => {
81 render(
82 <MemoryRouter>
83 <Upload />
84 </MemoryRouter>
85 );
86
87 await waitFor(() => {
88 const deleteButtons = screen.getAllByText('删除');
89 fireEvent.click(deleteButtons[0]);
90 });
91
92 expect(global.confirm).toHaveBeenCalledWith('确定要删除这个种子吗?此操作不可撤销!');
93 await waitFor(() => {
94 expect(deleteTorrent).toHaveBeenCalledWith(1);
95 expect(getUserTorrents).toHaveBeenCalledTimes(2); // 初始加载 + 删除后刷新
96 });
97 });
98
99 it('应该处理分页变化', async () => {
100 render(
101 <MemoryRouter>
102 <Upload />
103 </MemoryRouter>
104 );
105
106 await waitFor(() => {
107 const nextPageButton = screen.getByText('下一页');
108 fireEvent.click(nextPageButton);
109 });
110
111 await waitFor(() => {
112 expect(getUserTorrents).toHaveBeenLastCalledWith(2, 5);
113 });
114 });
115
116 it('应该处理直接跳转页码', async () => {
117 render(
118 <MemoryRouter>
119 <Upload />
120 </MemoryRouter>
121 );
122
123 await waitFor(() => {
124 const pageInput = screen.getByRole('spinbutton');
125 fireEvent.change(pageInput, { target: { value: '2' } });
126 fireEvent.keyDown(pageInput, { key: 'Enter' });
127 });
128
129 await waitFor(() => {
130 expect(getUserTorrents).toHaveBeenLastCalledWith(2, 5);
131 }, { timeout: 1000 });
132 });
133
134 it('应该处理返回按钮点击', async () => {
135 render(
136 <MemoryRouter>
137 <Upload />
138 </MemoryRouter>
139 );
140
141 await waitFor(() => {
142 const backButton = screen.getByText((content) =>
143 content.includes('返回个人中心')
144 );
145 fireEvent.click(backButton);
146
147 expect(mockNavigate).toHaveBeenCalledWith('/personal', {
148 state: {
149 fromSubpage: true,
150 dashboardTab: 'uploads'
151 },
152 replace: true
153 });
154 });
155 });
156
157 it('应该显示错误信息当API调用失败', async () => {
158 getUserTorrents.mockRejectedValue(new Error('获取上传记录失败'));
159
160 render(
161 <MemoryRouter>
162 <Upload />
163 </MemoryRouter>
164 );
165
166 await waitFor(() => {
167 expect(screen.getByText('错误: 获取上传记录失败')).toBeInTheDocument();
168 });
169 });
170
171
172 it('应该禁用分页按钮当在第一页或最后一页', async () => {
173 render(
174 <MemoryRouter>
175 <Upload />
176 </MemoryRouter>
177 );
178
179 await waitFor(() => {
180 const prevButton = screen.getByText('上一页');
181 const firstPageButton = screen.getByText('首页');
182
183 expect(prevButton).toBeDisabled();
184 expect(firstPageButton).toBeDisabled();
185 });
186 });
187
188 it('应该显示正确的页码导航', async () => {
189 // 模拟有更多页的情况
190 getUserTorrents.mockResolvedValue({
191 records: mockTorrents,
192 total: 50
193 });
194
195 render(
196 <MemoryRouter>
197 <Upload />
198 </MemoryRouter>
199 );
200
201 await waitFor(() => {
202 expect(screen.getByText('...')).toBeInTheDocument();
203 expect(screen.getByText('共 10 页')).toBeInTheDocument();
204 });
205 });
206});