Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | import MockAdapter from 'axios-mock-adapter';
|
| 2 | import { api } from './auth'; // 添加api导入
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 3 | import { createHelpPost, getHelpPosts, getHelpPostDetail, likeHelpPost, addHelpPostComment,deleteHelpPost,searchHelpPosts } from './helpPost';
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 4 |
|
| 5 | describe('求助帖API', () => {
|
| 6 | let mockAxios;
|
| 7 |
|
| 8 | beforeEach(() => {
|
| 9 | mockAxios = new MockAdapter(api);
|
| 10 | });
|
| 11 |
|
| 12 | afterEach(() => {
|
| 13 | mockAxios.restore();
|
| 14 | });
|
| 15 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 16 | describe('createHelpPost - 创建求助帖', () => {
|
Akane1217 | d1e9f71 | 2025-05-29 14:36:56 +0800 | [diff] [blame] | 17 | it('应该正确发送无图片帖子数据', async () => {
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 18 | const postData = {
|
| 19 | title: '测试标题',
|
| 20 | content: '测试内容',
|
| 21 | authorId: 'user123'
|
| 22 | };
|
Akane1217 | d1e9f71 | 2025-05-29 14:36:56 +0800 | [diff] [blame] | 23 | // 使用函数匹配器来验证FormData内容
|
| 24 | mockAxios.onPost('/help/posts').reply(config => {
|
| 25 | const data = config.data;
|
| 26 | expect(data.get('title')).toBe(postData.title);
|
| 27 | expect(data.get('content')).toBe(postData.content);
|
| 28 | expect(data.get('authorId')).toBe(postData.authorId);
|
| 29 | expect(data.has('image')).toBe(false);
|
| 30 | return [201, { code: 201 }];
|
| 31 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 32 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 33 | const response = await createHelpPost(postData.title, postData.content, postData.authorId);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 34 | expect(response.status).toBe(201);
|
| 35 | });
|
| 36 | });
|
Akane1217 | d1e9f71 | 2025-05-29 14:36:56 +0800 | [diff] [blame] | 37 | it('应该正确处理带图片的帖子', async () => {
|
| 38 | const postData = {
|
| 39 | title: '测试标题',
|
| 40 | content: '测试内容',
|
| 41 | authorId: 'user123',
|
| 42 | selectedImage: new File(['content'], 'test.jpg')
|
| 43 | };
|
| 44 |
|
| 45 | mockAxios.onPost('/help/posts').reply(config => {
|
| 46 | const data = config.data;
|
| 47 | expect(data.get('image')).toBeInstanceOf(File);
|
| 48 | return [201, { code: 201 }];
|
| 49 | });
|
| 50 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 51 | const response = await createHelpPost(
|
Akane1217 | d1e9f71 | 2025-05-29 14:36:56 +0800 | [diff] [blame] | 52 | postData.title,
|
| 53 | postData.content,
|
| 54 | postData.authorId,
|
| 55 | postData.selectedImage
|
| 56 | );
|
| 57 | expect(response.status).toBe(201);
|
| 58 | });
|
| 59 |
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 60 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 61 | describe('getHelpPosts - 获取求助帖列表', () => {
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 62 | it('应该支持分页参数', async () => {
|
| 63 | const page = 2, size = 10;
|
| 64 | mockAxios.onGet('/help/posts', { params: { page, size } }).reply(200, {
|
| 65 | code: 200,
|
| 66 | data: []
|
| 67 | });
|
| 68 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 69 | const response = await getHelpPosts(page, size);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 70 | expect(response.status).toBe(200);
|
| 71 | });
|
| 72 | });
|
| 73 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 74 | describe('likeHelpPost - 点赞求助帖', () => {
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 75 | it('应该正确发送点赞请求', async () => {
|
| 76 | mockAxios.onPost('/help/posts/post123/like').reply(200, { code: 200 });
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 77 | const response = await likeHelpPost('post123');
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 78 | expect(response.status).toBe(200);
|
| 79 | });
|
| 80 | });
|
| 81 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 82 |
|
| 83 | describe('deleteHelpPost - 删除帖子', () => {
|
Akane1217 | d1e9f71 | 2025-05-29 14:36:56 +0800 | [diff] [blame] | 84 | it('应该正确发送删除请求', async () => {
|
| 85 | const postId = 'post789';
|
| 86 | const authorId = 'user1';
|
| 87 |
|
| 88 | mockAxios.onDelete(`/help/posts/${postId}`).reply(config => {
|
| 89 | expect(config.params).toEqual({ authorId });
|
| 90 | return [200, { code: 200 }];
|
| 91 | });
|
| 92 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 93 | const response = await deleteHelpPost(postId, authorId);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 94 | expect(response.status).toBe(200);
|
| 95 | });
|
| 96 | });
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 97 | describe('searchHelpPosts - 搜索求助帖', () => {
|
| 98 | it('应该正确发送搜索请求', async () => {
|
| 99 | const keyword = '测试';
|
| 100 | const page = 1, size = 5;
|
| 101 |
|
| 102 | mockAxios.onGet('/help/posts/search', {
|
| 103 | params: { keyword, page, size }
|
| 104 | }).reply(200, {
|
| 105 | code: 200,
|
| 106 | data: {
|
| 107 | records: [],
|
| 108 | total: 0
|
| 109 | }
|
| 110 | });
|
| 111 |
|
| 112 | const response = await searchHelpPosts(keyword, page, size);
|
| 113 | expect(response.status).toBe(200);
|
| 114 | expect(response.data.data).toBeDefined();
|
| 115 | });
|
| 116 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 117 | }); |