刘嘉昕 | b845fa0 | 2025-06-09 17:34:59 +0800 | [diff] [blame] | 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 3 | import { MemoryRouter } from 'react-router-dom'; |
| 4 | import UploadTorrent from '../components/upload'; |
| 5 | import { Form, Select } from 'antd'; |
| 6 | |
| 7 | // 模拟 axios |
| 8 | vi.mock('axios'); |
| 9 | |
| 10 | // 模拟 antd 的 message |
| 11 | vi.mock('antd', async () => { |
| 12 | const actual = await vi.importActual('antd'); |
| 13 | return { |
| 14 | ...actual, |
| 15 | message: { |
| 16 | success: vi.fn(), |
| 17 | error: vi.fn(), |
| 18 | }, |
| 19 | }; |
| 20 | }); |
| 21 | |
| 22 | describe('UploadTorrent 组件', () => { |
| 23 | const mockCategories = [ |
| 24 | { categoryid: '1', category_name: '电影' }, |
| 25 | { categoryid: '2', category_name: '剧集' }, |
| 26 | { categoryid: '3', category_name: '音乐' }, |
| 27 | { categoryid: '4', category_name: '动漫' }, |
| 28 | { categoryid: '5', category_name: '游戏' }, |
| 29 | { categoryid: '6', category_name: '综艺' }, |
| 30 | { categoryid: '7', category_name: '体育' }, |
| 31 | { categoryid: '8', category_name: '软件' }, |
| 32 | { categoryid: '9', category_name: '学习资料' }, |
| 33 | { categoryid: '10', category_name: '纪录片' }, |
| 34 | { categoryid: '11', category_name: '音乐' }, |
| 35 | ]; |
| 36 | |
| 37 | beforeEach(() => { |
| 38 | vi.clearAllMocks(); |
| 39 | // 模拟 axios.get 返回分类数据 |
| 40 | vi.mocked(axios.get).mockResolvedValueOnce({ data: mockCategories }); |
| 41 | }); |
| 42 | |
| 43 | it('应该正确渲染而不崩溃', async () => { |
| 44 | render( |
| 45 | <MemoryRouter> |
| 46 | <UploadTorrent /> |
| 47 | </MemoryRouter> |
| 48 | ); |
| 49 | |
| 50 | // 等待分类数据加载 |
| 51 | await waitFor(() => { |
| 52 | expect(screen.getByText('上传种子')).toBeInTheDocument(); |
| 53 | }); |
| 54 | |
| 55 | // 验证一些基本元素是否存在 |
| 56 | expect(screen.getByText('封面图片')).toBeInTheDocument(); |
| 57 | expect(screen.getByText('种子文件')).toBeInTheDocument(); |
| 58 | expect(screen.getByText('标题')).toBeInTheDocument(); |
| 59 | expect(screen.getByText('描述')).toBeInTheDocument(); |
| 60 | expect(screen.getByText('分类')).toBeInTheDocument(); |
| 61 | expect(screen.getByText('上传种子')).toBeInTheDocument(); |
| 62 | }); |
| 63 | |
| 64 | it('应该加载分类数据', async () => { |
| 65 | render( |
| 66 | <MemoryRouter> |
| 67 | <UploadTorrent /> |
| 68 | </MemoryRouter> |
| 69 | ); |
| 70 | |
| 71 | // 等待分类数据加载 |
| 72 | await waitFor(() => { |
| 73 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 74 | }); |
| 75 | |
| 76 | // 验证分类下拉框是否渲染了正确的选项 |
| 77 | const categorySelect = screen.getByLabelText('分类'); |
| 78 | expect(categorySelect).toBeInTheDocument(); |
| 79 | |
| 80 | // 打开下拉菜单并检查选项 |
| 81 | fireEvent.mouseDown(categorySelect); |
| 82 | mockCategories.forEach(category => { |
| 83 | expect(screen.getByText(category.category_name)).toBeInTheDocument(); |
| 84 | }); |
| 85 | }); |
| 86 | |
| 87 | it('应该在选择分类后显示相应的动态字段', async () => { |
| 88 | render( |
| 89 | <MemoryRouter> |
| 90 | <UploadTorrent /> |
| 91 | </MemoryRouter> |
| 92 | ); |
| 93 | |
| 94 | // 等待分类数据加载 |
| 95 | await waitFor(() => { |
| 96 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 97 | }); |
| 98 | |
| 99 | // 选择"电影"分类 |
| 100 | const categorySelect = screen.getByLabelText('分类'); |
| 101 | fireEvent.mouseDown(categorySelect); |
| 102 | fireEvent.click(screen.getByText('电影')); |
| 103 | |
| 104 | // 验证电影相关的字段是否显示 |
| 105 | expect(screen.getByLabelText('字幕/说明')).toBeInTheDocument(); |
| 106 | expect(screen.getByLabelText('地区')).toBeInTheDocument(); |
| 107 | expect(screen.getByLabelText('年份')).toBeInTheDocument(); |
| 108 | expect(screen.getByLabelText('类型')).toBeInTheDocument(); |
| 109 | expect(screen.getByLabelText('编码格式')).toBeInTheDocument(); |
| 110 | expect(screen.getByLabelText('分辨率')).toBeInTheDocument(); |
| 111 | }); |
| 112 | |
| 113 | it('应该在选择"剧集"分类后显示相应的动态字段', async () => { |
| 114 | render( |
| 115 | <MemoryRouter> |
| 116 | <UploadTorrent /> |
| 117 | </MemoryRouter> |
| 118 | ); |
| 119 | |
| 120 | // 等待分类数据加载 |
| 121 | await waitFor(() => { |
| 122 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 123 | }); |
| 124 | |
| 125 | // 选择"剧集"分类 |
| 126 | const categorySelect = screen.getByLabelText('分类'); |
| 127 | fireEvent.mouseDown(categorySelect); |
| 128 | fireEvent.click(screen.getByText('剧集')); |
| 129 | |
| 130 | // 验证剧集相关的字段是否显示 |
| 131 | expect(screen.getByLabelText('地区')).toBeInTheDocument(); |
| 132 | expect(screen.getByLabelText('格式')).toBeInTheDocument(); |
| 133 | expect(screen.getByLabelText('类型')).toBeInTheDocument(); |
| 134 | }); |
| 135 | |
| 136 | it('应该在选择"游戏"分类后显示相应的动态字段', async () => { |
| 137 | render( |
| 138 | <MemoryRouter> |
| 139 | <UploadTorrent /> |
| 140 | </MemoryRouter> |
| 141 | ); |
| 142 | |
| 143 | // 等待分类数据加载 |
| 144 | await waitFor(() => { |
| 145 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 146 | }); |
| 147 | |
| 148 | // 选择"游戏"分类 |
| 149 | const categorySelect = screen.getByLabelText('分类'); |
| 150 | fireEvent.mouseDown(categorySelect); |
| 151 | fireEvent.click(screen.getByText('游戏')); |
| 152 | |
| 153 | // 验证游戏相关的字段是否显示 |
| 154 | expect(screen.getByLabelText('平台')).toBeInTheDocument(); |
| 155 | expect(screen.getByLabelText('类型')).toBeInTheDocument(); |
| 156 | expect(screen.getByLabelText('语言')).toBeInTheDocument(); |
| 157 | expect(screen.getByLabelText('数据类型')).toBeInTheDocument(); |
| 158 | }); |
| 159 | |
| 160 | it('应该在选择"综艺"分类后显示相应的动态字段', async () => { |
| 161 | render( |
| 162 | <MemoryRouter> |
| 163 | <UploadTorrent /> |
| 164 | </MemoryRouter> |
| 165 | ); |
| 166 | |
| 167 | // 等待分类数据加载 |
| 168 | await waitFor(() => { |
| 169 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 170 | }); |
| 171 | |
| 172 | // 选择"综艺"分类 |
| 173 | const categorySelect = screen.getByLabelText('分类'); |
| 174 | fireEvent.mouseDown(categorySelect); |
| 175 | fireEvent.click(screen.getByText('综艺')); |
| 176 | |
| 177 | // 验证综艺相关的字段是否显示 |
| 178 | expect(screen.getByLabelText('是否大陆综艺')).toBeInTheDocument(); |
| 179 | expect(screen.getByLabelText('类型')).toBeInTheDocument(); |
| 180 | expect(screen.getByLabelText('格式')).toBeInTheDocument(); |
| 181 | }); |
| 182 | |
| 183 | it('应该在选择"动漫"分类后显示相应的动态字段', async () => { |
| 184 | render( |
| 185 | <MemoryRouter> |
| 186 | <UploadTorrent /> |
| 187 | </MemoryRouter> |
| 188 | ); |
| 189 | |
| 190 | // 等待分类数据加载 |
| 191 | await waitFor(() => { |
| 192 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 193 | }); |
| 194 | |
| 195 | // 选择"动漫"分类 |
| 196 | const categorySelect = screen.getByLabelText('分类'); |
| 197 | fireEvent.mouseDown(categorySelect); |
| 198 | fireEvent.click(screen.getByText('动漫')); |
| 199 | |
| 200 | // 验证动漫相关的字段是否显示 |
| 201 | expect(screen.getByLabelText('类型')).toBeInTheDocument(); |
| 202 | expect(screen.getByLabelText('格式')).toBeInTheDocument(); |
| 203 | expect(screen.getByLabelText('分辨率')).toBeInTheDocument(); |
| 204 | }); |
| 205 | |
| 206 | it('应该在选择"学习资料"分类后显示相应的动态字段', async () => { |
| 207 | render( |
| 208 | <MemoryRouter> |
| 209 | <UploadTorrent /> |
| 210 | </MemoryRouter> |
| 211 | ); |
| 212 | |
| 213 | // 等待分类数据加载 |
| 214 | await waitFor(() => { |
| 215 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 216 | }); |
| 217 | |
| 218 | // 选择"学习资料"分类 |
| 219 | const categorySelect = screen.getByLabelText('分类'); |
| 220 | fireEvent.mouseDown(categorySelect); |
| 221 | fireEvent.click(screen.getByText('学习资料')); |
| 222 | |
| 223 | // 验证学习资料相关的字段是否显示 |
| 224 | expect(screen.getByLabelText('类型')).toBeInTheDocument(); |
| 225 | expect(screen.getByLabelText('格式')).toBeInTheDocument(); |
| 226 | }); |
| 227 | |
| 228 | it('应该在选择"纪录片"分类后显示相应的动态字段', async () => { |
| 229 | render( |
| 230 | <MemoryRouter> |
| 231 | <UploadTorrent /> |
| 232 | </MemoryRouter> |
| 233 | ); |
| 234 | |
| 235 | // 等待分类数据加载 |
| 236 | await waitFor(() => { |
| 237 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 238 | }); |
| 239 | |
| 240 | // 选择"纪录片"分类 |
| 241 | const categorySelect = screen.getByLabelText('分类'); |
| 242 | fireEvent.mouseDown(categorySelect); |
| 243 | fireEvent.click(screen.getByText('纪录片')); |
| 244 | |
| 245 | // 验证纪录片相关的字段是否显示 |
| 246 | expect(screen.getByLabelText('年份')).toBeInTheDocument(); |
| 247 | expect(screen.getByLabelText('视频源')).toBeInTheDocument(); |
| 248 | expect(screen.getByLabelText('格式')).toBeInTheDocument(); |
| 249 | }); |
| 250 | |
| 251 | it('应该在选择"音乐"分类后显示相应的动态字段', async () => { |
| 252 | render( |
| 253 | <MemoryRouter> |
| 254 | <UploadTorrent /> |
| 255 | </MemoryRouter> |
| 256 | ); |
| 257 | |
| 258 | // 等待分类数据加载 |
| 259 | await waitFor(() => { |
| 260 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 261 | }); |
| 262 | |
| 263 | // 选择"音乐"分类 |
| 264 | const categorySelect = screen.getByLabelText('分类'); |
| 265 | fireEvent.mouseDown(categorySelect); |
| 266 | fireEvent.click(screen.getByText('音乐')); |
| 267 | |
| 268 | // 验证音乐相关的字段是否显示 |
| 269 | expect(screen.getByLabelText('类型')).toBeInTheDocument(); |
| 270 | expect(screen.getByLabelText('地区')).toBeInTheDocument(); |
| 271 | expect(screen.getByLabelText('风格')).toBeInTheDocument(); |
| 272 | expect(screen.getByLabelText('格式')).toBeInTheDocument(); |
| 273 | }); |
| 274 | |
| 275 | it('应该在选择"软件"分类后显示相应的动态字段', async () => { |
| 276 | render( |
| 277 | <MemoryRouter> |
| 278 | <UploadTorrent /> |
| 279 | </MemoryRouter> |
| 280 | ); |
| 281 | |
| 282 | // 等待分类数据加载 |
| 283 | await waitFor(() => { |
| 284 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 285 | }); |
| 286 | |
| 287 | // 选择"软件"分类 |
| 288 | const categorySelect = screen.getByLabelText('分类'); |
| 289 | fireEvent.mouseDown(categorySelect); |
| 290 | fireEvent.click(screen.getByText('软件')); |
| 291 | |
| 292 | // 验证软件相关的字段是否显示 |
| 293 | expect(screen.getByLabelText('平台')).toBeInTheDocument(); |
| 294 | expect(screen.getByLabelText('类型')).toBeInTheDocument(); |
| 295 | expect(screen.getByLabelText('格式')).toBeInTheDocument(); |
| 296 | }); |
| 297 | |
| 298 | it('应该在选择"体育"分类后显示相应的动态字段', async () => { |
| 299 | render( |
| 300 | <MemoryRouter> |
| 301 | <UploadTorrent /> |
| 302 | </MemoryRouter> |
| 303 | ); |
| 304 | |
| 305 | // 等待分类数据加载 |
| 306 | await waitFor(() => { |
| 307 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 308 | }); |
| 309 | |
| 310 | // 选择"体育"分类 |
| 311 | const categorySelect = screen.getByLabelText('分类'); |
| 312 | fireEvent.mouseDown(categorySelect); |
| 313 | fireEvent.click(screen.getByText('体育')); |
| 314 | |
| 315 | // 验证体育相关的字段是否显示 |
| 316 | expect(screen.getByLabelText('类型')).toBeInTheDocument(); |
| 317 | expect(screen.getByLabelText('格式')).toBeInTheDocument(); |
| 318 | expect(screen.getByLabelText('赛事类型')).toBeInTheDocument(); |
| 319 | }); |
| 320 | |
| 321 | it('应该在选择分类后正确设置状态', async () => { |
| 322 | render( |
| 323 | <MemoryRouter> |
| 324 | <UploadTorrent /> |
| 325 | </MemoryRouter> |
| 326 | ); |
| 327 | |
| 328 | // 等待分类数据加载 |
| 329 | await waitFor(() => { |
| 330 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 331 | }); |
| 332 | |
| 333 | // 选择"电影"分类 |
| 334 | const categorySelect = screen.getByLabelText('分类'); |
| 335 | fireEvent.mouseDown(categorySelect); |
| 336 | fireEvent.click(screen.getByText('电影')); |
| 337 | |
| 338 | // 验证状态是否正确设置 |
| 339 | // 注意:由于状态是组件内部的,我们需要通过 UI 变化来间接验证 |
| 340 | // 这里我们验证电影相关的字段是否显示 |
| 341 | expect(screen.getByLabelText('字幕/说明')).toBeInTheDocument(); |
| 342 | }); |
| 343 | |
| 344 | it('应该在选择种子文件后更新状态', async () => { |
| 345 | render( |
| 346 | <MemoryRouter> |
| 347 | <UploadTorrent /> |
| 348 | </MemoryRouter> |
| 349 | ); |
| 350 | |
| 351 | // 等待分类数据加载 |
| 352 | await waitFor(() => { |
| 353 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 354 | }); |
| 355 | |
| 356 | // 模拟文件选择 |
| 357 | const fileInput = screen.getByLabelText('种子文件').querySelector('input'); |
| 358 | const mockFile = new File(['torrent content'], 'test.torrent', { type: 'application/x-bittorrent' }); |
| 359 | fireEvent.change(fileInput, { target: { files: [mockFile] } }); |
| 360 | |
| 361 | // 验证文件是否被设置(通过 UI 变化间接验证) |
| 362 | // 由于我们无法直接访问组件状态,我们可以通过表单验证状态来间接验证 |
| 363 | // 这里我们只是验证文件输入是否正常工作 |
| 364 | expect(fileInput).toBeInTheDocument(); |
| 365 | }); |
| 366 | |
| 367 | it('应该在选择封面图片后更新状态', async () => { |
| 368 | render( |
| 369 | <MemoryRouter> |
| 370 | <UploadTorrent /> |
| 371 | </MemoryRouter> |
| 372 | ); |
| 373 | |
| 374 | // 等待分类数据加载 |
| 375 | await waitFor(() => { |
| 376 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 377 | }); |
| 378 | |
| 379 | // 模拟图片选择 |
| 380 | const imageInput = screen.getByLabelText('封面图片').querySelector('input'); |
| 381 | const mockImage = new File(['image content'], 'test.jpg', { type: 'image/jpeg' }); |
| 382 | fireEvent.change(imageInput, { target: { files: [mockImage] } }); |
| 383 | |
| 384 | // 验证图片是否被设置(通过 UI 变化间接验证) |
| 385 | expect(imageInput).toBeInTheDocument(); |
| 386 | }); |
| 387 | |
| 388 | it('应该在表单提交时验证必填字段', async () => { |
| 389 | render( |
| 390 | <MemoryRouter> |
| 391 | <UploadTorrent /> |
| 392 | </MemoryRouter> |
| 393 | ); |
| 394 | |
| 395 | // 等待分类数据加载 |
| 396 | await waitFor(() => { |
| 397 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 398 | }); |
| 399 | |
| 400 | // 直接提交表单而不填写任何字段 |
| 401 | const submitButton = screen.getByText('上传种子'); |
| 402 | fireEvent.click(submitButton); |
| 403 | |
| 404 | // 验证错误消息是否显示 |
| 405 | // 注意:由于我们使用了 antd 的 Form,错误消息可能由 antd 内部处理 |
| 406 | // 我们可以通过检查是否有错误提示来验证 |
| 407 | // 这里我们只是验证表单提交被调用 |
| 408 | // 更详细的验证可能需要更复杂的测试设置 |
| 409 | }); |
| 410 | |
| 411 | it('应该在填写所有必填字段后成功提交表单', async () => { |
| 412 | // 模拟 axios.post |
| 413 | vi.mocked(axios.post).mockResolvedValueOnce({ data: {} }); |
| 414 | |
| 415 | render( |
| 416 | <MemoryRouter> |
| 417 | <UploadTorrent /> |
| 418 | </MemoryRouter> |
| 419 | ); |
| 420 | |
| 421 | // 等待分类数据加载 |
| 422 | await waitFor(() => { |
| 423 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/categories'); |
| 424 | }); |
| 425 | |
| 426 | // 选择分类 |
| 427 | const categorySelect = screen.getByLabelText('分类'); |
| 428 | fireEvent.mouseDown(categorySelect); |
| 429 | fireEvent.click(screen.getByText('电影')); |
| 430 | |
| 431 | // 填写标题 |
| 432 | const titleInput = screen.getByLabelText('标题'); |
| 433 | fireEvent.change(titleInput, { target: { value: '测试电影' } }); |
| 434 | |
| 435 | // 填写描述 |
| 436 | const descriptionInput = screen.getByLabelText('描述'); |
| 437 | fireEvent.change(descriptionInput, { target: { value: '这是一个测试电影的描述' } }); |
| 438 | |
| 439 | // 选择封面图片 |
| 440 | const imageInput = screen.getByLabelText('封面图片').querySelector('input'); |
| 441 | const mockImage = new File(['image content'], 'test.jpg', { type: 'image/jpeg' }); |
| 442 | fireEvent.change(imageInput, { target: { files: [mockImage] } }); |
| 443 | |
| 444 | // 选择种子文件 |
| 445 | const fileInput = screen.getByLabelText('种子文件').querySelector('input'); |
| 446 | const mockFile = new File(['torrent content'], 'test.torrent', { type: 'application/x-bittorrent' }); |
| 447 | fireEvent.change(fileInput, { target: { files: [mockFile] } }); |
| 448 | |
| 449 | // 填写电影相关字段 |
| 450 | const captionInput = screen.getByLabelText('字幕/说明'); |
| 451 | fireEvent.change(captionInput, { target: { value: '测试字幕' } }); |
| 452 | |
| 453 | const regionSelect = screen.getByLabelText('地区'); |
| 454 | fireEvent.mouseDown(regionSelect); |
| 455 | fireEvent.click(screen.getByText('大陆')); |
| 456 | |
| 457 | const yearInput = screen.getByLabelText('年份'); |
| 458 | fireEvent.change(yearInput, { target: { value: '2023' } }); |
| 459 | |
| 460 | const genreSelect = screen.getByLabelText('类型'); |
| 461 | fireEvent.mouseDown(genreSelect); |
| 462 | fireEvent.click(screen.getByText('动作')); |
| 463 | |
| 464 | const codecFormatSelect = screen.getByLabelText('编码格式'); |
| 465 | fireEvent.mouseDown(codecFormatSelect); |
| 466 | fireEvent.click(screen.getByText('H.264')); |
| 467 | |
| 468 | const resolutionSelect = screen.getByLabelText('分辨率'); |
| 469 | fireEvent.mouseDown(resolutionSelect); |
| 470 | fireEvent.click(screen.getByText('1080p')); |
| 471 | |
| 472 | // 提交表单 |
| 473 | const submitButton = screen.getByText('上传种子'); |
| 474 | fireEvent.click(submitButton); |
| 475 | |
| 476 | // 验证 axios.post 是否被调用 |
| 477 | expect(axios.post).toHaveBeenCalledWith( |
| 478 | 'http://localhost:8080/torrent/upload', |
| 479 | expect.any(FormData), |
| 480 | { |
| 481 | headers: { 'Content-Type': 'multipart/form-data' }, |
| 482 | responseType: 'blob', |
| 483 | } |
| 484 | ); |
| 485 | |
| 486 | // 验证成功消息是否显示 |
| 487 | expect(vi.mocked(message.success)).toHaveBeenCalledWith('上传成功!'); |
| 488 | }); |
| 489 | }); |