聊天界面所有前端
Change-Id: I0278cae1cd7152f4f797c00ac68cbdb36a2dd768
diff --git a/src/api/__tests__/chat.test.js b/src/api/__tests__/chat.test.js
new file mode 100644
index 0000000..8571fbc
--- /dev/null
+++ b/src/api/__tests__/chat.test.js
@@ -0,0 +1,84 @@
+// src/api/__tests__/chat.test.js
+const axios = require('axios');
+const MockAdapter = require('axios-mock-adapter');
+const {
+ createChat,
+ deleteChat,
+ getChatsByUser,
+ getChatsBetweenUsers
+} = require('../chat');
+
+jest.mock('axios');
+
+describe('Chat API Tests', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ test('createChat should post chat data', async () => {
+ const mockData = {
+ senderId: 1,
+ receiverId: 2,
+ content: 'Hello!',
+ talkTime: '2025-06-09T10:00:00'
+ };
+ const mockResponse = { data: { ...mockData, informationid: 101 } };
+ axios.post.mockResolvedValue(mockResponse);
+
+ const response = await createChat(mockData);
+
+ expect(axios.post).toHaveBeenCalledWith(
+ 'http://localhost:8080/chat/create',
+ mockData
+ );
+ expect(response.data).toEqual(mockResponse.data);
+ });
+
+ test('deleteChat should send delete request', async () => {
+ axios.delete.mockResolvedValue({ data: '删除成功' });
+
+ const response = await deleteChat(101);
+
+ expect(axios.delete).toHaveBeenCalledWith('http://localhost:8080/chat/delete/101');
+ expect(response.data).toBe('删除成功');
+ });
+
+ test('getChatsByUser should fetch all chats for a user', async () => {
+ const userId = 1;
+ const mockData = { data: [{ senderId: 1, receiverId: 2, content: 'Hi' }] };
+ axios.get.mockResolvedValue(mockData);
+
+ const result = await getChatsByUser(userId);
+
+ expect(axios.get).toHaveBeenCalledWith(`http://localhost:8080/chat/user/${userId}`);
+ expect(result).toEqual(mockData.data);
+ });
+
+ test('getChatsBetweenUsers should fetch chats between two users', async () => {
+ const user1 = 1;
+ const user2 = 2;
+ const mockData = {
+ data: [{ senderId: 1, receiverId: 2, content: 'Hi there!' }]
+ };
+ axios.get.mockResolvedValue(mockData);
+
+ const result = await getChatsBetweenUsers(user1, user2);
+
+ expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/chat/between', {
+ params: { user1, user2 }
+ });
+ expect(result).toEqual(mockData.data);
+ });
+
+ test('getChatsByUser should return [] on error', async () => {
+ axios.get.mockRejectedValue(new Error('Failed'));
+ const result = await getChatsByUser(1);
+ expect(result).toEqual([]);
+ });
+
+ test('getChatsBetweenUsers should return [] on error', async () => {
+ axios.get.mockRejectedValue(new Error('Failed'));
+ const result = await getChatsBetweenUsers(1, 2);
+ expect(result).toEqual([]);
+ });
+});
diff --git a/src/api/chat.js b/src/api/chat.js
new file mode 100644
index 0000000..ab2d38d
--- /dev/null
+++ b/src/api/chat.js
@@ -0,0 +1,40 @@
+import axios from 'axios';
+
+const BASE_URL = 'http://localhost:8080/chat';
+
+// 创建聊天记录
+export const createChat = (chatData) => {
+ return axios.post(`${BASE_URL}/create`, chatData);
+};
+
+// 删除聊天记录
+export const deleteChat = (chatId) => {
+ return axios.delete(`${BASE_URL}/delete/${chatId}`);
+};
+
+// 获取某个用户的所有聊天记录(与所有人的)
+export async function getChatsByUser(userId) {
+ try {
+ const response = await axios.get(`${BASE_URL}/user/${userId}`);
+ return Array.isArray(response.data) ? response.data : [];
+ } catch (error) {
+ console.error('获取用户聊天记录失败', error);
+ return [];
+ }
+}
+
+// 获取两个用户之间的聊天记录(请求参数形式)
+export async function getChatsBetweenUsers(user1Id, user2Id) {
+ try {
+ const response = await axios.get(`${BASE_URL}/between`, {
+ params: {
+ user1: user1Id,
+ user2: user2Id
+ }
+ });
+ return Array.isArray(response.data) ? response.data : [];
+ } catch (error) {
+ console.error('获取两人聊天记录失败', error);
+ return [];
+ }
+}