DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 1 | import MockAdapter from 'axios-mock-adapter'; |
| 2 | import { api } from './auth'; |
| 3 | import { |
| 4 | createRequestPost, |
| 5 | getRequestPosts, |
| 6 | getRequestPostDetail, |
| 7 | likeRequestPost, |
| 8 | addRequestPostComment, |
| 9 | deleteRequestPost, |
| 10 | searchRequestPosts |
| 11 | } from './requestPost'; |
| 12 | |
| 13 | describe('求种帖API', () => { |
| 14 | let mockAxios; |
| 15 | |
| 16 | beforeEach(() => { |
| 17 | mockAxios = new MockAdapter(api); |
| 18 | }); |
| 19 | |
| 20 | afterEach(() => { |
| 21 | mockAxios.restore(); |
| 22 | }); |
| 23 | |
| 24 | describe('createRequestPost - 创建求种帖', () => { |
| 25 | it('应该正确发送无图片帖子数据', async () => { |
| 26 | const postData = { |
| 27 | title: '求种测试标题', |
| 28 | content: '求种测试内容', |
| 29 | authorId: 'user123' |
| 30 | }; |
| 31 | |
| 32 | mockAxios.onPost('/request/posts').reply(config => { |
| 33 | const data = config.data; |
| 34 | expect(data.get('title')).toBe(postData.title); |
| 35 | expect(data.get('content')).toBe(postData.content); |
| 36 | expect(data.get('authorId')).toBe(postData.authorId); |
| 37 | expect(data.has('image')).toBe(false); |
| 38 | return [201, { code: 201 }]; |
| 39 | }); |
| 40 | |
| 41 | const response = await createRequestPost( |
| 42 | postData.title, |
| 43 | postData.content, |
| 44 | postData.authorId |
| 45 | ); |
| 46 | expect(response.status).toBe(201); |
| 47 | }); |
| 48 | |
| 49 | it('应该正确处理带图片的帖子', async () => { |
| 50 | const postData = { |
| 51 | title: '求种测试标题', |
| 52 | content: '求种测试内容', |
| 53 | authorId: 'user123', |
| 54 | selectedImage: new File(['content'], 'request.jpg') |
| 55 | }; |
| 56 | |
| 57 | mockAxios.onPost('/request/posts').reply(config => { |
| 58 | const data = config.data; |
| 59 | expect(data.get('image')).toBeInstanceOf(File); |
| 60 | return [201, { code: 201 }]; |
| 61 | }); |
| 62 | |
| 63 | const response = await createRequestPost( |
| 64 | postData.title, |
| 65 | postData.content, |
| 66 | postData.authorId, |
| 67 | postData.selectedImage |
| 68 | ); |
| 69 | expect(response.status).toBe(201); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | describe('getRequestPosts - 获取求种帖列表', () => { |
| 74 | it('应该支持分页参数', async () => { |
| 75 | const page = 2, size = 10; |
| 76 | mockAxios.onGet('/request/posts', { params: { page, size } }) |
| 77 | .reply(200, { |
| 78 | code: 200, |
| 79 | data: { |
| 80 | records: [], |
| 81 | total: 0 |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | const response = await getRequestPosts(page, size); |
| 86 | expect(response.status).toBe(200); |
| 87 | expect(response.data.data).toBeDefined(); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | describe('getRequestPostDetail - 获取求种帖详情', () => { |
| 92 | it('应该正确获取帖子详情', async () => { |
| 93 | const postId = 'req123'; |
| 94 | mockAxios.onGet(`/request/posts/${postId}`) |
| 95 | .reply(200, { |
| 96 | code: 200, |
| 97 | data: { |
| 98 | post: { |
| 99 | id: postId, |
| 100 | title: '测试求种帖' |
| 101 | } |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | const response = await getRequestPostDetail(postId); |
| 106 | expect(response.status).toBe(200); |
| 107 | expect(response.data.data.post.id).toBe(postId); |
| 108 | }); |
| 109 | }); |
| 110 | |
| 111 | |
| 112 | |
| 113 | describe('deleteRequestPost - 删除求种帖', () => { |
| 114 | it('应该正确发送删除请求', async () => { |
| 115 | const postId = 'req101'; |
| 116 | const authorId = 'user1'; |
| 117 | |
| 118 | mockAxios.onDelete(`/request/posts/${postId}`, { |
| 119 | params: { authorId } |
| 120 | }).reply(200, { code: 200 }); |
| 121 | |
| 122 | const response = await deleteRequestPost(postId, authorId); |
| 123 | expect(response.status).toBe(200); |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | describe('searchRequestPosts - 搜索求种帖', () => { |
| 128 | it('应该正确发送搜索请求', async () => { |
| 129 | const keyword = '测试'; |
| 130 | const page = 1, size = 5; |
| 131 | |
| 132 | mockAxios.onGet('/request/posts/search', { |
| 133 | params: { keyword, page, size } |
| 134 | }).reply(200, { |
| 135 | code: 200, |
| 136 | data: { |
| 137 | records: [], |
| 138 | total: 0 |
| 139 | } |
| 140 | }); |
| 141 | |
| 142 | const response = await searchRequestPosts(keyword, page, size); |
| 143 | expect(response.status).toBe(200); |
| 144 | expect(response.data.data).toBeDefined(); |
| 145 | }); |
| 146 | }); |
| 147 | }); |