ym923 | 6e0ddcf | 2025-06-09 20:14:11 +0800 | [diff] [blame^] | 1 | const axios = require('axios'); |
| 2 | jest.mock('axios'); // mock axios |
| 3 | |
| 4 | const { |
| 5 | addFriend, |
| 6 | acceptFriend, |
| 7 | rejectFriend, |
| 8 | deleteFriend, |
| 9 | getFriendsByUserId, |
| 10 | getPendingRequests |
| 11 | } = require('../friends'); // 引入你的 API 方法 |
| 12 | |
| 13 | describe('Friends API Tests', () => { |
| 14 | beforeEach(() => { |
| 15 | jest.clearAllMocks(); // 每个测试前清除 mock 状态 |
| 16 | }); |
| 17 | |
| 18 | test('addFriend should post friend request data', async () => { |
| 19 | const mockFriendData = { |
| 20 | friend1: 1, |
| 21 | friend2: 2 |
| 22 | }; |
| 23 | axios.post.mockResolvedValue({ data: true }); |
| 24 | |
| 25 | const response = await addFriend(mockFriendData); |
| 26 | |
| 27 | expect(axios.post).toHaveBeenCalledWith( |
| 28 | 'http://localhost:8080/friends/add', |
| 29 | mockFriendData |
| 30 | ); |
| 31 | expect(response.data).toBe(true); |
| 32 | }); |
| 33 | |
| 34 | test('acceptFriend should send accept request with params', async () => { |
| 35 | axios.post.mockResolvedValue({ data: true }); |
| 36 | |
| 37 | const response = await acceptFriend(1, 2); |
| 38 | |
| 39 | expect(axios.post).toHaveBeenCalledWith( |
| 40 | 'http://localhost:8080/friends/accept', |
| 41 | null, |
| 42 | { params: { friend1: 1, friend2: 2 } } |
| 43 | ); |
| 44 | expect(response.data).toBe(true); |
| 45 | }); |
| 46 | |
| 47 | test('rejectFriend should delete pending friend request', async () => { |
| 48 | axios.delete.mockResolvedValue({ data: true }); |
| 49 | |
| 50 | const response = await rejectFriend(1, 2); |
| 51 | |
| 52 | expect(axios.delete).toHaveBeenCalledWith( |
| 53 | 'http://localhost:8080/friends/delete', |
| 54 | { params: { friend1: 1, friend2: 2 } } |
| 55 | ); |
| 56 | expect(response.data).toBe(true); |
| 57 | }); |
| 58 | |
| 59 | test('deleteFriend should delete a friend relationship', async () => { |
| 60 | axios.delete.mockResolvedValue({ data: true }); |
| 61 | |
| 62 | const response = await deleteFriend(3, 4); |
| 63 | |
| 64 | expect(axios.delete).toHaveBeenCalledWith( |
| 65 | 'http://localhost:8080/friends/delete', |
| 66 | { params: { friend1: 3, friend2: 4 } } |
| 67 | ); |
| 68 | expect(response.data).toBe(true); |
| 69 | }); |
| 70 | |
| 71 | test('getFriendsByUserId should get all accepted friends for user', async () => { |
| 72 | const mockData = { data: [{ relationid: 1, friend1: 1, friend2: 2, status: 'accepted' }] }; |
| 73 | axios.get.mockResolvedValue(mockData); |
| 74 | |
| 75 | const response = await getFriendsByUserId(1); |
| 76 | |
| 77 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/friends/list/1'); |
| 78 | expect(response.data).toEqual(mockData.data); |
| 79 | }); |
| 80 | |
| 81 | test('getPendingRequests should fetch pending friend requests', async () => { |
| 82 | const mockData = { data: [{ relationid: 2, friend1: 3, friend2: 1, status: 'pending' }] }; |
| 83 | axios.get.mockResolvedValue(mockData); |
| 84 | |
| 85 | const response = await getPendingRequests(1); |
| 86 | |
| 87 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/friends/pending/1'); |
| 88 | expect(response.data).toEqual(mockData.data); |
| 89 | }); |
| 90 | }); |