合并
Change-Id: I19ca58c58a513cba20c162c9ed7cbab90e060bf6
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);
});
});