好友的相关前端
Change-Id: Iebe50bff7e96fcf6c13b57c159182f54d0a38b93
diff --git a/src/api/__tests__/friends.test.js b/src/api/__tests__/friends.test.js
new file mode 100644
index 0000000..2358e04
--- /dev/null
+++ b/src/api/__tests__/friends.test.js
@@ -0,0 +1,90 @@
+const axios = require('axios');
+jest.mock('axios'); // mock axios
+
+const {
+ addFriend,
+ acceptFriend,
+ rejectFriend,
+ deleteFriend,
+ getFriendsByUserId,
+ getPendingRequests
+} = require('../friends'); // 引入你的 API 方法
+
+describe('Friends API Tests', () => {
+ beforeEach(() => {
+ jest.clearAllMocks(); // 每个测试前清除 mock 状态
+ });
+
+ test('addFriend should post friend request data', async () => {
+ const mockFriendData = {
+ friend1: 1,
+ friend2: 2
+ };
+ axios.post.mockResolvedValue({ data: true });
+
+ const response = await addFriend(mockFriendData);
+
+ expect(axios.post).toHaveBeenCalledWith(
+ 'http://localhost:8080/friends/add',
+ mockFriendData
+ );
+ expect(response.data).toBe(true);
+ });
+
+ test('acceptFriend should send accept request with params', async () => {
+ axios.post.mockResolvedValue({ data: true });
+
+ const response = await acceptFriend(1, 2);
+
+ expect(axios.post).toHaveBeenCalledWith(
+ 'http://localhost:8080/friends/accept',
+ null,
+ { params: { friend1: 1, friend2: 2 } }
+ );
+ expect(response.data).toBe(true);
+ });
+
+ test('rejectFriend should delete pending friend request', async () => {
+ axios.delete.mockResolvedValue({ data: true });
+
+ const response = await rejectFriend(1, 2);
+
+ expect(axios.delete).toHaveBeenCalledWith(
+ 'http://localhost:8080/friends/delete',
+ { params: { friend1: 1, friend2: 2 } }
+ );
+ expect(response.data).toBe(true);
+ });
+
+ test('deleteFriend should delete a friend relationship', async () => {
+ axios.delete.mockResolvedValue({ data: true });
+
+ const response = await deleteFriend(3, 4);
+
+ expect(axios.delete).toHaveBeenCalledWith(
+ 'http://localhost:8080/friends/delete',
+ { params: { friend1: 3, friend2: 4 } }
+ );
+ expect(response.data).toBe(true);
+ });
+
+ test('getFriendsByUserId should get all accepted friends for user', async () => {
+ const mockData = { data: [{ relationid: 1, friend1: 1, friend2: 2, status: 'accepted' }] };
+ axios.get.mockResolvedValue(mockData);
+
+ const response = await getFriendsByUserId(1);
+
+ expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/friends/list/1');
+ expect(response.data).toEqual(mockData.data);
+ });
+
+ test('getPendingRequests should fetch pending friend requests', async () => {
+ const mockData = { data: [{ relationid: 2, friend1: 3, friend2: 1, status: 'pending' }] };
+ axios.get.mockResolvedValue(mockData);
+
+ const response = await getPendingRequests(1);
+
+ expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/friends/pending/1');
+ expect(response.data).toEqual(mockData.data);
+ });
+});
diff --git a/src/api/friends.js b/src/api/friends.js
new file mode 100644
index 0000000..794e061
--- /dev/null
+++ b/src/api/friends.js
@@ -0,0 +1,39 @@
+import axios from 'axios';
+
+const BASE_URL = 'http://localhost:8080/friends';
+
+// 添加好友(发起好友请求)
+export const addFriend = (friendData) => {
+ return axios.post(`${BASE_URL}/add`, friendData);
+};
+
+// 同意好友申请
+export const acceptFriend = (friend1, friend2) => {
+ return axios.post(`${BASE_URL}/accept`, null, {
+ params: { friend1, friend2 }
+ });
+};
+
+// 拒绝好友申请(删除 pending 状态的好友关系)
+export const rejectFriend = (friend1, friend2) => {
+ return axios.delete(`${BASE_URL}/delete`, {
+ params: { friend1, friend2 }
+ });
+};
+
+// 删除好友
+export const deleteFriend = (friend1, friend2) => {
+ return axios.delete(`${BASE_URL}/delete`, {
+ params: { friend1, friend2 }
+ });
+};
+
+// 查询某用户所有已通过好友
+export const getFriendsByUserId = (userid) => {
+ return axios.get(`${BASE_URL}/list/${userid}`);
+};
+
+// 查询某用户收到的好友申请(pending 状态)
+export const getPendingRequests = (userid) => {
+ return axios.get(`${BASE_URL}/pending/${userid}`);
+};