崔向南 | 4a9c416 | 2025-06-05 18:39:29 +0800 | [diff] [blame^] | 1 | import * as bountyService from '../../src/services/bounty/bounty'; |
| 2 | |
| 3 | // Mock request module |
| 4 | jest.mock('@umijs/max', () => ({ |
| 5 | request: jest.fn(), |
| 6 | })); |
| 7 | |
| 8 | const { request } = require('@umijs/max'); |
| 9 | |
| 10 | describe('Bounty Service', () => { |
| 11 | beforeEach(() => { |
| 12 | jest.clearAllMocks(); |
| 13 | }); |
| 14 | |
| 15 | // ✅ 悬赏列表相关测试 |
| 16 | describe('getBountyList', () => { |
| 17 | it('应该成功获取悬赏列表', async () => { |
| 18 | const mockResponse = { |
| 19 | code: 200, |
| 20 | data: { |
| 21 | records: [ |
| 22 | { |
| 23 | id: 1, |
| 24 | title: '测试悬赏', |
| 25 | creator_id: 1, |
| 26 | reward: 100, |
| 27 | deadline: '2023-12-31', |
| 28 | status: 0, |
| 29 | submissionsCount: 0 |
| 30 | } |
| 31 | ], |
| 32 | total: 1 |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 37 | |
| 38 | const result = await bountyService.getBountyList({ pageNum: 1, pageSize: 10 }); |
| 39 | |
| 40 | expect(result).toEqual(mockResponse); |
| 41 | expect(request).toHaveBeenCalledWith('/api/bounties', { |
| 42 | method: 'GET', |
| 43 | headers: { |
| 44 | 'Content-Type': 'application/json;charset=UTF-8', |
| 45 | }, |
| 46 | params: { pageNum: 1, pageSize: 10 }, |
| 47 | }); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | |
| 52 | // ✅ 发布悬赏测试 |
| 53 | describe('publishBounty', () => { |
| 54 | it('应该成功发布悬赏', async () => { |
| 55 | const mockResponse = { |
| 56 | code: 200, |
| 57 | msg: '发布成功' |
| 58 | }; |
| 59 | |
| 60 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 61 | |
| 62 | const postData = { |
| 63 | title: '新悬赏标题', |
| 64 | description: '新悬赏描述', |
| 65 | reward: 100, |
| 66 | deadline: '2023-12-31T23:59:59' |
| 67 | }; |
| 68 | |
| 69 | const result = await bountyService.publishBounty(postData); |
| 70 | |
| 71 | expect(result).toEqual(mockResponse); |
| 72 | expect(request).toHaveBeenCalledWith('/api/bounties/publish', { |
| 73 | method: 'POST', |
| 74 | headers: { |
| 75 | 'Content-Type': 'application/json;charset=UTF-8', |
| 76 | }, |
| 77 | data: { |
| 78 | ...postData, |
| 79 | status: 0 |
| 80 | }, |
| 81 | }); |
| 82 | }); |
| 83 | }); |
| 84 | |
| 85 | // ✅ 提交回复测试 |
| 86 | describe('submitBountyReply', () => { |
| 87 | it('应该成功提交回复', async () => { |
| 88 | const mockResponse = { |
| 89 | code: 200, |
| 90 | data: { |
| 91 | id: 1 |
| 92 | }, |
| 93 | msg: '操作成功' |
| 94 | }; |
| 95 | |
| 96 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 97 | |
| 98 | const params = { |
| 99 | bountyId: 1, |
| 100 | content: '这是一条回复内容', |
| 101 | file: new File(['test'], 'test.txt') as File |
| 102 | }; |
| 103 | |
| 104 | const result = await bountyService.submitBountyReply(params); |
| 105 | |
| 106 | expect(result).toEqual(mockResponse); |
| 107 | expect(request).toHaveBeenCalledWith('/api/bounty-submissions', { |
| 108 | method: 'POST', |
| 109 | data: expect.any(FormData) |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | it('应该处理空回复内容', async () => { |
| 114 | const mockResponse = { |
| 115 | code: 400, |
| 116 | data: { |
| 117 | msg: '内容不能为空' |
| 118 | }, |
| 119 | msg: '操作成功' |
| 120 | }; |
| 121 | |
| 122 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 123 | |
| 124 | const result = await bountyService.submitBountyReply({ |
| 125 | bountyId: 1, |
| 126 | content: '' |
| 127 | }); |
| 128 | |
| 129 | expect(result).toEqual(mockResponse); |
| 130 | }); |
| 131 | }); |
| 132 | |
| 133 | // ✅ 错误处理测试 |
| 134 | describe('error handling', () => { |
| 135 | it('应该处理网络错误', async () => { |
| 136 | const error = new Error('Network Error'); |
| 137 | (request as jest.Mock).mockRejectedValue(error); |
| 138 | |
| 139 | await expect(bountyService.getBountyList()).rejects.toThrow('Network Error'); |
| 140 | }); |
| 141 | |
| 142 | it('应该处理服务器错误响应', async () => { |
| 143 | const mockResponse = { code: 500, msg: '服务器内部错误' }; |
| 144 | (request as jest.Mock).mockResolvedValue(mockResponse); |
| 145 | |
| 146 | const result = await bountyService.getBountyList(); |
| 147 | |
| 148 | expect(result).toEqual(mockResponse); |
| 149 | }); |
| 150 | }); |
| 151 | }); |