合并
Change-Id: I19ca58c58a513cba20c162c9ed7cbab90e060bf6
diff --git a/src/api/helpComment.js b/src/api/helpComment.js
index e711587..c1d00c0 100644
--- a/src/api/helpComment.js
+++ b/src/api/helpComment.js
@@ -9,5 +9,20 @@
};
export const addCommentReply = (commentId, replyData) => {
- return api.post(`/help/comments/${commentId}/replies`, replyData);
+ const formData = new FormData();
+ formData.append('authorId', replyData.authorId);
+ formData.append('content', replyData.content);
+
+ // 如果有图片,添加到formData
+ if (replyData.image) {
+ formData.append('image', replyData.image);
+ }
+
+ return api.post(`/help/comments/${commentId}/replies`, formData);
+};
+
+export const deleteComment = (commentId, authorId) => {
+ return api.delete(`/help/comments/${commentId}`, {
+ params: { authorId }
+ });
};
\ No newline at end of file
diff --git a/src/api/helpComment.test.js b/src/api/helpComment.test.js
index 2b7be4c..a4a7d99 100644
--- a/src/api/helpComment.test.js
+++ b/src/api/helpComment.test.js
@@ -1,6 +1,6 @@
import MockAdapter from 'axios-mock-adapter';
import { api } from './auth'; // 添加api导入
-import { likePostComment, getCommentReplies, addCommentReply } from './helpComment';
+import { likePostComment, getCommentReplies, addCommentReply, deleteComment } from './helpComment';
describe('求助帖评论API', () => {
let mockAxios;
@@ -34,11 +34,53 @@
});
describe('addCommentReply - 添加评论回复', () => {
- it('应该正确发送回复内容', async () => {
- const testData = { content: '测试回复', author: 'user1' };
- mockAxios.onPost('/help/comments/789/replies', testData).reply(200, { code: 200 });
+ it('应该正确发送回复内容(无图片)', async () => {
+ const commentId = '789';
+ const replyData = {
+ authorId: 'user1',
+ content: '测试回复'
+ };
+
+ mockAxios.onPost(`/help/comments/${commentId}/replies`).reply(config => {
+ const data = config.data;
+ expect(data.get('authorId')).toBe(replyData.authorId);
+ expect(data.get('content')).toBe(replyData.content);
+ expect(data.has('image')).toBe(false);
+ return [200, { code: 200 }];
+ });
- const response = await addCommentReply('789', testData);
+ const response = await addCommentReply(commentId, replyData);
+ expect(response.status).toBe(200);
+ });
+ it('应该正确处理带图片的回复', async () => {
+ const commentId = '789';
+ const replyData = {
+ authorId: 'user1',
+ content: '测试回复',
+ image: new File(['content'], 'reply.jpg')
+ };
+
+ mockAxios.onPost(`/help/comments/${commentId}/replies`).reply(config => {
+ const data = config.data;
+ expect(data.get('image')).toBeInstanceOf(File);
+ return [200, { code: 200 }];
+ });
+
+ const response = await addCommentReply(commentId, replyData);
+ expect(response.status).toBe(200);
+ });
+ });
+ describe('deleteComment - 删除评论', () => {
+ it('应该正确发送删除请求', async () => {
+ const commentId = '101112';
+ const authorId = 'user1';
+
+ mockAxios.onDelete(`/help/comments/${commentId}`).reply(config => {
+ expect(config.params).toEqual({ authorId });
+ return [200, { code: 200 }];
+ });
+
+ const response = await deleteComment(commentId, authorId);
expect(response.status).toBe(200);
});
});
diff --git a/src/api/helpPost.js b/src/api/helpPost.js
index 426e92b..4813aa3 100644
--- a/src/api/helpPost.js
+++ b/src/api/helpPost.js
@@ -1,12 +1,19 @@
// src/api/helpPost.js
import { api } from './auth'; // 复用已有的axios实例
-export const createPost = (title, content, authorId) => {
- return api.post('/help/posts', {
- title,
- content,
- authorId
- });
+export const createPost = (title, content, authorId, selectedImage) => {
+ // 创建 FormData 对象
+ const formData = new FormData();
+ formData.append('title', title);
+ formData.append('content', content);
+ formData.append('authorId', authorId);
+
+ // 如果有图片,添加到 FormData
+ if (selectedImage) {
+ formData.append('image', selectedImage);
+ }
+
+ return api.post('/help/posts', formData);
};
export const getPosts = (page = 1, size = 5) => {
@@ -19,10 +26,28 @@
return api.get(`/help/posts/${postId}`);
};
-export const likePost = (postId) => {
- return api.post(`/help/posts/${postId}/like`);
+export const likePost = (postId, data) => {
+ return api.post(`/help/posts/${postId}/like`, null, {
+ params: data
+ });
};
export const addPostComment = (postId, commentData) => {
- return api.post(`/help/posts/${postId}/comments`, commentData);
+ // 创建FormData对象来处理文件上传
+ const formData = new FormData();
+ formData.append('authorId', commentData.authorId);
+ formData.append('content', commentData.content);
+
+ // 如果有图片,添加到formData
+ if (commentData.commentImage) {
+ formData.append('image', commentData.commentImage);
+ }
+
+ return api.post(`/help/posts/${postId}/comments`, formData);
+};
+
+export const deletePost = (postId, authorId) => {
+ return api.delete(`/help/posts/${postId}`, {
+ params: { authorId }
+ });
};
\ No newline at end of file
diff --git a/src/api/helpPost.test.js b/src/api/helpPost.test.js
index e163090..b445b4d 100644
--- a/src/api/helpPost.test.js
+++ b/src/api/helpPost.test.js
@@ -1,6 +1,6 @@
import MockAdapter from 'axios-mock-adapter';
import { api } from './auth'; // 添加api导入
-import { createPost, getPosts, getPostDetail, likePost, addPostComment } from './helpPost';
+import { createPost, getPosts, getPostDetail, likePost, addPostComment,deletePost } from './helpPost';
describe('求助帖API', () => {
let mockAxios;
@@ -14,18 +14,49 @@
});
describe('createPost - 创建求助帖', () => {
- it('应该正确发送帖子数据', async () => {
+ it('应该正确发送无图片帖子数据', async () => {
const postData = {
title: '测试标题',
content: '测试内容',
authorId: 'user123'
};
- mockAxios.onPost('/help/posts', postData).reply(201, { code: 201 });
+ // 使用函数匹配器来验证FormData内容
+ mockAxios.onPost('/help/posts').reply(config => {
+ const data = config.data;
+ expect(data.get('title')).toBe(postData.title);
+ expect(data.get('content')).toBe(postData.content);
+ expect(data.get('authorId')).toBe(postData.authorId);
+ expect(data.has('image')).toBe(false);
+ return [201, { code: 201 }];
+ });
const response = await createPost(postData.title, postData.content, postData.authorId);
expect(response.status).toBe(201);
});
});
+ it('应该正确处理带图片的帖子', async () => {
+ const postData = {
+ title: '测试标题',
+ content: '测试内容',
+ authorId: 'user123',
+ selectedImage: new File(['content'], 'test.jpg')
+ };
+
+ mockAxios.onPost('/help/posts').reply(config => {
+ const data = config.data;
+ expect(data.get('image')).toBeInstanceOf(File);
+ return [201, { code: 201 }];
+ });
+
+ const response = await createPost(
+ postData.title,
+ postData.content,
+ postData.authorId,
+ postData.selectedImage
+ );
+ expect(response.status).toBe(201);
+ });
+
describe('getPosts - 获取求助帖列表', () => {
it('应该支持分页参数', async () => {
@@ -49,11 +80,53 @@
});
describe('addPostComment - 添加帖子评论', () => {
- it('应该正确发送评论数据', async () => {
- const comment = { content: '测试评论', author: 'user1' };
- mockAxios.onPost('/help/posts/post456/comments', comment).reply(200, { code: 200 });
+ it('应该正确发送评论数据(无图片)', async () => {
+ const postId = 'post456';
+ const commentData = {
+ authorId: 'user1',
+ content: '测试评论'
+ };
- const response = await addPostComment('post456', comment);
+ mockAxios.onPost(`/help/posts/${postId}/comments`).reply(config => {
+ const data = config.data;
+ expect(data.get('authorId')).toBe(commentData.authorId);
+ expect(data.get('content')).toBe(commentData.content);
+ expect(data.has('image')).toBe(false);
+ return [200, { code: 200 }];
+ });
+
+ const response = await addPostComment('post456', commentData);
+ expect(response.status).toBe(200);
+ });
+ it('应该正确处理带图片的评论', async () => {
+ const postId = 'post456';
+ const commentData = {
+ authorId: 'user1',
+ content: '测试评论',
+ commentImage: new File(['content'], 'comment.jpg')
+ };
+
+ mockAxios.onPost(`/help/posts/${postId}/comments`).reply(config => {
+ const data = config.data;
+ expect(data.get('image')).toBeInstanceOf(File);
+ return [200, { code: 200 }];
+ });
+
+ const response = await addPostComment(postId, commentData);
+ expect(response.status).toBe(200);
+ });
+ });
+ describe('deletePost - 删除帖子', () => {
+ it('应该正确发送删除请求', async () => {
+ const postId = 'post789';
+ const authorId = 'user1';
+
+ mockAxios.onDelete(`/help/posts/${postId}`).reply(config => {
+ expect(config.params).toEqual({ authorId });
+ return [200, { code: 200 }];
+ });
+
+ const response = await deletePost(postId, authorId);
expect(response.status).toBe(200);
});
});