blob: b445b4d526d61331994781c6e2842205167cf80f [file] [log] [blame]
Akane121765b61a72025-05-17 13:52:25 +08001import MockAdapter from 'axios-mock-adapter';
2import { api } from './auth'; // 添加api导入
Akane1217d1e9f712025-05-29 14:36:56 +08003import { createPost, getPosts, getPostDetail, likePost, addPostComment,deletePost } from './helpPost';
Akane121765b61a72025-05-17 13:52:25 +08004
5describe('求助帖API', () => {
6 let mockAxios;
7
8 beforeEach(() => {
9 mockAxios = new MockAdapter(api);
10 });
11
12 afterEach(() => {
13 mockAxios.restore();
14 });
15
16 describe('createPost - 创建求助帖', () => {
Akane1217d1e9f712025-05-29 14:36:56 +080017 it('应该正确发送无图片帖子数据', async () => {
Akane121765b61a72025-05-17 13:52:25 +080018 const postData = {
19 title: '测试标题',
20 content: '测试内容',
21 authorId: 'user123'
22 };
Akane1217d1e9f712025-05-29 14:36:56 +080023 // 使用函数匹配器来验证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 });
Akane121765b61a72025-05-17 13:52:25 +080032
33 const response = await createPost(postData.title, postData.content, postData.authorId);
34 expect(response.status).toBe(201);
35 });
36 });
Akane1217d1e9f712025-05-29 14:36:56 +080037 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
51 const response = await createPost(
52 postData.title,
53 postData.content,
54 postData.authorId,
55 postData.selectedImage
56 );
57 expect(response.status).toBe(201);
58 });
59
Akane121765b61a72025-05-17 13:52:25 +080060
61 describe('getPosts - 获取求助帖列表', () => {
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
69 const response = await getPosts(page, size);
70 expect(response.status).toBe(200);
71 });
72 });
73
74 describe('likePost - 点赞求助帖', () => {
75 it('应该正确发送点赞请求', async () => {
76 mockAxios.onPost('/help/posts/post123/like').reply(200, { code: 200 });
77 const response = await likePost('post123');
78 expect(response.status).toBe(200);
79 });
80 });
81
82 describe('addPostComment - 添加帖子评论', () => {
Akane1217d1e9f712025-05-29 14:36:56 +080083 it('应该正确发送评论数据(无图片)', async () => {
84 const postId = 'post456';
85 const commentData = {
86 authorId: 'user1',
87 content: '测试评论'
88 };
Akane121765b61a72025-05-17 13:52:25 +080089
Akane1217d1e9f712025-05-29 14:36:56 +080090 mockAxios.onPost(`/help/posts/${postId}/comments`).reply(config => {
91 const data = config.data;
92 expect(data.get('authorId')).toBe(commentData.authorId);
93 expect(data.get('content')).toBe(commentData.content);
94 expect(data.has('image')).toBe(false);
95 return [200, { code: 200 }];
96 });
97
98 const response = await addPostComment('post456', commentData);
99 expect(response.status).toBe(200);
100 });
101 it('应该正确处理带图片的评论', async () => {
102 const postId = 'post456';
103 const commentData = {
104 authorId: 'user1',
105 content: '测试评论',
106 commentImage: new File(['content'], 'comment.jpg')
107 };
108
109 mockAxios.onPost(`/help/posts/${postId}/comments`).reply(config => {
110 const data = config.data;
111 expect(data.get('image')).toBeInstanceOf(File);
112 return [200, { code: 200 }];
113 });
114
115 const response = await addPostComment(postId, commentData);
116 expect(response.status).toBe(200);
117 });
118 });
119 describe('deletePost - 删除帖子', () => {
120 it('应该正确发送删除请求', async () => {
121 const postId = 'post789';
122 const authorId = 'user1';
123
124 mockAxios.onDelete(`/help/posts/${postId}`).reply(config => {
125 expect(config.params).toEqual({ authorId });
126 return [200, { code: 200 }];
127 });
128
129 const response = await deletePost(postId, authorId);
Akane121765b61a72025-05-17 13:52:25 +0800130 expect(response.status).toBe(200);
131 });
132 });
133});