Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | import MockAdapter from 'axios-mock-adapter';
|
| 2 | import { api } from './auth'; // 添加api导入
|
| 3 | import { createPost, getPosts, getPostDetail, likePost, addPostComment } from './helpPost';
|
| 4 |
|
| 5 | describe('求助帖API', () => {
|
| 6 | let mockAxios;
|
| 7 |
|
| 8 | beforeEach(() => {
|
| 9 | mockAxios = new MockAdapter(api);
|
| 10 | });
|
| 11 |
|
| 12 | afterEach(() => {
|
| 13 | mockAxios.restore();
|
| 14 | });
|
| 15 |
|
| 16 | describe('createPost - 创建求助帖', () => {
|
| 17 | it('应该正确发送帖子数据', async () => {
|
| 18 | const postData = {
|
| 19 | title: '测试标题',
|
| 20 | content: '测试内容',
|
| 21 | authorId: 'user123'
|
| 22 | };
|
| 23 | mockAxios.onPost('/help/posts', postData).reply(201, { code: 201 });
|
| 24 |
|
| 25 | const response = await createPost(postData.title, postData.content, postData.authorId);
|
| 26 | expect(response.status).toBe(201);
|
| 27 | });
|
| 28 | });
|
| 29 |
|
| 30 | describe('getPosts - 获取求助帖列表', () => {
|
| 31 | it('应该支持分页参数', async () => {
|
| 32 | const page = 2, size = 10;
|
| 33 | mockAxios.onGet('/help/posts', { params: { page, size } }).reply(200, {
|
| 34 | code: 200,
|
| 35 | data: []
|
| 36 | });
|
| 37 |
|
| 38 | const response = await getPosts(page, size);
|
| 39 | expect(response.status).toBe(200);
|
| 40 | });
|
| 41 | });
|
| 42 |
|
| 43 | describe('likePost - 点赞求助帖', () => {
|
| 44 | it('应该正确发送点赞请求', async () => {
|
| 45 | mockAxios.onPost('/help/posts/post123/like').reply(200, { code: 200 });
|
| 46 | const response = await likePost('post123');
|
| 47 | expect(response.status).toBe(200);
|
| 48 | });
|
| 49 | });
|
| 50 |
|
| 51 | describe('addPostComment - 添加帖子评论', () => {
|
| 52 | it('应该正确发送评论数据', async () => {
|
| 53 | const comment = { content: '测试评论', author: 'user1' };
|
| 54 | mockAxios.onPost('/help/posts/post456/comments', comment).reply(200, { code: 200 });
|
| 55 |
|
| 56 | const response = await addPostComment('post456', comment);
|
| 57 | expect(response.status).toBe(200);
|
| 58 | });
|
| 59 | });
|
| 60 | }); |