meisiyu | 1d4aade | 2025-06-02 20:10:36 +0800 | [diff] [blame] | 1 | /** |
| 2 | * 帖子服务单元测试 |
| 3 | * |
| 4 | * 测试覆盖所有帖子相关的API接口 |
| 5 | * 包括:帖子列表、详情、评论、收藏、点赞、发布、审核、举报等功能 |
| 6 | */ |
| 7 | |
| 8 | import * as postService from '../../src/services/post'; |
| 9 | |
| 10 | // Mock request module |
| 11 | jest.mock('@umijs/max', () => ({ |
| 12 | request: jest.fn(), |
| 13 | })); |
| 14 | |
| 15 | const { request } = require('@umijs/max'); |
| 16 | |
| 17 | describe('Post Service', () => { |
| 18 | beforeEach(() => { |
| 19 | jest.clearAllMocks(); |
| 20 | }); |
| 21 | |
| 22 | // 帖子列表相关测试 |
| 23 | describe('getPostList', () => { |
| 24 | it('应该成功获取帖子列表', async () => { |
| 25 | const mockResponse = { |
| 26 | code: 200, |
| 27 | rows: [ |
| 28 | { |
| 29 | postId: 1, |
| 30 | title: '测试帖子', |
| 31 | content: '测试内容', |
| 32 | author: 'testuser', |
| 33 | views: 100, |
| 34 | comments: 5, |
| 35 | favorites: 10, |
| 36 | likes: 15, |
| 37 | tags: 'tag1,tag2', |
| 38 | publishTime: '2024-01-01 12:00:00', |
| 39 | status: '1' |
| 40 | } |
| 41 | ], |
| 42 | total: 1 |
| 43 | }; |
| 44 | |
| 45 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 46 | |
| 47 | const result = await postService.getPostList({ pageNum: 1, pageSize: 10 }); |
| 48 | |
| 49 | expect(result).toEqual(mockResponse); |
| 50 | expect(request).toHaveBeenCalledWith('/api/post-center/list', { |
| 51 | method: 'GET', |
| 52 | params: { pageNum: 1, pageSize: 10 }, |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | it('应该处理带标签筛选的帖子列表请求', async () => { |
| 57 | const mockResponse = { code: 200, rows: [], total: 0 }; |
| 58 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 59 | |
| 60 | await postService.getPostList({ pageNum: 1, pageSize: 10, tags: '日剧' }); |
| 61 | |
| 62 | expect(request).toHaveBeenCalledWith('/api/post-center/list', { |
| 63 | method: 'GET', |
| 64 | params: { pageNum: 1, pageSize: 10, tags: '日剧' }, |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | it('应该处理搜索关键词的请求', async () => { |
| 69 | const mockResponse = { code: 200, rows: [], total: 0 }; |
| 70 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 71 | |
| 72 | await postService.getPostList({ pageNum: 1, pageSize: 10, title: '搜索关键词' }); |
| 73 | |
| 74 | expect(request).toHaveBeenCalledWith('/api/post-center/list', { |
| 75 | method: 'GET', |
| 76 | params: { pageNum: 1, pageSize: 10, title: '搜索关键词' }, |
| 77 | }); |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | // 帖子详情相关测试 |
| 82 | describe('getPostDetail', () => { |
| 83 | it('应该成功获取帖子详情', async () => { |
| 84 | const mockResponse = { |
| 85 | code: 200, |
| 86 | data: { |
| 87 | post: { |
| 88 | postId: 1, |
| 89 | title: '测试帖子', |
| 90 | content: '测试内容', |
| 91 | author: 'testuser', |
| 92 | views: 100, |
| 93 | comments: 5, |
| 94 | favorites: 10, |
| 95 | likes: 15, |
| 96 | tags: 'tag1,tag2', |
| 97 | publishTime: '2024-01-01 12:00:00', |
| 98 | status: '1' |
| 99 | }, |
| 100 | tags: [], |
| 101 | comments: [], |
| 102 | recommendedPosts: [], |
| 103 | favorited: false |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 108 | |
| 109 | const result = await postService.getPostDetail(1); |
| 110 | |
| 111 | expect(result).toEqual(mockResponse); |
| 112 | expect(request).toHaveBeenCalledWith('/api/post-center/1', { |
| 113 | method: 'GET', |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | it('应该处理获取不存在帖子的情况', async () => { |
| 118 | const mockResponse = { code: 404, msg: '帖子不存在' }; |
| 119 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 120 | |
| 121 | const result = await postService.getPostDetail(999); |
| 122 | |
| 123 | expect(result).toEqual(mockResponse); |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | // 评论相关测试 |
| 128 | describe('addComment', () => { |
| 129 | it('应该成功添加评论', async () => { |
| 130 | const mockResponse = { code: 200, msg: '评论成功', data: { commentId: 1 } }; |
| 131 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 132 | |
| 133 | const commentData = { |
| 134 | postId: 1, |
| 135 | content: '这是一条测试评论', |
| 136 | parentId: 0 |
| 137 | }; |
| 138 | |
| 139 | const result = await postService.addComment(commentData); |
| 140 | |
| 141 | expect(result).toEqual(mockResponse); |
| 142 | expect(request).toHaveBeenCalledWith('/api/post-center/comment', { |
| 143 | method: 'POST', |
| 144 | data: commentData, |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | it('应该处理空评论内容', async () => { |
| 149 | const mockResponse = { code: 400, msg: '评论内容不能为空' }; |
| 150 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 151 | |
| 152 | const result = await postService.addComment({ postId: 1, content: '' }); |
| 153 | |
| 154 | expect(result).toEqual(mockResponse); |
| 155 | }); |
| 156 | }); |
| 157 | |
| 158 | // 收藏相关测试 |
| 159 | describe('toggleFavorite', () => { |
| 160 | it('应该成功收藏帖子', async () => { |
| 161 | const mockResponse = { code: 200, msg: '收藏成功' }; |
| 162 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 163 | |
| 164 | const result = await postService.toggleFavorite(1, true); |
| 165 | |
| 166 | expect(result).toEqual(mockResponse); |
| 167 | expect(request).toHaveBeenCalledWith('/api/post-center/favorite/1', { |
| 168 | method: 'POST', |
| 169 | params: { favorite: true }, |
| 170 | }); |
| 171 | }); |
| 172 | |
| 173 | it('应该成功取消收藏帖子', async () => { |
| 174 | const mockResponse = { code: 200, msg: '取消收藏成功' }; |
| 175 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 176 | |
| 177 | const result = await postService.toggleFavorite(1, false); |
| 178 | |
| 179 | expect(result).toEqual(mockResponse); |
| 180 | expect(request).toHaveBeenCalledWith('/api/post-center/favorite/1', { |
| 181 | method: 'POST', |
| 182 | params: { favorite: false }, |
| 183 | }); |
| 184 | }); |
| 185 | }); |
| 186 | |
| 187 | // 点赞相关测试 |
| 188 | describe('toggleLike', () => { |
| 189 | it('应该成功点赞帖子', async () => { |
| 190 | const mockResponse = { code: 200, msg: '点赞成功' }; |
| 191 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 192 | |
| 193 | const result = await postService.toggleLike(1, true); |
| 194 | |
| 195 | expect(result).toEqual(mockResponse); |
| 196 | expect(request).toHaveBeenCalledWith('/api/post-center/like/1', { |
| 197 | method: 'POST', |
| 198 | params: { like: true }, |
| 199 | }); |
| 200 | }); |
| 201 | |
| 202 | it('应该成功取消点赞帖子', async () => { |
| 203 | const mockResponse = { code: 200, msg: '取消点赞成功' }; |
| 204 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 205 | |
| 206 | const result = await postService.toggleLike(1, false); |
| 207 | |
| 208 | expect(result).toEqual(mockResponse); |
| 209 | }); |
| 210 | }); |
| 211 | |
| 212 | // 评论点赞测试 |
| 213 | describe('toggleCommentLike', () => { |
| 214 | it('应该成功点赞评论', async () => { |
| 215 | const mockResponse = { code: 200, msg: '点赞成功' }; |
| 216 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 217 | |
| 218 | const result = await postService.toggleCommentLike(1, true); |
| 219 | |
| 220 | expect(result).toEqual(mockResponse); |
| 221 | expect(request).toHaveBeenCalledWith('/api/post-center/comment/like/1', { |
| 222 | method: 'POST', |
| 223 | params: { like: true }, |
| 224 | }); |
| 225 | }); |
| 226 | }); |
| 227 | |
| 228 | // 发布帖子测试 |
| 229 | describe('publishPost', () => { |
| 230 | it('应该成功发布帖子', async () => { |
| 231 | const mockResponse = { code: 200, msg: '发布成功' }; |
| 232 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 233 | |
| 234 | const postData = { |
| 235 | title: '新帖子标题', |
| 236 | content: '新帖子内容', |
| 237 | summary: '新帖子摘要', |
| 238 | tags: 'tag1,tag2', |
| 239 | promotionPlan: 1 |
| 240 | }; |
| 241 | |
| 242 | const result = await postService.publishPost(postData); |
| 243 | |
| 244 | expect(result).toEqual(mockResponse); |
| 245 | expect(request).toHaveBeenCalledWith('/api/post-center/publish', { |
| 246 | method: 'POST', |
| 247 | data: postData, |
| 248 | }); |
| 249 | }); |
| 250 | |
| 251 | it('应该处理发布帖子失败的情况', async () => { |
| 252 | const mockResponse = { code: 400, msg: '标题不能为空' }; |
| 253 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 254 | |
| 255 | const result = await postService.publishPost({ |
| 256 | title: '', |
| 257 | content: '内容', |
| 258 | summary: '摘要', |
| 259 | tags: 'tag1' |
| 260 | }); |
| 261 | |
| 262 | expect(result).toEqual(mockResponse); |
| 263 | }); |
| 264 | }); |
| 265 | |
| 266 | // 个人帖子列表测试 |
| 267 | describe('getMyPosts', () => { |
| 268 | it('应该成功获取我的帖子列表', async () => { |
| 269 | const mockResponse = { |
| 270 | code: 200, |
| 271 | rows: [ |
| 272 | { |
| 273 | postId: 1, |
| 274 | title: '我的帖子', |
| 275 | author: 'currentUser', |
| 276 | status: '1' |
| 277 | } |
| 278 | ], |
| 279 | total: 1 |
| 280 | }; |
| 281 | |
| 282 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 283 | |
| 284 | const result = await postService.getMyPosts({ pageNum: 1, pageSize: 10 }); |
| 285 | |
| 286 | expect(result).toEqual(mockResponse); |
| 287 | expect(request).toHaveBeenCalledWith('/api/post-center/my-posts', { |
| 288 | method: 'GET', |
| 289 | params: { pageNum: 1, pageSize: 10 }, |
| 290 | }); |
| 291 | }); |
| 292 | }); |
| 293 | |
| 294 | // 收藏列表测试 |
| 295 | describe('getMyFavorites', () => { |
| 296 | it('应该成功获取我的收藏列表', async () => { |
| 297 | const mockResponse = { |
| 298 | code: 200, |
| 299 | rows: [ |
| 300 | { |
| 301 | postId: 1, |
| 302 | title: '收藏的帖子', |
| 303 | author: 'otherUser' |
| 304 | } |
| 305 | ], |
| 306 | total: 1 |
| 307 | }; |
| 308 | |
| 309 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 310 | |
| 311 | const result = await postService.getMyFavorites({ pageNum: 1, pageSize: 10 }); |
| 312 | |
| 313 | expect(result).toEqual(mockResponse); |
| 314 | expect(request).toHaveBeenCalledWith('/api/post-center/my-favorites', { |
| 315 | method: 'GET', |
| 316 | params: { pageNum: 1, pageSize: 10 }, |
| 317 | }); |
| 318 | }); |
| 319 | }); |
| 320 | |
| 321 | // 更新帖子测试 |
| 322 | describe('updatePost', () => { |
| 323 | it('应该成功更新帖子', async () => { |
| 324 | const postData: API.Post.PostInfo = { |
| 325 | postId: 1, |
| 326 | title: '更新的标题', |
| 327 | content: '更新的内容', |
| 328 | author: 'testuser', |
| 329 | views: 0, |
| 330 | comments: 0, |
| 331 | favorites: 0, |
| 332 | likes: 0, |
| 333 | tags: 'tag1,tag2', |
| 334 | publishTime: new Date().toISOString(), |
| 335 | status: '1' |
| 336 | }; |
| 337 | |
| 338 | const mockResponse = { code: 200, msg: '更新成功', data: null }; |
| 339 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 340 | |
| 341 | const result = await postService.updatePost(postData); |
| 342 | |
| 343 | expect(result).toEqual(mockResponse); |
| 344 | expect(request).toHaveBeenCalledWith('/api/post-center/update', { |
| 345 | method: 'PUT', |
| 346 | data: postData, |
| 347 | }); |
| 348 | }); |
| 349 | }); |
| 350 | |
| 351 | // 删除帖子测试 |
| 352 | describe('deletePost', () => { |
| 353 | it('应该成功删除帖子', async () => { |
| 354 | const mockResponse = { code: 200, msg: '删除成功' }; |
| 355 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 356 | |
| 357 | const result = await postService.deletePost(1); |
| 358 | |
| 359 | expect(result).toEqual(mockResponse); |
| 360 | expect(request).toHaveBeenCalledWith('/api/post-center/delete/1', { |
| 361 | method: 'DELETE', |
| 362 | }); |
| 363 | }); |
| 364 | }); |
| 365 | |
| 366 | // 标签相关测试 |
| 367 | describe('tag operations', () => { |
| 368 | it('应该成功获取热门标签', async () => { |
| 369 | const mockResponse = { |
| 370 | code: 200, |
| 371 | data: [ |
| 372 | { tagId: 1, tagName: '日剧', postCount: 10 }, |
| 373 | { tagId: 2, tagName: '电影', postCount: 8 } |
| 374 | ] |
| 375 | }; |
| 376 | |
| 377 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 378 | |
| 379 | const result = await postService.getHotTags(); |
| 380 | |
| 381 | expect(result).toEqual(mockResponse); |
| 382 | expect(request).toHaveBeenCalledWith('/api/post-center/tags/hot', { |
| 383 | method: 'GET', |
| 384 | }); |
| 385 | }); |
| 386 | |
| 387 | it('应该成功获取可用标签', async () => { |
| 388 | const mockResponse = { |
| 389 | code: 200, |
| 390 | data: [ |
| 391 | { tagId: 1, tagName: '日剧', tagColor: 'blue' }, |
| 392 | { tagId: 2, tagName: '电影', tagColor: 'green' } |
| 393 | ] |
| 394 | }; |
| 395 | |
| 396 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 397 | |
| 398 | const result = await postService.getAvailableTags(); |
| 399 | |
| 400 | expect(result).toEqual(mockResponse); |
| 401 | expect(request).toHaveBeenCalledWith('/api/post-center/tags/available', { |
| 402 | method: 'GET', |
| 403 | }); |
| 404 | }); |
| 405 | |
| 406 | it('应该成功根据标签获取帖子', async () => { |
| 407 | const mockResponse = { |
| 408 | code: 200, |
| 409 | rows: [{ postId: 1, title: '日剧帖子' }], |
| 410 | total: 1 |
| 411 | }; |
| 412 | |
| 413 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 414 | |
| 415 | const result = await postService.getPostsByTag(1); |
| 416 | |
| 417 | expect(result).toEqual(mockResponse); |
| 418 | expect(request).toHaveBeenCalledWith('/api/post-center/bytag/1', { |
| 419 | method: 'GET', |
| 420 | }); |
| 421 | }); |
| 422 | }); |
| 423 | |
| 424 | // 图片上传测试 |
| 425 | describe('image operations', () => { |
| 426 | it('应该成功上传图片', async () => { |
| 427 | const mockResponse = { |
| 428 | code: 200, |
| 429 | data: { |
| 430 | url: '/images/123456_test.jpg', |
| 431 | filename: '123456_test.jpg', |
| 432 | originalName: 'test.jpg', |
| 433 | size: '1024' |
| 434 | } |
| 435 | }; |
| 436 | |
| 437 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 438 | |
| 439 | const formData = new FormData(); |
| 440 | formData.append('file', new Blob(['test'], { type: 'image/jpeg' })); |
| 441 | |
| 442 | const result = await postService.uploadImage(formData); |
| 443 | |
| 444 | expect(result).toEqual(mockResponse); |
| 445 | expect(request).toHaveBeenCalledWith('/api/post-center/upload', { |
| 446 | method: 'POST', |
| 447 | data: formData, |
| 448 | }); |
| 449 | }); |
| 450 | |
| 451 | it('应该成功删除图片', async () => { |
| 452 | const mockResponse = { code: 200, msg: '删除成功' }; |
| 453 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 454 | |
| 455 | const result = await postService.deleteImage('test.jpg'); |
| 456 | |
| 457 | expect(result).toEqual(mockResponse); |
| 458 | expect(request).toHaveBeenCalledWith('/api/post-center/upload', { |
| 459 | method: 'DELETE', |
| 460 | params: { filename: 'test.jpg' }, |
| 461 | }); |
| 462 | }); |
| 463 | }); |
| 464 | |
| 465 | // 推广相关测试 |
| 466 | describe('promotion operations', () => { |
| 467 | it('应该成功获取推广帖子', async () => { |
| 468 | const mockResponse = { |
| 469 | code: 200, |
| 470 | data: [ |
| 471 | { |
| 472 | postId: 1, |
| 473 | title: '推广帖子', |
| 474 | promotionPlanId: 1 |
| 475 | } |
| 476 | ] |
| 477 | }; |
| 478 | |
| 479 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 480 | |
| 481 | const result = await postService.getPromotionPosts(); |
| 482 | |
| 483 | expect(result).toEqual(mockResponse); |
| 484 | expect(request).toHaveBeenCalledWith('/api/post-center/promotion', { |
| 485 | method: 'GET', |
| 486 | }); |
| 487 | }); |
| 488 | |
| 489 | it('应该成功获取推广计划', async () => { |
| 490 | const mockResponse = { |
| 491 | code: 200, |
| 492 | data: [ |
| 493 | { |
| 494 | id: 1, |
| 495 | name: '首页推荐', |
| 496 | description: '帖子显示在首页推荐位置', |
| 497 | price: 50.00, |
| 498 | duration: 7 |
| 499 | } |
| 500 | ] |
| 501 | }; |
| 502 | |
| 503 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 504 | |
| 505 | const result = await postService.getPromotionPlans(); |
| 506 | |
| 507 | expect(result).toEqual(mockResponse); |
| 508 | expect(request).toHaveBeenCalledWith('/api/post-center/promotion-plans', { |
| 509 | method: 'GET', |
| 510 | }); |
| 511 | }); |
| 512 | |
| 513 | it('应该成功创建支付记录', async () => { |
| 514 | const mockResponse = { |
| 515 | code: 200, |
| 516 | data: { |
| 517 | paymentId: 1, |
| 518 | postId: 1, |
| 519 | planId: 1, |
| 520 | amount: 50.00, |
| 521 | paymentStatus: 'pending' |
| 522 | } |
| 523 | }; |
| 524 | |
| 525 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 526 | |
| 527 | const paymentData = { |
| 528 | postId: 1, |
| 529 | planId: 1, |
| 530 | amount: 50.00 |
| 531 | }; |
| 532 | |
| 533 | const result = await postService.createPayment(paymentData); |
| 534 | |
| 535 | expect(result).toEqual(mockResponse); |
| 536 | expect(request).toHaveBeenCalledWith('/api/post-center/payment', { |
| 537 | method: 'POST', |
| 538 | data: paymentData, |
| 539 | }); |
| 540 | }); |
| 541 | |
| 542 | it('应该成功获取推广状态', async () => { |
| 543 | const mockResponse = { |
| 544 | code: 200, |
| 545 | data: { |
| 546 | hasPromotion: true, |
| 547 | promotionPlanId: 1 |
| 548 | } |
| 549 | }; |
| 550 | |
| 551 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 552 | |
| 553 | const result = await postService.getPromotionStatus(1); |
| 554 | |
| 555 | expect(result).toEqual(mockResponse); |
| 556 | expect(request).toHaveBeenCalledWith('/api/post-center/promotion-status/1', { |
| 557 | method: 'GET', |
| 558 | }); |
| 559 | }); |
| 560 | |
| 561 | it('应该成功确认支付', async () => { |
| 562 | const mockResponse = { code: 200, msg: '支付成功' }; |
| 563 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 564 | |
| 565 | const result = await postService.confirmPayment(1); |
| 566 | |
| 567 | expect(result).toEqual(mockResponse); |
| 568 | expect(request).toHaveBeenCalledWith('/api/post-center/payment/confirm/1', { |
| 569 | method: 'POST', |
| 570 | }); |
| 571 | }); |
| 572 | |
| 573 | it('应该成功取消支付', async () => { |
| 574 | const mockResponse = { code: 200, msg: '支付已取消' }; |
| 575 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 576 | |
| 577 | const result = await postService.cancelPayment(1); |
| 578 | |
| 579 | expect(result).toEqual(mockResponse); |
| 580 | expect(request).toHaveBeenCalledWith('/api/post-center/payment/cancel/1', { |
| 581 | method: 'POST', |
| 582 | }); |
| 583 | }); |
| 584 | }); |
| 585 | |
meisiyu | d83081c | 2025-06-05 22:25:36 +0800 | [diff] [blame^] | 586 | // 新推广支付流程测试 |
| 587 | describe('new promotion payment flow', () => { |
| 588 | it('应该成功执行新推广流程:发布帖子 -> 创建支付 -> 确认支付', async () => { |
| 589 | // 1. 先发布帖子 |
| 590 | const publishResponse = { |
| 591 | code: 200, |
| 592 | data: { postId: 123, message: '帖子发布成功' } |
| 593 | }; |
| 594 | (request as jest.Mock).mockResolvedValueOnce(publishResponse); |
| 595 | |
| 596 | const postData = { |
| 597 | title: '推广帖子标题', |
| 598 | content: '推广帖子内容', |
| 599 | summary: '推广帖子摘要', |
| 600 | tags: 'tag1,tag2' |
| 601 | }; |
| 602 | |
| 603 | const publishResult = await postService.publishPost(postData); |
| 604 | expect(publishResult).toEqual(publishResponse); |
| 605 | |
| 606 | // 2. 创建支付记录(使用真实的帖子ID) |
| 607 | const paymentResponse = { |
| 608 | code: 200, |
| 609 | data: { |
| 610 | paymentId: 1, |
| 611 | postId: 123, // 真实的帖子ID |
| 612 | planId: 1, |
| 613 | amount: 50.00, |
| 614 | paymentStatus: 'pending' |
| 615 | } |
| 616 | }; |
| 617 | (request as jest.Mock).mockResolvedValueOnce(paymentResponse); |
| 618 | |
| 619 | const paymentData = { |
| 620 | postId: 123, // 使用发布帖子返回的ID |
| 621 | planId: 1, |
| 622 | amount: 50.00 |
| 623 | }; |
| 624 | |
| 625 | const paymentResult = await postService.createPayment(paymentData); |
| 626 | expect(paymentResult).toEqual(paymentResponse); |
| 627 | |
| 628 | // 3. 确认支付 |
| 629 | const confirmResponse = { code: 200, msg: '支付成功,推广已生效' }; |
| 630 | (request as jest.Mock).mockResolvedValueOnce(confirmResponse); |
| 631 | |
| 632 | const confirmResult = await postService.confirmPayment(1); |
| 633 | expect(confirmResult).toEqual(confirmResponse); |
| 634 | |
| 635 | // 验证API调用顺序和参数 |
| 636 | expect(request).toHaveBeenCalledTimes(3); |
| 637 | expect(request).toHaveBeenNthCalledWith(1, '/api/post-center/publish', { |
| 638 | method: 'POST', |
| 639 | data: postData, |
| 640 | }); |
| 641 | expect(request).toHaveBeenNthCalledWith(2, '/api/post-center/payment', { |
| 642 | method: 'POST', |
| 643 | data: paymentData, |
| 644 | }); |
| 645 | expect(request).toHaveBeenNthCalledWith(3, '/api/post-center/payment/confirm/1', { |
| 646 | method: 'POST', |
| 647 | }); |
| 648 | }); |
| 649 | |
| 650 | it('应该处理支付失败后重新尝试支付的场景', async () => { |
| 651 | // 第一次支付失败,第二次应该复用已创建的帖子 |
| 652 | const paymentData = { |
| 653 | postId: 123, // 复用已创建的帖子ID |
| 654 | planId: 1, |
| 655 | amount: 50.00 |
| 656 | }; |
| 657 | |
| 658 | // 第一次支付失败 |
| 659 | const failedPaymentResponse = { code: 400, msg: '支付处理失败' }; |
| 660 | (request as jest.Mock).mockResolvedValueOnce(failedPaymentResponse); |
| 661 | |
| 662 | let result = await postService.createPayment(paymentData); |
| 663 | expect(result).toEqual(failedPaymentResponse); |
| 664 | |
| 665 | // 第二次支付成功 |
| 666 | const successPaymentResponse = { |
| 667 | code: 200, |
| 668 | data: { |
| 669 | paymentId: 2, |
| 670 | postId: 123, |
| 671 | planId: 1, |
| 672 | amount: 50.00, |
| 673 | paymentStatus: 'pending' |
| 674 | } |
| 675 | }; |
| 676 | (request as jest.Mock).mockResolvedValueOnce(successPaymentResponse); |
| 677 | |
| 678 | result = await postService.createPayment(paymentData); |
| 679 | expect(result).toEqual(successPaymentResponse); |
| 680 | |
| 681 | // 验证使用的是同一个帖子ID |
| 682 | expect(request).toHaveBeenCalledTimes(2); |
| 683 | expect(request).toHaveBeenNthCalledWith(1, '/api/post-center/payment', { |
| 684 | method: 'POST', |
| 685 | data: paymentData, |
| 686 | }); |
| 687 | expect(request).toHaveBeenNthCalledWith(2, '/api/post-center/payment', { |
| 688 | method: 'POST', |
| 689 | data: paymentData, |
| 690 | }); |
| 691 | }); |
| 692 | |
| 693 | it('应该成功删除取消发布的草稿帖子', async () => { |
| 694 | const deleteResponse = { code: 200, msg: '草稿帖子删除成功' }; |
| 695 | (request as jest.Mock).mockResolvedValue(deleteResponse); |
| 696 | |
| 697 | const result = await postService.deletePost(123); |
| 698 | expect(result).toEqual(deleteResponse); |
| 699 | |
| 700 | expect(request).toHaveBeenCalledWith('/api/post-center/delete/123', { |
| 701 | method: 'DELETE', |
| 702 | }); |
| 703 | }); |
| 704 | |
| 705 | it('应该处理发布帖子成功但创建支付失败的情况', async () => { |
| 706 | // 1. 发布帖子成功 |
| 707 | const publishResponse = { |
| 708 | code: 200, |
| 709 | data: { postId: 123, message: '帖子发布成功' } |
| 710 | }; |
| 711 | (request as jest.Mock).mockResolvedValueOnce(publishResponse); |
| 712 | |
| 713 | const postData = { |
| 714 | title: '推广帖子标题', |
| 715 | content: '推广帖子内容', |
| 716 | summary: '推广帖子摘要', |
| 717 | tags: 'tag1,tag2' |
| 718 | }; |
| 719 | |
| 720 | await postService.publishPost(postData); |
| 721 | |
| 722 | // 2. 创建支付失败 |
| 723 | const paymentResponse = { code: 400, msg: '该帖子已购买推广服务' }; |
| 724 | (request as jest.Mock).mockResolvedValueOnce(paymentResponse); |
| 725 | |
| 726 | const paymentData = { |
| 727 | postId: 123, |
| 728 | planId: 1, |
| 729 | amount: 50.00 |
| 730 | }; |
| 731 | |
| 732 | const paymentResult = await postService.createPayment(paymentData); |
| 733 | expect(paymentResult).toEqual(paymentResponse); |
| 734 | }); |
| 735 | |
| 736 | it('应该处理发布帖子失败的情况', async () => { |
| 737 | const publishResponse = { code: 400, msg: '帖子标题不能为空' }; |
| 738 | (request as jest.Mock).mockResolvedValue(publishResponse); |
| 739 | |
| 740 | const postData = { |
| 741 | title: '', |
| 742 | content: '内容', |
| 743 | summary: '摘要', |
| 744 | tags: 'tag1' |
| 745 | }; |
| 746 | |
| 747 | const result = await postService.publishPost(postData); |
| 748 | expect(result).toEqual(publishResponse); |
| 749 | }); |
| 750 | |
| 751 | it('应该处理支付确认失败的情况', async () => { |
| 752 | const confirmResponse = { code: 400, msg: '支付记录中的帖子ID无效' }; |
| 753 | (request as jest.Mock).mockResolvedValue(confirmResponse); |
| 754 | |
| 755 | const result = await postService.confirmPayment(1); |
| 756 | expect(result).toEqual(confirmResponse); |
| 757 | }); |
| 758 | |
| 759 | it('应该成功取消支付并返回发布页面', async () => { |
| 760 | const cancelResponse = { code: 200, msg: '支付已取消' }; |
| 761 | (request as jest.Mock).mockResolvedValue(cancelResponse); |
| 762 | |
| 763 | const result = await postService.cancelPayment(1); |
| 764 | expect(result).toEqual(cancelResponse); |
| 765 | }); |
| 766 | |
| 767 | it('应该处理重复购买推广的情况', async () => { |
| 768 | const paymentResponse = { code: 400, msg: '该帖子已购买推广服务' }; |
| 769 | (request as jest.Mock).mockResolvedValue(paymentResponse); |
| 770 | |
| 771 | const paymentData = { |
| 772 | postId: 123, |
| 773 | planId: 1, |
| 774 | amount: 50.00 |
| 775 | }; |
| 776 | |
| 777 | const result = await postService.createPayment(paymentData); |
| 778 | expect(result).toEqual(paymentResponse); |
| 779 | }); |
| 780 | }); |
| 781 | |
meisiyu | 1d4aade | 2025-06-02 20:10:36 +0800 | [diff] [blame] | 782 | // 举报相关测试 |
| 783 | describe('report operations', () => { |
| 784 | it('应该成功举报帖子', async () => { |
| 785 | const mockResponse = { code: 200, msg: '举报提交成功' }; |
| 786 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 787 | |
| 788 | const result = await postService.reportPost(1, '内容不当'); |
| 789 | |
| 790 | expect(result).toEqual(mockResponse); |
| 791 | expect(request).toHaveBeenCalledWith('/api/post-center/report/1', { |
| 792 | method: 'POST', |
| 793 | data: { reason: '内容不当' }, |
| 794 | }); |
| 795 | }); |
| 796 | }); |
| 797 | |
| 798 | // 管理员功能测试 |
| 799 | describe('admin operations', () => { |
| 800 | it('应该成功获取待审核帖子', async () => { |
| 801 | const mockResponse = { |
| 802 | code: 200, |
| 803 | rows: [ |
| 804 | { |
| 805 | postId: 1, |
| 806 | title: '待审核帖子', |
| 807 | status: '0' |
| 808 | } |
| 809 | ], |
| 810 | total: 1 |
| 811 | }; |
| 812 | |
| 813 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 814 | |
| 815 | const result = await postService.getReviewPosts({ status: '0' }); |
| 816 | |
| 817 | expect(result).toEqual(mockResponse); |
| 818 | expect(request).toHaveBeenCalledWith('/api/post/review/list', { |
| 819 | method: 'GET', |
| 820 | params: { status: '0' }, |
| 821 | }); |
| 822 | }); |
| 823 | |
| 824 | it('应该成功审核帖子', async () => { |
| 825 | const mockResponse = { code: 200, msg: '审核成功' }; |
| 826 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 827 | |
| 828 | const result = await postService.reviewPost(1, 'approve', '内容合规'); |
| 829 | |
| 830 | expect(result).toEqual(mockResponse); |
| 831 | expect(request).toHaveBeenCalledWith('/api/post/review/1', { |
| 832 | method: 'PUT', |
| 833 | data: { action: 'approve', reason: '内容合规' }, |
| 834 | }); |
| 835 | }); |
| 836 | |
| 837 | it('应该成功下架帖子', async () => { |
| 838 | const mockResponse = { code: 200, msg: '下架成功' }; |
| 839 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 840 | |
| 841 | const result = await postService.takeDownPost(1, '违规内容'); |
| 842 | |
| 843 | expect(result).toEqual(mockResponse); |
| 844 | expect(request).toHaveBeenCalledWith('/api/post/takedown/1', { |
| 845 | method: 'PUT', |
| 846 | data: { reason: '违规内容' }, |
| 847 | }); |
| 848 | }); |
| 849 | |
| 850 | it('应该成功获取举报列表', async () => { |
| 851 | const mockResponse = { |
| 852 | code: 200, |
| 853 | rows: [ |
| 854 | { |
| 855 | reportId: 1, |
| 856 | postId: 1, |
| 857 | reportReason: '内容不当', |
| 858 | status: '0' |
| 859 | } |
| 860 | ], |
| 861 | total: 1 |
| 862 | }; |
| 863 | |
| 864 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 865 | |
| 866 | const result = await postService.getReportList({}); |
| 867 | |
| 868 | expect(result).toEqual(mockResponse); |
| 869 | expect(request).toHaveBeenCalledWith('/api/post/report/list', { |
| 870 | method: 'GET', |
| 871 | params: {}, |
| 872 | }); |
| 873 | }); |
| 874 | |
| 875 | it('应该成功处理举报', async () => { |
| 876 | const mockResponse = { code: 200, msg: '举报处理成功' }; |
| 877 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 878 | |
| 879 | const result = await postService.handleReport(1, 'approve', 1, '举报属实'); |
| 880 | |
| 881 | expect(result).toEqual(mockResponse); |
| 882 | expect(request).toHaveBeenCalledWith('/api/post/report/handle/1', { |
| 883 | method: 'PUT', |
| 884 | data: { action: 'approve', postId: 1, reason: '举报属实' }, |
| 885 | }); |
| 886 | }); |
| 887 | }); |
| 888 | |
| 889 | // 错误处理测试 |
| 890 | describe('error handling', () => { |
| 891 | it('应该处理网络错误', async () => { |
| 892 | const error = new Error('Network Error'); |
| 893 | (request as jest.Mock).mockRejectedValue(error); |
| 894 | |
| 895 | await expect(postService.getPostList({})).rejects.toThrow('Network Error'); |
| 896 | }); |
| 897 | |
| 898 | it('应该处理服务器错误响应', async () => { |
| 899 | const mockResponse = { code: 500, msg: '服务器内部错误' }; |
| 900 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 901 | |
| 902 | const result = await postService.getPostList({}); |
| 903 | |
| 904 | expect(result).toEqual(mockResponse); |
| 905 | }); |
| 906 | }); |
| 907 | }); |